如何处理使用wkhtmltopdf时出现的ContentNotFoundError错误?

12

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

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

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

[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
  • 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。


    这些问题听起来像是由ashkulz在github问题跟踪器中最好回答的错误,我认为你最好在那里提问。 - Joel Peltonen
    我曾经遇到类似的错误,把 CSS 中的 @font-face 删掉之后问题就解决了。 - pravin kumar sinha
    4个回答

    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,您还可以使用htmldocPhantomJS以及一些额外的脚本,例如使用rasterize.js
    phantomjs rasterize.js http://example.com/
    

    使用如下示例代码,您可以使用PHP中的dompdf(将HTML转换为PDF的工具,可以通过composer安装):

    或者dompdf

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

    1
    它的处理方式与你在这里提到的一样,经过更多关于wkhtmltopdf的工作,我也了解了相对资源路径、损坏的链接等问题。我通过修复phantomjs脚本解决了我的问题。 - Murali Mopuru

    1
    我的问题得到了解决,是通过从CSS中删除@font-face实现的。

    0

    我以前也遇到过这个问题,解决方法如下:

    wkhtmltopdf

    在上面的例子中,我有一些“src”文件和“url”,它们引用静态目录,但静态目录不存在,所以wkhtmltopdf抛出了那个错误。例如:
    src:url(“file:///home/ehsan/Projects/Example/main/sib/static/WebYekan.eot”);
    还有一件更重要的事情,我必须说的是,在HTML文件中的所有文件路径都必须是绝对路径。根本不要使用相对路径。
    我希望这可以帮助你。

    0

    我搜索了很多但是没有找到,最终在这里找到了。我之前使用了 (./name) 但是出现了"contentnotfound"的错误。

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


    1
    抱歉,我不太明白你提出的建议作为答案。能否提供你解决方案的代码示例? - shox

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接