有人可以告诉我如何解决以下问题吗?
[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
[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
最佳答案
不幸的是,wkhtmltopdf
无法处理复杂网站的下载,因为它使用的Qt/QtWebKit库似乎有一些问题。
一个问题是wkhtmltopdf
不支持相对地址(GitHub:#1634,#1886,#2359,QTBUG-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
但是,对于在 Remote 上具有此类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");
关于qt - 使用wkhtmltopdf时如何处理ContentNotFoundError?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25894251/