Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

helvetica font not working in wkhtmltopdf

I have been trying to use Helvetica font while creating the pdf but the font is not reflected back in pdf.

I did some google and found some solutions but none are workable.

Tried Solution 1

I found similar thing here : Font issue

Tried the last solution mentioned over there

workaround is to define @font-face in your css and have src link to external host that is accessible by wkhtmltopdf

But the solution is not working.

Tried Solution 2 I also tried to use google font api, but result is still not workable. helvetica font

here is an example Example of custom font

Tried Solution 3 I tried doing it using css property @page. But that also is not working.

What is the approach for a workable solution.

like image 299
Padmalochan Avatar asked Apr 01 '13 15:04

Padmalochan


3 Answers

The easiest way to fix wkhtmltopdf's font problems is to Base64 encode the font (you can use this tool) and include it in your CSS:

@font-face {
    font-family: 'Helvetica';
    src: url(data:font/truetype;charset=utf-8;base64,AAEAAAATAQA...
}

This works with all fonts (including Google Fonts), and guarantees cross-platform compatibility across different machines and operating systems.

like image 200
Arman H Avatar answered Nov 07 '22 01:11

Arman H


For me loading fonts from Google Fonts didn't work. And putting base64ed binary into a CSS file seems a little to much for me(Korean types are several megabytes). I'd recommend to install the fonts you need to use on the machine. For Ubuntu you can simply download fonts files from Google Fonts and copy the files in to $HOME/.fonts directory and run fc-cache command in command line to rebuild the fonts list.

$ mkdir ~/.fonts
$ copy your-font-file.ttf ~/.fonts/
$ fc-cache -fv

For a Rails application you can symlink

$ ln -s /your/app/root/assets/fonts ~/.fonts

Then you will be able to list all available fonts.

$ fc-list
Nimbus Sans L:style=Regular Italic
URW Palladio L:style=Roman
Century Schoolbook L:style=Bold Italic
Nimbus Sans L:style=Bold
URW Chancery L:style=Medium Italic
Nimbus Roman No9 L:style=Regular
Century Schoolbook L:style=Bold
Century Schoolbook L:style=Italic
Nimbus Sans L:style=Regular
....
like image 42
baxang Avatar answered Nov 07 '22 01:11

baxang


To add to the fray, using wkhtmltopdf 0.12.1 (with patched qt) on linux this worked for me:

@font-face {
    font-family: dejaSansMono;
    src: url('file:///usr/share/fonts/dejavu/DejaVuSansMono.ttf');
} 

I.e., specifying the path to the .ttf. Simply naming a font-family, any font-family, did not work even if it did in the browser.

like image 3
CodeClown42 Avatar answered Nov 07 '22 03:11

CodeClown42