Use Freemarker to generate pdf files

2022-09-02

        Today I received a task to generate pdf, and the web end must be able to download; I also found a lot of tools on the Internet such as: itext, etc., I feel that it is quite complicated and not so easy to use, and then I remembered that I used Freemarker to generate world documents before. It is easy to use, and then the investigation found that pdf can also be generated, but there is a little difference. If Freemarker generates the world, it uses the world document as a template, while pdf is relatively simple, directly using html files to make templates, but at the end the file suffix must be . Change to ftl file.

This blogger writes very well, you can go directly to this blogger’s article, I just record it as a note, and refer to the article link

Link to this article: Java uses Freemarker to export PDF files through template files and display them horizontally Responsible, or the effect is not ideal. After repeated searching for information, it is found that this method is the most ideal. 1. Rely on jar package<!-- freemarker reads html template file--><dependency> <groupId>org.freemarker</groupId&gt ; & https://blog.csdn.net/weixin_39806100/article/details/86616041

code show as below:

  • maven dependencies:

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.29</version>
</dependency>

<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf</artifactId>
    <version>9.1.18</version>
</dependency>
  • service layer:

public void exportPdf(HttpServletResponse response, Integer id, Integer type) throws Exception {
        ByteArrayOutputStream baos = null;
        OutputStream out = null;
        FileOutputStream fileOutputStream = null;
        try {
            //获取提货单数据,根据提货单id
            TakeOrder takeOrder = this.getTakeById(id);
            //翻译提货单状态
            String[] stateName = {"待备货","备货中","已备货","已出库","装车中","已装车","已进厂","已出厂"};
            takeOrder.setStateName(takeOrder.getState() == null ? "" : stateName[takeOrder.getState() - 1]);
            //翻译提货单提货状态
            String[] orderStateName = {"待提货","已提货","作废"};
            takeOrder.setOrderStateName(orderStateName[takeOrder.getOrderState() - 1]);
            
            // 模板中的数据,实际运用从数据库中查询
            Map<String,Object> data = new HashMap<>();
            data.put("takeOrder", takeOrder);
            data.put("fileName", type == 1 ? "备货联" : "承运联");

            //因为我自己的需求有两套模板,所以我让模板名称动态化了,如果不用直接删除这个type参数,正常填文件名称就可以,记得带上后缀
            baos = PDFTemplateUtil.createPDF(data, "modezs"+type+".ftl");
            // 设置响应消息头,告诉浏览器当前响应是一个下载文件
            response.setContentType( "application/x-msdownload");
            // 告诉浏览器,当前响应数据要求用户干预保存到文件中,以及文件名是什么 如果文件名有中文,必须URL编码 
            String fileName = URLEncoder.encode("月度报告.pdf", "UTF-8");
            response.setHeader( "Content-Disposition", "attachment;filename=" + fileName);
            out = response.getOutputStream();
            baos.writeTo(out);
            baos.close();
            //下载到本地位置
//            fileOutputStream = new FileOutputStream("D:\\zscProject\\zsc.pdf");
            //生成pdf完成记录行为记录
            this.addActionLog(takeOrder.getTakeOrderNo(),1);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("导出失败:" + e.getMessage());
        } finally{
            if(baos != null){
                baos.close();
            }
            if(out != null){
                out.close();
            }
            if (fileOutputStream != null){
                fileOutputStream.close();
            }
        }
    }

 ps:

1. When using the tool class, pass the parameter of the file name

 2. If you do not need to download pdf on the web side, you can use the file output stream to download directly to the local

 

  • Tools:

can be used directly

public class PDFTemplateUtil {

	/**
	 * 通过模板导出pdf文件
	 * @param data 数据
	 * @param templateFileName 模板文件名
	 * @throws Exception
	 */
    public static ByteArrayOutputStream createPDF(Map<String,Object> data, String templateFileName) throws Exception {
        // 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例
        Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 指定FreeMarker模板文件的位置 
        cfg.setClassForTemplateLoading(PDFTemplateUtil.class,"/templates");
        ITextRenderer renderer = new ITextRenderer();
        OutputStream out = new ByteArrayOutputStream();
        try {
            // 设置 css中 的字体样式(暂时仅支持宋体和黑体) 必须,不然中文不显示
            renderer.getFontResolver().addFont("/templates/font/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            // 设置模板的编码格式
            cfg.setEncoding(Locale.CHINA, "UTF-8");
            // 获取模板文件 
            Template template = cfg.getTemplate(templateFileName, "UTF-8");
            StringWriter writer = new StringWriter();
            
            // 将数据输出到html中
            template.process(data, writer);
            writer.flush();

            String html = writer.toString();
            // 把html代码传入渲染器中
            renderer.setDocumentFromString(html);

             // 设置模板中的图片路径 (这里的images在resources目录下) 模板中img标签src路径需要相对路径加图片名 如<img src="images/xh.jpg"/>
//            URI images = PDFTemplateUtil.class.getClassLoader().getResource("images").toURI();
//            if (images != null) {
//                String url = images.toString();
//                renderer.getSharedContext().setBaseURL(url);
//            }
            renderer.layout();
            
            renderer.createPDF(out, false);
            renderer.finishPDF();
            out.flush();
            return (ByteArrayOutputStream)out;
        } finally {
        	if(out != null){
        		 out.close();
        	}
        }
    }
}

ps:

The pdf I exported contains pictures, but my pictures are base64 bytecodes (this method is recommended), and the data is not filled in a local way. If you can only use local pictures, change this section in the tool class Unpack the code and improve it yourself

 

  • Template file:

<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<title></title>
	<style>
		* {
			margin: 0;
			padding: 0;
			box-sizing: border-box;
		}

		body {
			font-family: SimSun;
			padding: 30px 20px 0;
		}

		section {
			display: block;
			/* margin: 20px 10px; */
		}

		.title {
			text-align: center;
			margin-bottom: 20px;
		}

		.preface p {
			line-height: 30px;
			display: inline-block;
		}

		.preface p.content {
			text-indent: 2em;
		}

		section>table {
			border-collapse: collapse;
			table-layout: fixed;
			width: 100%;
			font-size: 13px;
			/* margin: 20px 0px; */
			text-align: center;
			word-wrap: break-word;
		}

		section table td {
			padding: 5px 0px;
		}
		.topTitle section{
			width: 30%;
			font-size: 13px;
			display: inline-block;
			margin-top: 20px;
		}
		.topTitle{
		}
		.outTitle{
		}
		.outTitle section{
			font-size: 13px;
			display: inline-block;
		}
		.detail{
			margin-top: 20px;
		}
		.outTable{
			margin-bottom: 20px;
		}
		.box1{
		}
		.box2{
			width: 80%;
			display: inline-block;
		}
		.box3{
			display: inline-block;
			width: 18%;
			/* min-width: 180px; */
		}
		.box3 img{
			width: 100%;
		}
		.box3 p{
			font-size: 12px;
		}
	</style>
</head>

<body>
<h3>${(fileName)!''}</h3>
<div class="box1">
	<section class="title">
		<h2>XXXXXXXXXXXXXX有限公司</h2>
		<h2>提货单</h2>
	</section>
	<div class="box2">
		<!-- 标题 start -->
		<!-- 标题 end -->

		<!-- 前言 start -->
		<div class="topTitle">
			<section class="preface">
				<p>提货单号:</p>
				<p>${(takeOrder.takeOrderNo)!''}</p>
			</section>
			<section class="preface">
				<p>提货日期:</p>
				<p>${(takeOrder.takeDate)!''}</p>
			</section>
			<section class="preface">
				<p>提货状态:</p>
				<p>${(takeOrder.orderStateName)!''}</p>
			</section>
			<section class="preface">
				<p>状态:</p>
				<p>${(takeOrder.stateName)!''}</p>
			</section>
<#--			<section class="preface">-->
<#--				<p>承运商:</p>-->
<#--				<p>${(takeOrder.takeOrderNo)!''}</p>-->
<#--			</section>-->
<#--			<section class="preface">-->
<#--				<p>车辆:</p>-->
<#--				<p>${(takeOrder.takeOrderNo)!''}</p>-->
<#--			</section>-->
			<section class="preface">
				<p>司机:</p>
				<p>${(takeOrder.driver)!''}</p>
			</section>
			<section class="preface">
				<p>发运方式:</p>
				<p>${(takeOrder.shippingMethod)!''}</p>
			</section>
		</div>
	</div>
	<div class="box3">
		<img src="${(takeOrder.qrCode)!''}"></img>
		<p>凭此二维码进出厂区</p>

	</div>
</div>
<!-- 前言 end -->


<!-- 产品列表 start -->
<#if takeOrder.outOrderProducts ??>
<section class="detail">
	<table border="1" cellspacing="0" cellpadding="0">
		<tr>
			<td width="15%">品名编号</td>
			<td width="12%">品名</td>
			<td width="12%">规格型号</td>
			<td width="12%">销售型号</td>
			<td width="12%">包装规格</td>
			<td width="12%">批号</td>
			<td width="12%">数量</td>
			<td width="12%">单位</td>
			<td width="12%">仓库编号</td>
			<td width="12%">仓库名称</td>
		</tr>
		<#list takeOrder.outOrderProducts as ad>
			<tr>
				<td>${(ad.productCode)!''}</td>
				<td>${(ad.productName)!''}</td>
				<td>${(ad.typeNum)!''}</td>
				<td>${(ad.saleType)!''}</td>
				<td>${(ad.packSize)!''}</td>
				<td>${(ad.batchNumber)!''}</td>
				<td>${(ad.num)!''}</td>
				<td>${(ad.uint)!''}</td>
				<td>${(ad.stockNo)!''}</td>
				<td>${(ad.stockName)!''}</td>
			</tr>
		</#list>
	</table>
</section>
</#if>
<!-- 产品列表 end -->

<!-- 出库单 start -->
<#if takeOrder.outOrders ??>
<section class="detail">
	<h3>出库单信息:</h3>
	<#list takeOrder.outOrders as add>
	<div class="outTitle" >
		<section class="preface">
			<p>出库单号:</p>
			<p>${(add.outOrderNo)!''}</p>
		</section>
		<section class="preface">
			<p>发货单号:</p>
			<p>${(add.sendOrderNo)!''}</p>
		</section>
		<section class="preface">
			<p>出库日期:</p>
			<p>${(add.outDate)!''}</p>
		</section>
		<section class="preface">
			<p>装车号:</p>
			<p>${(add.loadingNumber)!''}</p>
		</section>
		<section class="preface">
			<p>客户名称:</p>
			<p>${(add.customerName)!''}</p>
		</section>
	</div>
	<!--出库的单产品列表-->
	<#if add.outOrderProducts ??>
	<table class="outTable" border="1" cellspacing="0" cellpadding="0">
		<tr>
			<td width="15%">品名编号</td>
			<td width="12%">品名</td>
			<td width="12%">规格型号</td>
			<td width="12%">客户销售型号</td>
			<td width="12%">包装规格</td>
			<td width="12%">批号</td>
			<td width="12%">数量</td>
			<td width="12%">内部备注</td>
			<td width="12%">备注</td>
		</tr>
		<#list add.outOrderProducts as ad>
			<tr>
				<td>${(ad.productCode)!''}</td>
				<td>${(ad.productName)!''}</td>
				<td>${(ad.typeNum)!''}</td>
				<td>${(ad.saleType)!''}</td>
				<td>${(ad.packSize)!''}</td>
				<td>${(ad.batchNumber)!''}</td>
				<td>${(ad.num)!''}</td>
				<td>${(ad.innerRemark)!''}</td>
				<td>${(ad.remark)!''}</td>
			</tr>
		</#list>
	</table>
	</#if>
	</#list>
</section>
</#if>
<!-- 出库单 end -->
</body>

</html>

ps:

1. The style here is based on the html style. If you design it yourself, you need to adjust the style yourself. It only supports positioning and floating, and does not support self-adaptation. It is recommended to write the style in the class instead of the style.

2. Like <#if takeOrder.outOrderProducts ??>, <#list takeOrder.outOrderProducts as ad> and ${(fileName)!''} are the grammar of ftl files, if you don't understand, you can search

The placeholder can be seen in the figure below

Link from this article: Simple method of Freemarker to judge whether an object is empty_OxYGC's blog-CSDN blog_freemarker if judged to be empty , freemarker will report an error. If you need to judge whether the object is empty: 2. Of course, you can also avoid the error that the object is empty by setting the default value ${name!''}. If the name is empty, it will be displayed with the default value (the character after "!"). 3. If the object user and name are attributes of user, both user and name may be empty, so... https://blog.csdn.net/YangCheney/article/details/105832444 3. Placement as shown in the picture

 

 

  • Font file:

In the path of C:\Windows\Fonts  in the windows10 system , search for Arial after entering ( ps: Be sure to search for Arial, do not search by the name simsun, anyway, pay attention to the suffix of the file is .ttc, not .ttf of )

  • front end:

1. Request method js: ( ps: Pay attention to the parameter responseType: 'arraybuffer' inside, because the byte array stream used in the background is written, so if you use responseType: 'blob' directly, it will result in an encapsulated blob object There is a problem, the downloaded pdf file is damaged, the link of this article:

When using FreeMarker to generate pdf, the code is not abnormal, but the file downloaded from the web end is damaged_A-Superman's Blog-CSDN Blog When using FreeMarker to generate pdf, the code is not abnormal, but the file downloaded from the web end is damaged /blog.csdn.net/Jackbillzsc/article/details/126662319

export function exportPdf(parameter) {
  return request({
    url: 'XXXXXXXXXXXXXXX/export/pdf',
    method: 'get',
    params:parameter,
    responseType: 'arraybuffer',
    
  })
}

2. Encapsulate the blob object and download the js method of pdf:

exportPdf(type) {
      this['loading'+type] = true
      exportPdf({ id: this.pageList.id, type: type }).then((res) => {
        if (!res) {
          alert('数据为空')
          return
        }
        const content = res
        const blob = new Blob([content], { type: 'application/pdf' })
        // const fileName = titName?titName: ''
        let fileName = this.pageList.takeOrderNo
        if ('download' in document.createElement('a')) {
          // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
          this['loading'+type] = false
        } else {
          // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },

Guess you like

Origin blog.csdn.net/Jackbillzsc/article/details/126783990