java word导出pdf

整体思路就是: 使用word模板引擎,将占位符(格式:{{title}})替换为查询出来的数据,再将替换完成的word转换为pdf

doc转pdf源码: https://gitee.com/java\_long/docs-to-pdf-converter.git

word模板引擎: https://github.com/Sayi/poi-tl.git

pom依赖:

<dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.6.0-beta1</version> </dependency> <dependency> <groupId>docs-to-pdf-converter</groupId> <artifactId>docs-to-pdf-converter</artifactId> <version>1.8</version> </dependency>

代码实现:

//word模板替换 XWPFTemplate template = XWPFTemplate.compile_("/Users/andy.l.an/IdeaProjects/ibuild-cscec82-contract/ibuild-cscec82-contract-portal/src/main/resources/word/subcontract/template/template.docx").render(new HashMap<String, Object>(){{ _ put_("title", "Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎Poi-tl 模板引擎"); }}); FileOutputStream out = new FileOutputStream("/Users/andy.l.an/IdeaProjects/ibuild-cscec82-contract/ibuild-cscec82-contract-portal/src/main/resources/word/subcontract/template/out_template.docx"); template.write(out); //word转换pdf FileOutputStream pdfout = new FileOutputStream("/Users/andy.l.an/IdeaProjects/ibuild-cscec82-contract/ibuild-cscec82-contract-portal/src/main/resources/word/subcontract/template/out_template.pdf"); DocxToPDFConverter docxToPDFConverter = new DocxToPDFConverter(new FileInputStream(new File("/Users/andy.l.an/IdeaProjects/ibuild-cscec82-contract/ibuild-cscec82-contract-portal/src/main/resources/word/subcontract/template/out_template.docx")), pdfout, true, true); docxToPDFConverter.convert(); out.flush(); out.close(); template.close()_;

踩坑记:

中文转换为空:

系统需要装word使用的字体:

mac: 已经要把字体放到/Library/Fonts 目录下, 仅仅通过字体集安装不行

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

PDF转WORD为什么是历史难题

PDF转Word是一个非常非常普遍的需求,可谓人人忌危,为什么如此普遍的需求,却如此难行呢,还得看为什么会有这样的一个需求:PDF文档遵循iOS32000的规范是由Adobe公司推出的文档格式,之所以应用如此广泛,是因为PDF精确定位了每个字符的坐标、根据坐标绘制的各种形状,使用PDF格式传输和打印文档可以保证格式的一致性,然后很多PDF文件是可用于阅

java代码操作word模板并生成PDF

这个博客自己现在没时间写,等后面有时间了,自己再写。这中需求是在实际的项目开发中是会经常遇到的。下面我们先从简单入手一步一步开始。1.首先,使用word创建一个6行两列的表格。点击插入6行2列的表格,如图所示:!(https://img2018.cnblogs.com/blog/1527997/201910/15279972019

java将pdf文件转为word

importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.io.Writer;importorg.apache.pdfbox.pdmode

通过Java实现Word转PDF

Word转为PDF是非常常见的一种格式转换。通过转换可以将文档以更为稳定的格式进行保存,避免他人随意修改格式和内容。其实Word转PDF并不难,除了直接转换外也可以通过编程的方式来实现。网上相关的教程分享也很多。今天想介绍一个JavaWord组件——Fre

利用Python将Word试卷匹配转换为Excel表格

需求有一个下面这种形式的word表格:希望能转换为下面这种格式的excel表格:测试word文档读取先测试一个word文档前1页的数据读取:fromdocximportDocumentdocDocument("编号02质检员高级技师(一级)理论试卷.docx")fori,paragraphinenumerate(doc.par

java word导出pdf - HelloWorld