Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pdfkit- Warning: Blocked access to file

Tags:

python

pdfkit

I am getting an error(Blocked access to the file) in HTML to pdf conversion using pdfkit library while using a local image in my HTML file. How can I use local images in my HTML file?

like image 970
sanjay Avatar asked Nov 27 '22 19:11

sanjay


2 Answers

I faced the same problem. I solved it by adding "enable-local-file-access" option to pdfkit.from_file().

options = {
  "enable-local-file-access": None
}

pdfkit.from_file(html_file_name, pdf_file_name, options=options)
like image 149
Susumu Ishihara Avatar answered Dec 19 '22 02:12

Susumu Ishihara


Pdfkit is a python wrapper for wkhtmltopdf. It seems to have inherited the default behaviour of wkhtmltopdf in recent versions, which now blocks local file access unless otherwise specified.

However, since pdfkit allows you to specify any of the original wkhtmltopdf options, you should be able to resolve this problem by passing the enable-local-file-access option.

Following the example on the pdfkit site, that would probably look something like this:

options = {
    "enable-local-file-access": ""
}

pdfkit.from_string(html, output_path=False, options=options)
like image 25
kimbespo Avatar answered Dec 19 '22 03:12

kimbespo