Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 688
Foo L Avatar asked Oct 18 '25 10:10

Foo L


1 Answers

I have managed to customize the font of my entire document passing a CSS stylesheet using user-style-sheet. Inside your CSS file, in order to avoid issues with the font structure, I recommend you to convert the font to a base64 format. base64 reference

@font-face {
    font-family: 'YourFont';
    font-style: normal;
    font-weight: 400;
    src: url(data:font/opentype;charset=utf-8;base64,d09GRgABAAAAAD00AA4A---[large string ommited]----3MAuAH/hbAEjQA=) format("woff"),
    url(data:font/truetype;charset=utf-8;base64,AAEAAAARAQAABAAQRFNJRwAAAAEAAJUIAAA---[large string ommited]-----wAAAAAAAAAAAAEAAAAA) format("truetype");
}

* {
  font-family: "YourFont" !important;
}

You can use a tool like this to transform your font to base64.

Hope it helps!

like image 67
Begoña Álvarez de la Cruz Avatar answered Oct 21 '25 02:10

Begoña Álvarez de la Cruz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!