HTML 使用自定义字体 (Identity-H) 到 PDF
HTML to PDF using custom font (Identity-H)
我正在使用 Flying Saucer 9.0.9(在 9.0.8 上也是如此),并且在使用 "IDENTITY-H" 编码的自定义字体时出现奇怪的行为。使用此编码时,我的 pdf 中的字符变得模糊和不合时宜。当使用 Arial Unicode MS 字体时,一切都按预期工作(但由于许可问题我不能使用它),使用任何其他字体我都没有成功。使用 FreeSans 的结果:
我是如何添加字体的:
ITextRenderer iTextRenderer = new ITextRenderer();
ITextFontResolver iTextFontResolver = iTextRenderer.getFontResolver();
iTextFontResolver.addFont("templates/FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
HTML:
<?xml version="1.0"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
* {
font-family: FreeSans;
}
</style>
</head>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<p>Test Great White Fox Jumps Over The Hedge?</p>
ĐĕĞĦķŔŶ
</body>
</html>
我是如何创建 pdf 的:
iTextRenderer.setDocument(new File(getClass().getClassLoader().getResource("templates/test.html").getFile()));
iTextRenderer.layout();
final FileOutputStream fs = new FileOutputStream("C://temp//temp.pdf");
iTextRenderer.createPDF(fs);
当使用不同的编码(例如:WINANSI)时,第一行文本被正确呈现,但显然第二行不是。我真的需要能够创建 UNICODE (IDENTITY-H) PDF。
您可以使用下面的 link 下载结果 pdf:
download pdf from dropbox
非常感谢任何帮助。
更新:
显然这与 iText 或飞碟无关。我们的 Maven 构建构建了一个 fat jar,字体也包含在这个 jar 中。但是在从文件系统复制到 jar 的过程中,字体被损坏了。
显然这与 iText 或 Flying Saucer 无关。我们的 Maven 构建构建了一个 fat jar,字体也包含在这个 jar 中。但是在从文件系统复制到 jar 的过程中,字体被损坏了。从过滤中排除字体解决了问题。
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>templates/font/*.ttf</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>templates/font/*.ttf</include>
</includes>
</resource>
</resources>
我正在使用 Flying Saucer 9.0.9(在 9.0.8 上也是如此),并且在使用 "IDENTITY-H" 编码的自定义字体时出现奇怪的行为。使用此编码时,我的 pdf 中的字符变得模糊和不合时宜。当使用 Arial Unicode MS 字体时,一切都按预期工作(但由于许可问题我不能使用它),使用任何其他字体我都没有成功。使用 FreeSans 的结果:
我是如何添加字体的:
ITextRenderer iTextRenderer = new ITextRenderer();
ITextFontResolver iTextFontResolver = iTextRenderer.getFontResolver();
iTextFontResolver.addFont("templates/FreeSans.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
HTML:
<?xml version="1.0"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
* {
font-family: FreeSans;
}
</style>
</head>
<body>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<p>Test Great White Fox Jumps Over The Hedge?</p>
ĐĕĞĦķŔŶ
</body>
</html>
我是如何创建 pdf 的:
iTextRenderer.setDocument(new File(getClass().getClassLoader().getResource("templates/test.html").getFile()));
iTextRenderer.layout();
final FileOutputStream fs = new FileOutputStream("C://temp//temp.pdf");
iTextRenderer.createPDF(fs);
当使用不同的编码(例如:WINANSI)时,第一行文本被正确呈现,但显然第二行不是。我真的需要能够创建 UNICODE (IDENTITY-H) PDF。
您可以使用下面的 link 下载结果 pdf: download pdf from dropbox
非常感谢任何帮助。
更新: 显然这与 iText 或飞碟无关。我们的 Maven 构建构建了一个 fat jar,字体也包含在这个 jar 中。但是在从文件系统复制到 jar 的过程中,字体被损坏了。
显然这与 iText 或 Flying Saucer 无关。我们的 Maven 构建构建了一个 fat jar,字体也包含在这个 jar 中。但是在从文件系统复制到 jar 的过程中,字体被损坏了。从过滤中排除字体解决了问题。
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>templates/font/*.ttf</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>templates/font/*.ttf</include>
</includes>
</resource>
</resources>