wkhtmltopdf, 0.12.6, Warning: Blocked access to file
Asked Answered
S

8

93

When upgrade wkhtmltopdf to 0.12.6, it came to such messages and the image did not show in the target pdf:

    Warning: Blocked access to file /path/to/bpa_product_layering.png

BTW, the same source html file works well with 0.12.5

Spinks answered 11/6, 2020 at 0:31 Comment(0)
S
155

This is caused by the change of default behavior in version 0.12.6 of wkhtmltopdf. wkhtmltopdf disables local file access by default now. It could be solved by adding the command line parameter

--enable-local-file-access

or the combination

--disable-local-file-access --allow <path>
Spinks answered 11/6, 2020 at 0:31 Comment(7)
please add an example..how can i add command line parameter? i am using phthonBisson
@sanjay, I ran into the same issue with Python, you can pass in options in the form of a dictionary. In this case, you could use the option "disable-local-file-access":"".Luik
when you get this, when using pandoc then use pandoc --pdf-engine-opt=--enable-local-file-accessPeewit
i have same problem in symfphony any idea how to finx it ? snapy pdf libraryBryannabryansk
Just using --allow works for me on version 0.12.6.Colman
This is almost right, but insufficient/incorrect - c.f. @Shrimp's answer below for the extra correction needed.Aracelyaraceous
Can we allow multiple paths ? or with a regex ? or with something like */my/very/last/folder ?Lucillalucille
P
42

For those that are using laravel-snappy, add the 'enable-local-file-access' option in the config\snappy.php:

'pdf' => [
        'enabled' => true,
        'binary'  => env('WKHTML_PDF_BINARY', '/usr/local/bin/wkhtmltopdf'),
        'timeout' => false,
        'options' => [
            'enable-local-file-access' => true,
            'orientation'   => 'landscape',
            'encoding'      => 'UTF-8'
        ],
        'env'     => [],
    ],

    'image' => [
        'enabled' => true,
        'binary'  => env('WKHTML_IMG_BINARY', '/usr/local/bin/wkhtmltoimage'),
        'timeout' => false,
        'options' => [
            'enable-local-file-access' => true,
            'orientation'   => 'landscape',
            'encoding'      => 'UTF-8'
        ],
        'env'     => [],
    ],

wkhtmltopdf disables local file access by default in the 0.12.6 version

Permalloy answered 8/12, 2020 at 3:56 Comment(0)
T
28

Just bumping this thread with a correction in case you're still getting the same error in spite of using:

--enable-local-file-access

For some reason, this cmd line argument does not work when being specified after input/output files, you have to write this argument right after wkhtmltopdf.exe.

So

wkhtmltopdf.exe --enable-local-file-access input.html output.pdf

instead of other variants.

Tricuspid answered 29/9, 2020 at 9:4 Comment(1)
Yes, the sequence matters. In 0.12.4, it was fine...Bluepoint
G
4

in my case, I put "enable-local-file-access": "", in options, it worked.

Gamble answered 20/9, 2020 at 7:1 Comment(0)
P
4

In Windows with Python, I came across a similar error as well when running code:

result = imgkit.from_file('postlayout.A.html', 'out.jpg', config=wkhtmltoimage_binaries)

Error:

Warning: Blocked access to file C:/XXXXXX/background.A.jpg

Error: Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown

What I did to resolve this:

Add variable options

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

Add options to call

FROM

result = imgkit.from_file('postlayout.A.html', 'out.jpg', config=wkhtmltoimage_binaries)

TO

result = imgkit.from_file('postlayout.A.html', 'out.jpg', config=wkhtmltoimage_binaries, options=kitoptions)

Full Source:

import imgkit

#library path to kit
path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe'
wkhtmltoimage_binaries  = imgkit.config(wkhtmltoimage=path_wkthmltopdf)

#OPTIONS
kitoptions = {
  "enable-local-file-access": None
}

html_file_directory = r'C:\XXXX\template'

result = imgkit.from_file('postlayout.A.html', 'out.jpg', config=wkhtmltoimage_binaries, options=kitoptions)
if result:
    print("successful")
else:
    print("failed")
Pieria answered 19/8, 2021 at 18:59 Comment(0)
C
2

For the C API, contrary to what the documentation says, it's not load.blockLocalFileAccess but loadPage.blockLocalFileAccess that you must set to "false":

wkhtmltoimage_set_global_setting(settings, "loadPage.blockLocalFileAccess", "false");

Hopefully, the documentation will be updated soon; see issue #4763.

Canonicals answered 30/11, 2020 at 12:56 Comment(0)
A
1

I confirm that the problem comes from the wkhtmltopdf version. For those on Symfony (3.4), just add an option in config.yml:

knp_snappy:
    pdf:
        options:
            enable-local-file-access: true
Amaro answered 10/1, 2022 at 10:30 Comment(0)
E
0

I know am a bit late in party but just wanted to write clear example with c# here so one can understand clearly.

ProcessStartInfo proc = new ProcessStartInfo();
        proc = new ProcessStartInfo();
        proc.RedirectStandardError = true;
        proc.UseShellExecute = false;
        proc.WorkingDirectory = @"" + Config.WkhtmltopdfPath;
        proc.FileName = @"" + Config.WkhtmltopdfPath + @"\wkhtmltopdf.exe";
        proc.Arguments = @"  --enable-local-file-access -T 0 -B 0 --page-width 210mm --page-height 450mm " + fileName + ".html " + fileName + ".pdf";
        Process inkscape = Process.Start(proc);
Equilateral answered 22/4, 2022 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.