描述:
我遇到了一个问题,一个中文字符(胸)在 html 中正常显示,带有“SourceHanSansSC-Medium.otf”字体,但打印在一个非常奇怪的位置,而 html 是由 pdf 反应器服务打印的。
如果我用 SourceHanSansSC-Norml.otf 替换它,那么 html 预览和 pdf-reacor 生成的 pdf 都运行良好,只有 SourceHanSansSC-Medium.otf 使用会导致这个问题。
我的环境:
- 系统:Mac os 10.12.6,Java 8 Pdf reactor 版本:10.0。
准备:
我从 docker hub 中提取了 pdf 反应器图像并将其作为本地 docker 容器运行,我的应用程序可以通过http://localhost:9423/service/rest访问它。
我写了一个非常简单的 html,其中包含SourceHanSansSC-Medium.otf和SourceHanSansSC-Medium.otf中的错误字符,只是为了比较两种字体的结果。它们都在 html 预览中正确显示,并且只有中等字体字符会打印在不正确的位置。
我将我的 html 本地父路径映射到 pdf-reactor /ro/config 以确保 pdf-reactor 能够获取要打印的 html。
HTML 代码:
这是我的 html 代码“print_sc_font.html”(我在 zip 中附加了 html 字体):
<html>
<head>
<style type="text/css">
@font-face {
font-family: shssc-normal;
src: url("./SourceHanSansSC-Normal.otf");
}
@font-face {
font-family: shssc-medium;
src: url("./SourceHanSansSC-Medium.otf");
}
</style>
</head>
<body>
<div style="font-family: shssc-normal;">Print by SC Normal Font: 肺癌</div>
<div style="font-family: shssc-medium;">Print by SC Medium Font: 肺癌</div>
</body>
</html>
html预览没问题
Java 打印代码 (PdfReactorTest.java):
package com.gc.dev;
import com.realobjects.pdfreactor.webservice.client.Configuration;
import com.realobjects.pdfreactor.webservice.client.PDFreactor;
import com.realobjects.pdfreactor.webservice.client.PDFreactorWebserviceException;
import com.realobjects.pdfreactor.webservice.client.Result;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class PDFReactorTest {
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String timeStamp = dateFormat.format(date);
// Create new PDFreactor instance
PDFreactor pdfReactor = new PDFreactor("http://localhost:9423/service/rest");
// Create a new configuration object
Configuration config = new Configuration()
// Specify the input document for Mac systems (adapt path if necessary) .setDocument("file:///ro/config/html/test/print_sc_font.html")
// Enable javaScriptSettings
.setJavaScriptMode(Configuration.JavaScriptMode.ENABLED)
// Set an appropriate log level
.setLogLevel(Configuration.LogLevel.DEBUG)
// Sets the title of the created PDF
.setTitle("Demonstration of PDFreactor Java API")
// Sets the author of the created PDF
.setAuthor("Myself")
// Enables links in the PDF document.
.setAddLinks(true)
// Enable bookmarks in the PDF document
.setAddBookmarks(true)
// Set some viewer preferences
.setViewerPreferences(
Configuration.ViewerPreferences.FIT_WINDOW,
Configuration.ViewerPreferences.PAGE_MODE_USE_THUMBS)
// Add user style sheets
.setUserStyleSheets(
new Configuration.Resource().setContent("@page {" +
"@top-center {" +
"content: 'PDFreactor Java API demonstration';" +
"}" +
" @bottom-center {" +
"content: \"Created on " + timeStamp + "\";" +
"}" +
"}"),
new Configuration.Resource().setUri("common.css"));
FileOutputStream fos = null;
try {
// Render document and save result to result
Result result = pdfReactor.convert(config);
if (result != null) {
byte[] pdf = result.getDocument();
//Save the pdf at the desired location
fos = new FileOutputStream("result.pdf");
fos.write(pdf);
fos.close();
}
} catch (PDFreactorWebserviceException exception) {
Result result = exception.getResult();
System.err.println(result.getError());
} catch (Exception e) {
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}
}
结果.pdf:
我附上了我的代码和屏幕快照。
SourceHanSansSC-Normal.otf 太大而无法附加,因此可以从https://github.com/adobe-fonts/source-han-sans/tree/release下载两个字体文件 SourceHanSansSC-Normal 和 SourceHanSansSC-Medium.otf /OTF/简体中文,

