pythonpdf-generationwkhtmltopdfpdfkitpython-pdfkit

specify font in Python pdfkit


I'm using python 3.6 with pdfkit 0.6.1 (and it seems wkhtmltopdf 0.12.3.2) on a Debian Docker image. I tried looking at the docs & wkhtmltopdf options but there's no way to specify the font for the whole document. There are only font options for footers & headers.

I tried specifying

font-family: "Times New Roman", Times, serif;

In a div wrapper in my html <style> section before html to pdf conversion, but it's not coming up "Times New Roman". Looking into the binary, it seems it's using DejaVuSerif.

Is there a way to specify the font for the document being converted?


Solution

  • Well, looks like I need to install ttf-mscorefonts-installer, but first, I need to add contrib into my sources.list

    sed -Ei 's/main$/main contrib/' /etc/apt/sources.list
    

    Then install the package

    apt-get -y install ttf-mscorefonts-installer
    

    The closest free font I was able to find was Liberation Serif in ttf-liberation, which means the html style font had to be changed to:

    font-family: "Times New Roman", Times, "Liberation Serif", serif;