Nandan Bhat
Nandan Bhat

Reputation: 1563

Boxes are displayed instead of text in pdfkit - Python3

I am trying to convert an HTML file to pdf using pdfkit python library. I followed the documentation from here.

Currently, I am trying to convert plain texts to PDF instead of whole html document. Everything is working fine but instead of text, I am seeing boxes in the generated PDF. This is my code.

import pdfkit
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf/wkhtmltox/bin/wkhtmltopdf')
content = 'This is a paragraph which I am trying to convert to pdf.'
pdfkit.from_string(content,'test.pdf',configuration=config)

This is the output.

Boxes are being displayed instead of plain text.

Instead of the text 'This is a paragraph which I am trying to convert to pdf.', converted PDF contains boxes.

Any help is appreciated. Thank you :)

Upvotes: 4

Views: 2312

Answers (2)

parth_sh
parth_sh

Reputation: 59

This issue is referred here Include custom fonts in AWS Lambda

if you are using pdfkit on lambda you will have to setup ENV variables as "FONT_CONFIG_PATH": '/opt/fonts/' "FONTCONFIG_FILE": '/opt/fonts/fonts.conf'

if this problem is in the local environment a fresh installation of wkhtmltopdf must resolve this

Upvotes: 1

Shubham Mishra
Shubham Mishra

Reputation: 351

Unable to reproduce the issue with Python 2.7 on Ubuntu 16.04 and it works fine on the specs mentioned. From my understanding this problem is from your Operating System not having the font or encoding in which the file is being generated by the pdfkit.

Maybe try doing this:

import pdfkit
config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf/wkhtmltox/bin/wkhtmltopdf')
content = 'This is a paragraph which I am trying to convert to pdf.'
options = {
    'encoding':'utf-8',
}
pdfkit.from_string(content,'test.pdf',configuration=config, options=options)

The options to modify pdf can be added as dictionary and assigned to options argument in from_string functions. The list of options can be found here.

Upvotes: 1

Related Questions