我正在使用FOP以编程方式创建PDF。
但是我不能更改默认字体...我想使用特定的字体。 因此,我下载了该字体的ttf文件,但无法使文本以该字体显示。
这是我的Java类,它加载conf文件并使用xsl文件。
public File createPDFFile(ByteArrayOutputStream xmlSource) throws IOException, SAXException {
File file = File.createTempFile("" + System.currentTimeMillis(), EXTENSION);
URL url = new File(this.getClass().getResource("/UserId.xsl").getFile()).toURI().toURL();
// creation of transform source
StreamSource transformSource = new StreamSource(url.openStream());
// create an instance of fop factory
FopFactory fopFactory = FopFactory.newInstance(new File(this.getClass().getResource("/conf.xconf").getFile()));
// a user agent is needed for transformation
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// to store output
ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();
StreamSource source = new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray()));
Transformer xslfoTransformer;
try {
TransformerFactory transfact = TransformerFactory.newInstance();
xslfoTransformer = transfact.newTransformer(transformSource);
// Construct fop with desired output format
Fop fop;
try {
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfoutStream);
// Resulting SAX events (the generated FO)
// must be piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
try {
// everything will happen here..
xslfoTransformer.transform(source, res);
// if you want to save PDF file use the following code
OutputStream out = new java.io.FileOutputStream(file);
out = new java.io.BufferedOutputStream(out);
FileOutputStream str = new FileOutputStream(file);
str.write(pdfoutStream.toByteArray());
str.close();
out.close();
} catch (TransformerException e) {
e.printStackTrace();
return null;
}
} catch (FOPException e) {
e.printStackTrace();
return null;
}
} catch (TransformerConfigurationException e) {
e.printStackTrace();
return null;
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
return null;
}
return file;
}
然后在我的配置中:
....
<fonts>
<font embed-url="ChaletNewYorkNineteenEighty.ttf">
<font-triplet name="Chalet" style="normal" weight="normal"/>
<font-triplet name="Chalet" style="normal" weight="bold"/>
</font>
....
在我的XLS中:
<fo:block border="solid 0.1mm black" font-family="Chalet" font-size="8pt" font-weight="normal">
但是在输出中始终使用标准字体,在控制台中,我得到以下信息:
WARN org.apache.fop.apps.FOUserAgent - Font "Chalet,normal,700" not found. Substituting with "any,normal,700".
答案 0 :(得分:0)
您的embed-url
似乎不正确。
相对URL相对于font-base属性(或基础)进行解析(如果有) https://xmlgraphics.apache.org/fop/2.1/fonts.html#register
答案 1 :(得分:0)
Chalet
字体未在conf.xconf文件中声明为字体样式style="normal"
和字体粗细weight="700"
。但是您尝试在XLS模板中将Chalet
和font-weight="700"
的字体与style="normal"
和Chalet
一起使用。因此,出现此错误
WARN org.apache.fop.apps.FOUserAgent-找不到字体“ Chalet,normal,700”。替换为“ any,normal,700”
使用style="normal"
和weight="700"
配置<fonts>
<font embed-url="ChaletNewYorkNineteenEighty.ttf">
<font-triplet name="Chalet" style="normal" weight="normal"/>
<font-triplet name="Chalet" style="normal" weight="700"/>
<font-triplet name="Chalet" style="normal" weight="bold"/>
</font>
.........
</fonts>
字体
{{1}}