I'm using org.xhtmlrenderer.pdf.ITextRenderer
to convert my (x)html page to pdf using Java.
I've got most of it working, except the font part.
I'm using verdana
in my page and the pdf is rendered using default font.
I have added the verdana.ttf to my jar and use the following code:
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();
What am I missing here?
Thanks, Bart
For xhtmlrenderer
to work, the CSS must read:
font-family: Verdana;
instead of
font-family:verdana;
It's case-sensitive.