wkhtmltopdf,0.12.6,警告:阻止访问文件

wkhtmltopdf, 0.12.6, Warning: Blocked access to file

wkhtmltopdf升级到0.12.6时,出现了这样的消息,目标pdf中没有显示图片:

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

顺便说一句,相同的来源 html 文件适用于 0.12.5

这是由 wkhtmltopdf 的 0.12.6 版中默认行为的更改引起的。 wkhtmltopdf 现在默认禁用本地文件访问。可以通过添加命令行参数

来解决
--enable-local-file-access

或组合

--disable-local-file-access --allow <path>

在我的例子中,我把 "enable-local-file-access": "", 放在选项中,它起作用了。

如果您在使用以下内容后仍然遇到相同的错误,请在此线程中进行更正:

--enable-local-file-access

由于某些原因,此命令行参数在 input/output 文件之后指定时不起作用,您必须在 wkhtmltopdf.exe 之后立即写入此参数.

所以

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

而不是其他变体。

对于 C API,与文档所说的相反,它不是 load.blockLocalFileAccess,而是 loadPage.blockLocalFileAccess,您必须将其设置为 "false":

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

希望文档能尽快更新;参见 issue #4763

对于那些正在使用 laravel-snappy 的用户,在 config\snappy.php 中添加 'enable-local-file-access' 选项:

'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 0.12.6版本默认禁用本地文件访问

在 Windows 和 Python 中,当 运行 代码:

时,我也遇到了类似的错误
result = imgkit.from_file('postlayout.A.html', 'out.jpg', config=wkhtmltoimage_binaries)

错误:

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

我做了什么来解决这个问题:

添加变量选项

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

添加通话选项

来自

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

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

完整来源:

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")

我确认问题出在 wkhtmltopdf 版本上。对于那些使用 Symfony (3.4) 的用户,只需在 config.yml:

中添加一个选项
knp_snappy:
    pdf:
        options:
            enable-local-file-access: true

我知道参加聚会有点晚了,但只是想在这里用 C# 写一个清晰的例子,这样人们就可以清楚地理解。

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);