您的位置:首页 > 运维架构 > Linux

linux 下xdocreport 生成word 和pdf 乱码(中文是空,或者方块)

2017-12-03 13:20 711 查看
最近项目需要pdf打印,通过调研和网上搜索xdocreport,处理图片和word,pdf无论是生成速度和内存消耗,比doc4j都要速度快,内存消耗小。开发一直在window下面,生成pdf正常,中文不会乱码,部署到linux服务器就各种问题。

解决方法:

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文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  乱码 linux word pdf