0

I want to convert a HTML file to PDF

Example Input

The HTML file from Google Drive looks good, can see content in my browser.

Code

import pdfkit # convert html to pdf

pdfkit.from_file('income_contract_01.html', 'res.pdf')

Issue

I expect to see data in res.pdf. But instead I see an empty file:

  • no text
  • but the same number of lines

Environment and versions used:

python 3.9
pdfkit versiion 1.0.0
wkhtmltopdf is the newest version (0.12.2.4-1)
OS Ubuntu 16.04

How can I fix the error? Don't see any error messages.

Update: I was trying to specify library in configuration but it didn't help

import pdfkit # convert html to pdf v1

path = "/usr/bin/wkhtmltopdf"
config = pdfkit.configuration(wkhtmltopdf=path)
pdfkit.from_file('income_contract_01.html', 'res123.pdf', configuration=config)
hc_dev
  • 8,389
  • 1
  • 26
  • 38
mascai
  • 1,373
  • 1
  • 9
  • 30

1 Answers1

0

I solved it with using wkhtmltopdf.

Once you download it from here then put wkhtmltopdf.exe's path to variable in code below which is path_wkhtmltopdf.

import pdfkit

path_wkhtmltopdf = r"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
pdfkit.from_url("income_contract.html", "res.pdf", configuration=config)

Result: res.pdf

  • Thank you, but this didn't help me (result is the same - empty file) – mascai Jul 10 '22 at 13:17
  • What is the result of your code with my file (link above)? – mascai Jul 10 '22 at 13:17
  • I added result link to answer. – whitehead421 Jul 10 '22 at 13:22
  • Downvoted, because OP didn't ask for Windows solution, rather has Linux/Ubuntu mentioned as OS. Please hold your answer generic and add OS-specific configurations as separate section to you answer. – hc_dev Jul 10 '22 at 13:32
  • Anyway, he/she tried to adding path. I just wanted to point out that he may use path. Another point of view :/ – whitehead421 Jul 10 '22 at 13:36
  • pdfkit is a wrapper for wkhtmltopdf. Can you try this `wkhtmltopdf --enable-local-file-access income_contract_01.html res123.pdf` – yoonghm Jul 11 '22 at 03:48
  • This should solve your issue. https://stackoverflow.com/questions/62814607/pdfkit-warning-blocked-access-to-file – yoonghm Jul 11 '22 at 03:57