使用wkhtmltopdf时如何处理ContentNotFoundError?

问题描述 投票:0回答:5

有人可以告诉我如何解决以下问题吗?

  1. wkhtmltopdf 没有选项传递代理信息(-p 或 --proxy),与以前的版本不同,它也不使用系统 $http_proxy 和 $https_proxy 环境变量。

  2. wkhtmltopdf 无法使用 HTTPS/SSL,即使我为 libssl.so 和 libcrypto.so 设置了 LD_LIBRARY_PATH

    [deploy@localhost ~]$ wkhtmltopdf https://www.google.co.in google.pdf
    loaded the Generic plugin 
    Loading page (1/2)
    Error: Failed loading page https://www.google.co.in (sometimes it will work just to ignore this error with --load-error-handling ignore)
    Exit with code 1 due to network error: UnknownNetworkError
    

    [deploy@localhost ~]$ wkhtmltoimage https://www.google.co.in sample.jpg
    loaded the Generic plugin 
    Loading page (1/2)
    Error: Failed loading page https://www.google.co.in (sometimes it will work just to ignore this error with --load-error-handling ignore)
    Exit with code 1 due to network error: UnknownNetworkError
    
  3. wkhtmltopdf 部分使用 HTTP。输出的 pdf 文件缺少一些内容/背景/位置。

    [deploy@localhost ~]$ wkhtmltopdf http://localhost:8880/ sample.pdf
    loaded the Generic plugin 
    Loading page (1/2)
    Printing pages (2/2)                                               
    Done                                                           
    Exit with code 1 due to network error: ContentNotFoundError
    
    [deploy@localhost ~]$ wkhtmltoimage http://localhost:8880/ sample.jpg
    loaded the Generic plugin 
    Loading page (1/2)
    Rendering (2/2)                                                    
    Done                                                               
    Exit with code 1 due to network error: ContentNotFoundError
    

注意:我使用 wkhtmltopdf-0.12.1-1.fc20.x86_64 和 qt-4.8.6-10.fc20.x86_64

qt command-line wkhtmltopdf download wkhtmltoimage
5个回答
4
投票

不幸的是

wkhtmltopdf
不能处理复杂网站的下载,因为它使用Qt/QtWebKit库,这似乎有一些问题。

一个问题是

wkhtmltopdf
不支持相对地址(GitHub:#1634#1886#2359QTBUG-46240),例如:

<img src="/images/filetypes/txt.png">
<script src="//cdn.optimizely.com/js/653710485.js">

并将它们加载为本地。我发现的一种解决方案是通过

ex
就地编辑器就地纠正 html 文件:

ex -V1 page.html <<-EOF
  %s,'//,'http://,ge 
  %s,"//,"http://,ge 
  %s,'/,'http://www.example.com/,ge
  %s,"/,"http://www.example.com/,ge
  wq " Update changes and quit.
EOF

但是,它不适用于远程上具有此类 URL 的文件。

另一个问题是它不处理丢失的资源。您可以尝试指定

--load-error-handling ignore
,但在大多数情况下它不起作用(参见 #2051),因此这仍然很突出。解决方法是在转换之前简单地删除这些无效资源。

除了

wkhtmltopdf
之外,您还可以使用
htmldoc
PhantomJS以及一些附加脚本,例如使用rasterize.js

phantomjs rasterize.js http://example.com/

dompdf(PHP 的 HTML 到 PDF 转换器,您可以通过 Composer 安装),示例代码如下:

<?php
// somewhere early in your project's loading, require the Composer autoloader
// see: http://getcomposer.org/doc/00-intro.md
$HOMEDIR = "/Users/foo";
require $HOMEDIR . '/.composer/vendor/autoload.php';

// disable DOMPDF's internal autoloader if you are using Composer
define('DOMPDF_ENABLE_AUTOLOAD', FALSE);
define('DOMPDF_ENABLE_REMOTE', TRUE);

// include DOMPDF's default configuration
require_once $HOMEDIR . '/.composer/vendor/dompdf/dompdf/dompdf_config.inc.php';

$htmlString = file_get_contents("https://example.com/foo.pdf");

$dompdf = new DOMPDF();
$dompdf->load_html($htmlString);
$dompdf->render();
$dompdf->stream("sample.pdf");

2
投票

我的问题已解决,从 css 中删除@font-face。


0
投票

我以前也遇到过这个问题。并像下面一样解决这个问题。

wkhtmltopdf

在上面的示例中,我有一些“src”文件和“url”,它们引用静态目录,但静态目录不存在,因此 wkhtmltopdf 向我抛出了该错误。例如:

src:url(“文件:///home/ehsan/Projects/Example/main/sib/static/WebYekan.eot”);

我要说的更重要的一件事是html文件中的所有文件路径都必须是绝对路径。根本不使用相对路径。

我希望这对你有帮助。


0
投票

我找了很多都找不到,但终于在这里找到了。我正在使用 (./name) 但这创建了 contentnotfound 错误。

但最终使用了完整的地址并得到了想要的结果


0
投票

如果 dns 设置错误,可能会发生这种情况。

从您的服务器本身 ping 网址。如果回放与服务器地址不一样,需要添加dns记录。

您可以通过添加正确的 dns 设置来修复它:

sudo nano /etc/hosts

然后添加 IP 地址和 Web 域。

© www.soinside.com 2019 - 2024. All rights reserved.