Wkhtmltopdf,0.12.6,警告: 阻止对文件的访问

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

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

顺便说一下,同样的源 html 文件可以很好地与 0.12.5一起工作

39820 次浏览

这是由于 wkhtmltopdf版本0.12.6中默认行为的改变造成的。wkhtmltopdf现在默认禁用本地文件访问。可以通过添加命令行参数来解决这个问题

--enable-local-file-access

或者密码

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

在我的案例中,我把 "enable-local-file-access": "",放入期权,它起作用了。

如果你使用以下命令仍然得到相同的错误,那么只需要修正一下这个线程:

--enable-local-file-access

由于某些原因,在输入/输出文件之后指定这个 cmd 行参数时无法工作,因此必须在 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");

希望很快能更新文档; 请参阅 第4763期

对于那些使用 吐得真快的用户,在 config snappy.php 中添加“ able-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'     => [],
],

在0.12.6版本中,wkhtmltopdf 默认禁用本地文件访问

在使用 Python 的 Windows 中,我在运行代码时也遇到了类似的错误:

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

错误:

警告: 阻止对文件 C:/XXXXXX/backound.A.jpg 的访问

错误: 无法加载 about: black,网络状态代码为301和 HTTP状态码0-协议“关于”是未知的

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

添加可变选项

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