PDF操作,常见以下几种方法:i
PDF操作,常见以下几种方法:i
一、概述
生成PDF文档通常涉及使用模板引擎、PDF库以及数据填充。常见以下几种方法:
iText:
iText是一个强大的PDF库,支持创建和操作PDF文档。
使用场景: 您可以使用iText来直接构建PDF文档,也可以将其与模板引擎结合使用,通过数据填充来生成PDF。
Apache PDFBox:
PDFBox是Apache软件基金会的一个项目,提供创建和处理PDF文档的功能。
使用场景: PDFBox可用于构建PDF文档,您可以将其用于模板生成PDF。
Apache FOP (Formatting Objects Processor):
FOP是Apache XML Graphics项目的一部分,用于将XML文档转换为PDF、PS、PNG等格式。
使用场景: FOP通常与XSL-FO(可扩展样式语言 - 格式化对象)一起使用,通过XSL-FO模板生成PDF。
Thymeleaf:
Thymeleaf是一个用于Web和独立环境的现代服务器端Java模板引擎。
使用场景: Thymeleaf可以用于生成HTML模板,然后通过转换库(如Flying Saucer)将HTML转换为PDF。
Freemarker和Velocity:
Freemarker和Velocity是两个常见的模板引擎,可以用于生成文本模板。
使用场景: 您可以使用这些引擎生成包含占位符的文本模板,然后使用PDF库将其转换为PDF。
JasperReports:
简介: JasperReports是一个用于生成报表的开源报表引擎。
使用场景: JasperReports支持定义报表模板,将数据与模板结合生成PDF报表。
Flying Saucer(XHTMLRenderer):
Flying Saucer是一个基于Java的渲染引擎,可将XHTML和CSS转换为PDF。
使用场景: 您可以使用Flying Saucer来渲染HTML模板,并将其转换为PDF。
二、使用
1、word 转 PDF ,使用aspose工具包
引入jar包:可以通过maven,或者项目本地路径指定
<!--word转PDF mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose.slides -Dversion=15.9.0 -Dpackaging=jar -Dfile=e:/test/jar/aspose.slides-15.9.0.jar mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-cells -Dversion=8.5.2 -Dpackaging=jar -Dfile=e:/test/jar/aspose-cells-8.5.2.jar mvn install:install-file -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=18.6 -Dpackaging=jar -Dfile=e:/test/jar/aspose-words-18.6-jdk16.jar --> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose.slides</artifactId> <version>15.9.0</version> <!-- <type>jar</type> <scope>system</scope> <systemPath>${project.basedir}/src/main/lib/aspose.slides-15.9.0.jar</systemPath>--> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-cells</artifactId> <version>8.5.2</version> <!-- <type>jar</type> <scope>system</scope> <systemPath>${project.basedir}/src/main/lib/aspose-cells-8.5.2.jar</systemPath>--> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>18.6</version> <!-- <type>jar</type> <scope>system</scope> <systemPath>${project.basedir}/src/main/lib/aspose-words-18.6-jdk16.jar</systemPath>--> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>View Code
linux服务器中文乱码解决,导出的PDF乱码如图:
解决方案
方案1: 环境解决:亲测可行,记得安装后重启项目
安装字库,将win的c:\windows\fonts下的全部文件拷贝到生产服务器字体安装目录下。
#查看linux目前的所有字体
fc-list
#查看Linux目前的所有中文字体
fc-list :lang=zh
#拷贝到linux下的字体目录
mkdir /usr/share/fonts/win
#更新系统中字体缓存
cd /usr/share/fonts
sudo fc-cache -fv
执行命令让字体生效
source /etc/profile
方案2: 代码解决(推荐)
a.将window中字体放到linux的/usr/shared/fonts/chinese目录
b.在aspose代码中添加:
//linux环境需要配置中文字体:
OsInfo osInfo = SystemUtil.getOsInfo();
if(osInfo.isLinux()){
FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts", true);
}
//新建一个空白pdf文档,byteArrayInputStream为word的输入流
com.aspose.words.Document pdf = new com.aspose.words.Document(byteArrayInputStream);
pdf.save(os, SaveFormat.PDF);
os.flush();
示例
/** * * @param docxPath E:\test\docment\c.docx * @param pdfPath E:\test\docment\a.pdf * @return */ public static String convertDocToPdf(String docxPath, String pdfPath) { if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生 return "PDF格式转化失败"; } try { // 新建一个空白pdf文档 FileOutputStream os = new FileOutputStream(new File(pdfPath)); Document doc = new Document(docxPath); // Address是将要被转化的word文档 doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, os.close(); return pdfPath; } catch (Exception e) { e.printStackTrace(); } return "PDF格式转化失败"; } public static boolean getLicense() { boolean result = false; try { // license.xml应放在..\WebRoot\WEB-INF\classes路径下 // InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("license/license.xml"); InputStream is = PdfUtil.class.getResourceAsStream("license/license.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
2、word 转 PDF ,使用pdf-gae工具包
缺点:会与itext.jar产生冲突,如果你的项目中已有部分业务使用了itext.jar里面的内容,则推荐上一种方式。
导入jar包:
<!--convert doc to pdf--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <!--此种转换方式会与itext冲突:尝试多次后无法解决--> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId> <version>2.0.2</version> <exclusions> <exclusion> <artifactId>org.apache.poi</artifactId> <groupId>poi-ooxml</groupId> </exclusion> </exclusions> </dependency>View Code
示例:
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Date; /** * 参考:https://www.cnblogs.com/h-w-b/p/17352151.html */ public class WordUtils { /** * 方式一:会与itext.jar产生冲突,如果你的项目中已有部分业务使用了itext.jar里面的内容,则推荐其他方式。 */ public static void convertDocxToPdf() { String pdfFilePath = "E:\\test\\docment\\a.pdf"; String docxFilePath = "E:\\test\\docment\\c.docx"; String targetPaht = "E:\\test\\docment\\" + new Date().getTime()+".pdf"; try{ InputStream inputStream = new FileInputStream(docxFilePath); FileOutputStream outputStream = new FileOutputStream(targetPaht); XWPFDocument xwpfDocument = new XWPFDocument(inputStream); PdfOptions pdfOptions = PdfOptions.create(); PdfConverter.getInstance().convert(xwpfDocument, outputStream, pdfOptions); outputStream.flush(); outputStream.close(); System.out.println("==========="); } catch (Exception e) { e.printStackTrace(); } } }
用户点评