最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

使用Apache FOP时无法使用波兰语特殊字母

SEO心得admin36浏览0评论
本文介绍了使用Apache FOP时无法使用波兰语特殊字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在努力使用FOP生成PDF,并使用波兰语字母。我一直在这里阅读很多帖子,但到目前为止还没有成功!

I've been struggling a lot with generating a PDF using FOP, with Polish letters. I have been reading a lot of posts here on SO but so far no success!

这是我的模板:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> <xsl:template match="root"> <fo:root xmlns:fo="www.w3/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simpleA4"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="11pt" font-family='Arial'> <xsl:value-of select="TestString"/> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> </xsl:stylesheet>

我的XML:

<?xml version="1.0" encoding="utf-8"?> <root> <TestString>Chcemy mieć pewność, że nasze produkty będą miały długą żywotność i stosujemy opakowania, aby uchronić je przed uszkodzeniem podczas przenoszenia i transportu.</TestString> </root>

和我的Java代码调用它:

and my Java code calling it:

private void PolishTest() throws IOException, FOPException, TransformerException { // the XSL FO file File xsltFile = new File("C:\\Temp\\PolishTest.xsl"); // the XML file which provides the input StreamSource xmlSource = new StreamSource(new File("C:\\Temp\\PolishTest.xml")); // create an instance of fop factory FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); // a user agent is needed for transformation FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // Setup output OutputStream out = new FileOutputStream("C:\\Temp\\PolishTest.pdf"); try { // Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); // Setup XSLT TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing // That's where the XML is first transformed to XSL-FO and then // PDF is created transformer.transform(xmlSource, res); } finally { out.close(); } }

在我的模板中看到我使用Arial作为字体系列,如果我在单词中输入文字,使用Arial它可以很好地工作。但是当从Java调用它时

as seen in my template I'm using Arial as font family, and if I type the text in word, using Arial it works out very fine. But when calling it from Java

我收到很多警告:

jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Font "Arial,normal,400" not found. Substituting with "any,normal,400". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Glyph "ć" (0x107, cacute) not available in font "Times-Roman". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Glyph "ś" (0x15b, sacute) not available in font "Times-Roman". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Glyph "ż" (0x17c, zdot) not available in font "Times-Roman". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Glyph "ę" (0x119, eogonek) not available in font "Times-Roman". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Glyph "ą" (0x105, aogonek) not available in font "Times-Roman". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent WARNING: Glyph "ł" (0x142, lslash) not available in font "Times-Roman". jul. 10, 2018 8:16:57 AM org.apache.fop.events.LoggingEventListener processEvent

所以我的问题是(因为)我做错了什么?

So my question is (ofcause) what am I doing wrong?

FOP如何选择Times-Roman,即使我已经指定了Arial?更重要的是如何解决它?

How come FOP chooses "Times-Roman", even though I've specified Arial? And more important how to fix it?

**更新**

使用am9417答案的代码我已将我的Java代码更新为以下:

With the code from am9417's answer I've updated my Java code to the following:

private static String config = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" + "<fop>\n" + " <renderers>\n" + " <renderer mime=\"application/pdf\">\n" + " <fonts>\n" + " <auto-detect/>\n" + " </fonts>\n" + " </renderer>\n" + " </renderers>\n" + "</fop>"; private void PolishTest() throws IOException, SAXException, TransformerException { // the XSL FO file File xsltFile = new File("C:\\Temp\\PolishTest.xsl"); // the XML file which provides the input StreamSource xmlSource = new StreamSource(new File("C:\\Temp\\PolishTest.xml")); // create an instance of fop factory URI uri = new File(".").toURI(); InputStream configSource = new ByteArrayInputStream(config.getBytes()); FopFactory fopFactory = FopFactory.newInstance(uri, configSource); // a user agent is needed for transformation FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); // Setup output OutputStream out = new FileOutputStream("C:\\Temp\\PolishTest.pdf"); try { // Construct fop with desired output format Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); // Setup XSLT TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); // Resulting SAX events (the generated FO) must be piped through to FOP Result res = new SAXResult(fop.getDefaultHandler()); // Start XSLT transformation and FOP processing // That's where the XML is first transformed to XSL-FO and then // PDF is created transformer.transform(xmlSource, res); } finally { out.close(); } }

推荐答案

此答案可能包含一些有用的信息。所有错误都是由于找不到Arial字体引起的,这就是为什么它切换到Times-Roman,因为无法找到特殊字符(由于某种原因)。

This answer might contain some useful information. All the errors are caused because Arial font is not found, that's why it switches to Times-Roman where special characters (for some reason) cannot be found.

所以,如同在该答案中,为FOP添加具有以下内容的配置xml以引用系统中的Arial字体:

So, as in that answer, add the config xml for the FOP with the following content to refer to the Arial font in your system:

C:\ Temp \ fopconf.xml

<?xml version="1.0" encoding="utf-8" ?> <fop> <renderers> <renderer mime="application/pdf"> <fonts> <font kerning="yes" embed-url="file:///C:/windows/fonts/arial.ttf"> <font-triplet name="Arial" style="normal" weight="normal"/> </font> </fonts> </renderer> </renderers> </fop>

使用文件 object:

Add the reference to that config file in the FopFactory constructor with a File object:

// create an instance of fop factory FopFactory fopFactory = FopFactory.newInstance(new File("C:\\Temp\\fopconf.xml"));

现在文件应该呈现并且字体应该是正确的:

Now the file should render and the font should be correct:

C:\ Temp \ PolishTest.pdf

编辑: 进一步测试后,我很幸运< auto-detect /> ; 标签。但是,在尝试搜索字体时,渲染需要花费更多时间。如果您不知道字体文件位置,这可能很有用。您可能想尝试一下:

After testing this further, I had luck with <auto-detect /> tag. However, rendering took a bit more time when it tried to search the fonts. This might be useful if you don't know the font file location. You might want to give it a try:

... <fonts> <auto-detect/> </fonts> ...
发布评论

评论列表(0)

  1. 暂无评论