Showing posts with label wkhtmltopdf pdf conversion html2pdf. Show all posts
Showing posts with label wkhtmltopdf pdf conversion html2pdf. Show all posts

Tuesday, October 5, 2010

Font problems with wkhtmltopdf

Recently I ran into problems with font kerning and font sizes when creating PDFs with wkhtmltopdf on different machines. I create PDFs on my ubuntu machine locally and on a FreeBSD server remotely.

Unfortunately font sizes differed a lot and from the beginning the kerning wasn't as I expected. The space after characters like 'v' is too small or too big, depending on the chosen font size. Sometimes it happens that two characters get rendered  one on the other. This is not acceptable.

I found out that this topic is already discussed in the wkhtml bugtracker. Users have different approaches to that problem, but none of the solutions is acceptable for me. Running an XServer is not possible and running xvfb as a wrapper to the tool sounds a lot slower than using wkhtmltopdf 'natively' and certainly is!

After experimenting with CSS modifications concerning font sizes, letter spacing, --zoom and --disable-smart-shrinking / --enable-smart-shrinking parameters to wkhtmltopdf I finally found a working solution. It's very easy to apply and has no disadvantages.

I need polish characters in my texts, so I downloaded high quality fonts from TeX Gyre Project. They provide their fonts in ODF format instead of the commonly used TTF format. I don't know, if the fonts of the Gyre Project solve the problem or if it is the newer ODF format that eliminates certain limitations of the TTF format when used on different operating systems.

In your external or internal stylesheet of your HTML file you'd like to convert, define a custom font face like this:


Don't forget to use the correct path to your font. Whenever you want to use it, use your new defined font family:


This eliminates all problems arising due to different font versions on different systems and the kerning bug in wkhtmltopdf version 0.10.0_beta5