我在跳一个人可以帮助我......看起来我想做的事情应该相当简单,但我一直在为这件事争取一整天,而且没有想法。我在StackOverflow和Internet上发现了大量信息,但没有什么能帮助我解决这个问题。@飞碟的font-face
我正在尝试使用itext-2.0.8以及core-renderer-R8来创建带有嵌入字体的PDF。我正尝试从有效的XHTML生成PDF,并使用@ font-face样式标记嵌入字体。我已经确认@ font-face标签是通过在浏览器中打开文件来包含字体的。我总是小心地保持TTF场相对于XHTML/CSS文档。
为了尝试和通过我的方式工作,我创建了一个小型'Hello World'类型的程序来尝试嵌入字体。我采取了两种独立的方法,他们都未能产生预期的结果。我已经将这个小Eclipse程序的副本放在http://christopherluft.com/FlyingSaucer.zip
该程序在两种情况下均生成PDF,但都没有按预期嵌入PDF。使用带有setDocument的文件的第一种方法不会产生错误,也不会产生字体。第二种方法生成PDF,但在调试输出中显示了java.net.MalformedURLException。
我尝试了各种路径和URL的许多排列组合;然而,没有人不能产生期望的结果。我的怀疑是我无法理解有关ITextRenderer.setDocument的内容;然而,我很难找到任何适合我的用例的文档。
我想第一个方法是:
public static void main(String[] args) throws IOException, DocumentException {
System.getProperties().setProperty("xr.util-logging.loggingEnabled",
"true");
XRLog.setLoggingEnabled(true);
String inputFile = "sample.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
,我使用(这是更接近我们用它在我们的应用程序的实际方式)的第二个方法是:
public static void main(String[] args) throws IOException, DocumentException {
System.getProperties().setProperty("xr.util-logging.loggingEnabled", "true");
XRLog.setLoggingEnabled(true);
String inputFile = "sample.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
DocumentBuilder documentBuilder;
org.w3c.dom.Document xhtmlContent;
try
{
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
documentBuilder.setEntityResolver(new XhtmlEntityResolver(new SuppressingEntityResolver()));
xhtmlContent = documentBuilder.parse(url);
System.out.println(url);
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(xhtmlContent,".");
renderer.layout();
renderer.createPDF(os);
System.out.println("Finishing up....");
os.close();
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
而在XHTML中包含的@ font-face看起来像这样:
现在我觉得这是一个很常见的u我想,我只是没有执行一个简单的步骤,为了让这个工作...问题是,我一直在这一段时间,并认为我无法通过树木看到森林。任何人可以提供给我的帮助将不胜感激。感谢您的时间。
确保Maven不会损坏您的字体。 http://stackoverflow.com/a/35367727/2420718 – 2016-06-29 21:54:13