我正在使用org.xhtmlrenderer.pdf.ITextRenderer将我的(x)html页面转换为使用Java的pdf。
我已经完成了大部分的工作,除了字体部分。
我在我的页面中使用了verdana,而pdf是使用默认字体呈现的。
我已经将verdana.ttf添加到我的jar中,并使用以下代码:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new StringBufferInputStream(html));
File tmpFontFile = new File(TEMP_FOLDER + "/verdana.ttf");
if(!tmpFontFile.exists())
{
tmpFontFile.createNewFile();
InputStream fontIs = getClass().getResourceAsStream("/com/mycompany/util/font/verdana.ttf");
OutputStream fontOs = new FileOutputStream(tmpFontFile);
byte buf[] = new byte[1024];
int len;
while((len = fontIs.read(buf)) > 0)
fontOs.write(buf,0,len);
fontOs.close();
fontIs.close();
}
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont(
tmpFontFile.getAbsolutePath(), BaseFont.IDENTITY_H ,BaseFont.EMBEDDED);
renderer.setDocument(doc, null);
String outputFile = TEMP_FOLDER + "/mypdf.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.close();这里我漏掉了什么?
谢谢,巴特
发布于 2010-05-07 19:41:23
要使xhtmlrenderer正常工作,CSS必须为:
font-family: Verdana;
而不是
font-family:verdana;
它区分大小写。
https://stackoverflow.com/questions/2771623
复制相似问题