成都建设网站那家好,wordpress的视频教程,wordpress 前端表单,青海西宁网页网站制作springboot 动态导出pdf 原理是通过freemarker插入数据到html模版里面#xff0c;html你就叫前端帮你写好#xff0c;他不写你就告诉你主管让他叫他写#xff0c;你只管往预留的位置插入数据就行#xff0c;然后通过itextpdf会将准备好html文件转成PDF。 ### 1、准备依赖 html你就叫前端帮你写好他不写你就告诉你主管让他叫他写你只管往预留的位置插入数据就行然后通过itextpdf会将准备好html文件转成PDF。 ### 1、准备依赖 !--下载pdf begin--dependencygroupIdcom.itextpdf/groupIdartifactIdhtml2pdf/artifactIdversion4.0.3/version/dependency!-- freemarker模版 --dependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.30/version/dependency!--下载pdf end--### 2、准备html模版
html
headmeta charsetUTF-8style.main-container {font-family: SF-UI-Text-Regular;width: 21cm;margin: 0 auto;padding: 20px;}.float-left {float: left;}.float-right {float: right;}.padding-left-right {padding-left: 20px;padding-right: 20px;}.margin-left-right {margin-left: 20px;margin-right: 20px;}.section {/*border: 3px dashed #CCC;*//*padding: 20px;*/border-Top: 2px dashed #D3D3D3;padding-Top: 13px;margin-bottom: 16px;margin-Top: 20px;}.section-details p {color: #126089;/*font-size: 16px;*/}.grouper {overflow: hidden;}.grid-50 {width: 50%;}font-face {font-family: SF-UI-Text-Regular;src: url(https://s3-ap-southeast-1.amazonaws.com/content.staging.whitecoat.global/PDF/Templates/fonts/SF-UI-Text-Regular.otf);font-style: normal;}h1 {font-size: 22px;font-family: SF-UI-Text-Regular;}.h1-main {margin-top: 90px;}p {margin: 0 0 10px 0;font-family: SF-UI-Text-Regular;font-size: 12px;}.p-spec {color: #149a9a;font-weight: bold;}table, th, td {padding: 5px;border-collapse: collapse;}th, td {border: 1px solid #D3D3D3;}.margin-b-30 {margin-bottom: 30px;}.row:after {content: ;display: table;clear: both;}.column-4 {float: left;width: 33.33%;padding: 10px;}.column-8 {float: left;width: 66.66%;padding: 10px;}* {box-sizing: border-box;font-size: 12px;}/style
/head
bodydiv classmain-containerdiv stylebackground-color: #fffdiv classgrouper main-header grouper padding-left-right stylepadding-top: 20pxdiv classmargin-b-30 rowdiv classcolumn-4img stylemax-width:90%srchttps://s3-ap-southeast-1.amazonaws.com/content.staging.whitecoat.global/PDF/Templates/Images/WhiteCoat_Tax_Invoice.png/divdiv classcolumn-4WhiteCoat Holdings Pte Ltd. br/Clinic Licence No.: 18M0119/01/182 br/201 Henderson Road #05-11/12 br/Apex Henderson br/Singapore 159545 br//divdiv classcolumn-4GST Reg. No: 201610235E br/T: 65 6909 6909 br/F: 65 6909 6909 br/E: contactuswhitecoat.com.sg br/a styletext-decoration: none; hrefhttps://whitecoat.com.sg/whitecoat.com.sg/a/div/div/divdiv classmain-bodydiv classgrouperdiv classgrouper margin-left-right row styleborder-Top: 1px solid #D3D3D3;padding-top:20px;div classcolumn-4Name: br/${data.name!} br/br/NRIC/FIN/Passport No: br/${data.idNo!}/divdiv classcolumn-8Invoice No.: br/${data.invoiceNo!} br/br/Invoice Date: br/${data.invoiceDateStr!} br/br/iThis is a computer generated invoice. No signature is required./i/div/divdiv classgrouper margin-left-right styleborder-Top: 1px solid #D3D3D3;padding-top:20px;tabletrthS/N/thth colspan2Description/thth styleborder:1px solid #D3D3D3;Amount/th/trtrtd stylewidth:30px;text-align:center;1/tdtd stylewidth:550px; border-right: noneMedication br/#list data.medicationList as item${item.productName!} br//#listibr/Subtotal/i/tdtd styletext-align:right; border-left: noneQuantity br/#list data.medicationList as item${item.quantity!} br//#listi stylecolor:#fffbr/Subtotal/i/tdtd stylewidth:200px;text-align:rightbr/#list data.medicationList as item$${item.amount!} br//#listbr/i${data.subtotal!}/i/td/trtrtd styletext-align:center;2/tdtd colspan2Administrative charges/tdtd styletext-align:right$${data.adminCharge!}/td/trtrtd colspan3 styletext-align:right; border-bottom: none; border-left: noneSubtotal(Excluding GST)/tdtd styletext-align:right$${data.subtotalExcludingGST!}/td/trtrtd colspan3styletext-align:right; ; border-bottom: none; border-top: none; border-left: noneGST(${data.tax!}%)/tdtd styletext-align:right$${data.GST!}/td/trtrtd colspan3styletext-align:right; border-top: none; border-left: none; border-bottom: noneTotal(Including GST)/tdtd styletext-align:right$${data.totalIncludingGST!}/td/tr/table/divdiv classgrouper margin-left-right float-left stylepadding-bottom :20px;padding-top :20pxspanBank remittance details for payment by Bank Telegraphic br/Transfer in Singapore Dollars to:br/Name of Bank: United Overseas Bank Ltd. Holland Branchbr/Account Name: Whitecoat Holdings Pte Ltd.br/Account No: 341-308-316-6br/Bank Code: 7375br/Branch Code: 020br/Bank Swift Code: UOVBSGSG/span/div/div/div/div/body
/html### 3、下载代码 ApiOperation(value 下载pdf)GetMapping(downloadPDF/staffPurchase)public ResponseResult download(String id, HttpServletResponse response) throws Exception {StaffPurchasePDFVo data staffPurchaseMapper.getStaffPurchasePDFData(id);HashMapString, Object mapData Maps.newHashMap();mapData.put(data, data);long timeMillis System.currentTimeMillis();String templateContent HtmlUtils.getTemplateContent(Staff_Purchase_Receipt.ftl, mapData);HtmlUtils.html2Pdf(response, templateContent, timeMillis );return ResponseResult.success();}### 4、htmlutils代码
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.layout.font.FontProvider;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.Objects;Slf4j
public class HtmlUtils {/*** return* throws Exception*/public static String getTemplateDirectory() {ClassLoader classLoader HtmlUtils.class.getClassLoader();URL resource classLoader.getResource(templates);try {return Objects.requireNonNull(resource).toURI().getPath();} catch (URISyntaxException e) {log.error(获取模板文件夹失败,{}, e);}return null;}/*** 获取模板内容** param templateName 模板文件名* param paramMap 模板参数* return* throws Exception*/public static String getTemplateContent(String templateName, MapString, Object paramMap) throws Exception {Configuration config ApplicationContextUtil.getBean(FreeMarkerConfigurer.class).getConfiguration();config.setDefaultEncoding(UTF-8);Template template config.getTemplate(templateName, UTF-8);return FreeMarkerTemplateUtils.processTemplateIntoString(template, paramMap);}/*** HTML 转 PDF** param content html内容* param outPath 输出pdf路径* return 是否创建成功*/public static boolean html2Pdf(String content, String outPath) {try {ConverterProperties converterProperties new ConverterProperties();converterProperties.setCharset(UTF-8);FontProvider fontProvider new FontProvider();fontProvider.addSystemFonts();converterProperties.setFontProvider(fontProvider);HtmlConverter.convertToPdf(content, new FileOutputStream(outPath), converterProperties);} catch (Exception e) {log.error(生成模板内容失败,{}, e);return false;}return true;}/*** HTML 转 PDF** param content html内容* return PDF字节数组*/public static void html2Pdf(HttpServletResponse response, String content,String filename) throws IOException {response.setHeader(Content-Type, application/octet-stream);response.setCharacterEncoding(utf-8);response.setHeader(Content-Disposition, attachment;filenamefilename.pdf);try (ServletOutputStream outputStream response.getOutputStream()) {ConverterProperties converterProperties new ConverterProperties();converterProperties.setCharset(UTF-8);FontProvider fontProvider new FontProvider();fontProvider.addSystemFonts();converterProperties.setFontProvider(fontProvider);HtmlConverter.convertToPdf(content, outputStream, converterProperties);} catch (Exception e) {log.error(生成 PDF 失败,{}, e);}}
} ApplicationContextUtil代码、
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;Configuration
AutoConfigureOrder(-1)
public class ApplicationContextUtil implements ApplicationContextAware {private static ApplicationContext applicationContext null;public static Object getBeanByName(String beanName) {if (applicationContext null) {return null;}return applicationContext.getBean(beanName);}public static T T getBean(ClassT type) {return applicationContext.getBean(type);}Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {ApplicationContextUtil.applicationContext applicationContext;}}5、freemarker的基本语法
https://developer.aliyun.com/article/942606