tek*_*unt 6 css itext font-face flying-saucer
我正在跳跃,有人可以帮助我...似乎我想要做的事情应该相当简单,但我现在已经打了一天超过一天的想法.我已经在StackOverflow和Internet上发现了大量信息,但没有任何帮助我解决这个问题.
我正在尝试使用itext-2.0.8和core-renderer-R8来创建带有嵌入字体的PDF.我试图从有效的XHTML生成PDF并使用@ font-face样式标记嵌入字体.我已经确认@ font-face标签通过在浏览器中打开文件来包含字体.我总是小心翼翼地保持TTF领域相对于XHTML/CSS doc.
为了尝试和完成我的工作,我创建了一个小的"Hello World"类型程序来尝试嵌入字体.我采取了两种不同的方法,但都无法产生预期的结果.我在http://christopherluft.com/FlyingSaucer.zip上放了一个这个小Eclipse程序的副本
该程序在两个实例中都生成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();
}
Run Code Online (Sandbox Code Playgroud)
我使用的第二种方法(更接近我们在应用程序中使用它的实际方式)是:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
XHTML中的@ font-face包含如下所示:
@font-face {
font-family: 'MisoRegular';
src: url("miso-regular-webfont.ttf");
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}
Run Code Online (Sandbox Code Playgroud)
现在我觉得这是一个非常常见的用例,我想我只是没有执行一个简单的步骤才能让它工作......问题是我已经在这一段时间并认为我是无法通过树木看到森林.任何人都可以提供给我的任何帮助将不胜感激.感谢您的时间.
我在使用 Flying Saucer 时遇到过问题,字体似乎没有正确嵌入,最终导致我尝试嵌入的字体已损坏或至少不完整。缺少有关字体系列和描述的属性。当我尝试使用 OS XI 上 FontBook 中的“验证文件”选项验证我的字体时,立即出现警告:
然而,FlyingSaucer 默默地失败了,只是继续嵌入 Times-Roman 和 Times-Bold 作为默认字体。当我尝试使用通过 FontBook 验证的字体时,我发现该字体已正确嵌入。
我花了几个小时试图增加 FlyingSaucer 日志记录的详细程度以获取更多信息,直到我逐步完成整个渲染过程并恰好注意到加载后 FontFamily 在调试器中设置为“”时才发现情况如此字体,并且它不包含我期望它注册的名称。
底线: 如果您遇到 FlyingSaucer 和字体问题并且正在用头撞墙,请确保您的字体有效。