linux 下xdocreport 生成word 和pdf 乱码(中文是空,或者方块)
2017-12-03 13:20
711 查看
最近项目需要pdf打印,通过调研和网上搜索xdocreport,处理图片和word,pdf无论是生成速度和内存消耗,比doc4j都要速度快,内存消耗小。开发一直在window下面,生成pdf正常,中文不会乱码,部署到linux服务器就各种问题。
解决方法:
1. 所引入jar包 pom.xml
模板是放到服务器文件夹,可以直接读取,或者放到项目resource中,打包需要过滤,不然会文件损坏(pom部分代码)。
3.需要有解析字体文件,需放到服务器的文件夹下读取,如放到resource下,无法加载字体,导致打印pdf乱码
4、生成模板文件
1.必须是docx文件,office2013 或者office2010 都可以,先新建一个docx文件,然后更改名称.zip文件
—》
打开zip模板中word/docment.xml,可作为模板,如果只要取出所需的xml就可以做作为模板啦,
5、jar冲突、如:引入1步的jar后,不需要引入poi和poi-ooxml,如引入可能造成jar冲突而报错
6、word 文档中最好是宋体,我项目中加载的是宋体的字体文件
7、Notepad++的XML Tools插件格式化XML文件
8、使用freemaker填充xml模版(el 表达式) 如:
9、后续代码放到git
参考:
基于freemarker ,xdocreport生成word,pdf
Notepad++的XML Tools插件格式化XML文件
解决方法:
1. 所引入jar包 pom.xml
<xdocreport.version>2.0.1</xdocreport.version> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.core</artifactId> <version>${xdocreport.version}</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId> <version>${xdocreport.version}</version> </dependency> <dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.xdocreport.document.docx</artifactId> <version>${xdocreport.version}</version> </dependency>
模板是放到服务器文件夹,可以直接读取,或者放到项目resource中,打包需要过滤,不然会文件损坏(pom部分代码)。
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>template/*.*</exclude> </excludes> </resource> </resources> <plugins> <!--资源文件管理--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>${maven.plugins.version}</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <nonFilteredFileExtensions> <nonFilteredFileExtension>zip</nonFilteredFileExtension> </nonFilteredFileExtensions> <overwrite>true</overwrite> <outputDirectory>${project.basedir}/target/classes/template</outputDirectory> <!--复制该profile的配置文件--> <resources> <resource> <directory>${project.basedir}/src/main/resources/template</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build>
3.需要有解析字体文件,需放到服务器的文件夹下读取,如放到resource下,无法加载字体,导致打印pdf乱码
如:宋体 windows字体文件simsun.ttc,linux字体文件simsunb.ttf 加载字体文件的代码: public class ExtFontFactoryImp extends FontFactoryImp { public ExtFontFactoryImp(){ super(); } /** 1. 注册字体(现在只有宋体) */ public int registerDirectories(){ int i = 0; /*****************注释的是默认加载的字体,windows下面是可以找到的,linux下无法加载/usr/下的字体*********************************/ /i += registerDirectory("C:/Windows/Fonts"); i += registerDirectory("c:/winnt/fonts"); i += registerDirectory("d:/windows/fonts"); i += registerDirectory("/usr/share/fonts", true); i += registerDirectory("/usr/share/fonts/zh_CN", true); i += registerDirectory("/usr/share/X11/fonts", true); i += registerDirectory("/usr/share/fonts", true); i += registerDirectory("/Library/Fonts"); i += registerDirectory("/System/Library/Fonts"); i += registerDirectory(System.getenv("LICENSE_HOME"), true);*/ /***********加载自己准备的字体文件D盘font下****************/ i += registerDirectory("D:"+File.separator+"font"); return i; } }
4、生成模板文件
1.必须是docx文件,office2013 或者office2010 都可以,先新建一个docx文件,然后更改名称.zip文件
—》
打开zip模板中word/docment.xml,可作为模板,如果只要取出所需的xml就可以做作为模板啦,
5、jar冲突、如:引入1步的jar后,不需要引入poi和poi-ooxml,如引入可能造成jar冲突而报错
6、word 文档中最好是宋体,我项目中加载的是宋体的字体文件
7、Notepad++的XML Tools插件格式化XML文件
8、使用freemaker填充xml模版(el 表达式) 如:
9、后续代码放到git
参考:
基于freemarker ,xdocreport生成word,pdf
Notepad++的XML Tools插件格式化XML文件
相关文章推荐
- JasperReport和JasperReport Studio(iReport)生成PDF中文乱码的解决方案
- linux下openoffice4的word文件转PDF中文乱码问题
- Jasperreport 生成PDF 中文乱码解决办法
- linux 安装 wkhtmltopdf 中文乱码或者空白解决方法
- 基于freemarker ,xdocreport生成word,pdf
- JasperReport和JasperReport Studio(iReport)生成PDF中文乱码的解决方案
- Linux下Brit 导出pdf,word等文件的时候echarts的X轴或者Title乱码问题
- 解决linux下openoffice word文件转PDF中文乱码的问题
- Linux下Gitbook生成pdf及中文问题的解决
- 滴水穿石--Java 生成PDF文件--iText使用之插入图片和中文乱码
- linux环境下jfreechart生成的图形中文乱码
- Linux删除中文乱码文件或者目录
- phpword生成中文乱码的问题
- Ubuntu下LaTeX生成的pdf,中文书签显示为乱码的解决方案
- PHP生成PDF完美支持中文,解决TCPDF乱码
- 一个命令解决Linux下XPDF中文乱码
- Oracle BI Publisher(BIP)生成PDF中文乱码(其他格式显示正常)
- Linux下的JFreeChart中文乱码解决(图片生成后中文显示为“口”)
- linux安装中文方块乱码现象