Skip to main content

xhtmlrenderer xhtml to pdf font problem

1 reply [Last post]
thierensb
Offline
Joined: 2010-05-07
Points: 0

Hi all,

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 html-page butthe pdf is rendered using default font.

I have added the verdana.ttf to my jar and use the following code:

//instantiate docbuilder
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//parse html document
Document doc = builder.parse(new StringBufferInputStream(html));

//create font-file if it doesn't exit because getting path from jarred file is impossible
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();
}

//get renderer
ITextRenderer renderer = new ITextRenderer();
//add my font
renderer.getFontResolver().addFont(
tmpFontFile.getAbsolutePath(), BaseFont.IDENTITY_H ,BaseFont.EMBEDDED);
//set document
renderer.setDocument(doc, null);

//write output to pdf
String outputFile = TEMP_FOLDER + "/mypdf.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.close();
//end

What am I missing here?

Thanks, Bart

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
thierensb
Offline
Joined: 2010-05-07
Points: 0

I resolved this myself.

The error was not in the Java code, but in the CSS stylesheet.
Aparrently, it is case-sensitive!

I had: font:family:verdana;

It had to be: font-family:Verdana;