pdf-generationwkhtmltopdfpython-pdfkit

pdfkit uses different fonts when renders PDF document on OS X and Ubuntu


I use pdfkit to generate PDF document from the HTML page. The problem is that it uses different fonts on my development machine (OS X) and production server (Ubuntu), so I can't get consistent rendering on development and production environment.

Take a look at these screenshots:

PDF generated on Ubuntu: PDF generated on Ubuntu

PDF generated on OS X: PDF generated on OS X

HTML version HTML version

PDF has different fonts but when I open HTML version in the browser it renders exactly the same when hosted on Ubuntu server and OS X locally and uses the same font as locally (OS X) generated PDF.

Here is the code that I use to generate HTML and PDF version (this is Django).

t = loader.get_template(self.template_name)
c = RequestContext(request, context)
html = t.render(c)

if format == "html":
    return HttpResponse(html, content_type="text/html")
elif format == "pdf":
    options = {
        'quiet': ''
    }
    pdf = pdfkit.from_string(smart_unicode(html), False, options=options)

    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = '{0}; filename=Order Sheet.pdf'\
        .format(self.get_disposition())
    return response
else:
    return HttpResponse("Unknown format", content_type="text/html")

and this is the CSS font definition

body {
    font-family: sans-serif;
    font-size: 13px;
    font-weight: normal;
}

I tried Arial as well.

Why does pdfkit (wkhtmltopdf) use different fonts on Ubuntu and how to force it to use the font I want?


Solution

  • OSX uses Helvetica as its default sans-serif font and the default for Ubuntu is DejaVu Sans. I would recommend using one that is common between environments.

    You can install Helvetica onto your Ubuntu machine and then modify font-family: "Helvetica", sans-serif;.

    Ideally I would explore a font that is accessible on both environments and then specify it within your font-family call.