我在跳一個人可以幫助我......看起來我想做的事情應該相當簡單,但我一直在爲這件事爭取一整天,而且沒有想法。我在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