gpt4 book ai didi

java - 如何在 Apache FOP 中从 Java 代码配置字体?

转载 作者:行者123 更新时间:2023-12-02 13:55:00 25 4
gpt4 key购买 nike

link说从 java 代码配置字体不是“容易”的。我该如何实现这一目标?我在使用法语和日语等国际语言呈现某些 HTML 时遇到问题。

WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
May 08, 2015 4:45:39 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".

生成的 PDF 结果被损坏。

更新:

我的 Html 包含法语单词,例如“Modifié Créée le Propriétaire”

File file = new File("C:\\Users\\me\\Desktop\\Test.html");


fopFactory = FopFactory.newInstance();
foUserAgent = fopFactory.newFOUserAgent();


String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1,file.getAbsolutePath().lastIndexOf("."));
String workspacePath = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\"));
File xsltfile = new File("xhtml2fo.xsl");
StreamSource source = null;
source = new StreamSource(file);
StreamSource transformSource = new StreamSource(xsltfile);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();


Transformer xslfoTransformer = null;
TransformerFactory fac = TransformerFactory.newInstance();
xslfoTransformer = fac.newTransformer(transformSource);
xslfoTransformer.setErrorListener(this);

Fop fop;
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
// Resulting SAX events (the generated FO)
Result res = new SAXResult(fop.getDefaultHandler());
xslfoTransformer.transform(source, res);

output = new File(workspacePath + File.separator + fileName + ".pdf");
OutputStream out = new java.io.FileOutputStream(output);
out = new java.io.BufferedOutputStream(out);
FileOutputStream str = new FileOutputStream(output);
str.write(outStream.toByteArray());
str.close();

我正在使用 Antennahouse 提供的 XSLT 将 HTML 标记转换为 FO 标记。

最佳答案

示例代码。

/** The Constant FOP_CONFIG. */
private static final String FOP_CONFIG = "pdf.fop.cfg.xml";

fopFactory = FopFactory.newInstance();
// for image base URL
String serverPath = request.getSession().getServletContext().getRealPath("/");
//disable strict validatetion
fopFactory.setStrictValidation(false);
fopFactory.setBaseURL(serverPath);
// for fonts base URL
fopFactory.getFontManager().setFontBaseURL(serverPath);
// read custom font setting
StreamSource configSrc = new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(PDFComponentFactory.FOP_CONFIG));
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
Configuration cfg = cfgBuilder.build(configSrc.getInputStream());
fopFactory.setUserConfig(cfg);

希望对您有帮助。

关于java - 如何在 Apache FOP 中从 Java 代码配置字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com