James Franco James Franco - 1 month ago 15
Python Question

pdfkit - python : 'str' object has no attribute decode

I am attempting to use pdfkit to convert string html to a pdf file. This is what I am doing

try:
options = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
}
config = pdfkit.configuration(wkhtmltopdf="/usr/local/bin/wkhtmltopdf")

str= "Hello!!"
pdfkit.from_string(str,"Somefile.pdf",options=options,configuration=config)
return HttpResponse("Works")

except Exception as e:
return HttpResponse(str(e))


however at
from_string
I get the exception 'str' object has no attribute decode. Any suggestions on how I can fix this ? I am using Python 3.5.1

Answer

Try replacing the config line with this - path to the binary is provided as a bytes object:

config = pdfkit.configuration(wkhtmltopdf=bytes("/usr/local/bin/wkhtmltopdf", 'utf8'))

Reference: https://github.com/JazzCore/python-pdfkit/issues/32