wkhtmltopdf 命令失败

发布于 2024-11-03 18:10:57 字数 600 浏览 6 评论 0原文

我正在尝试使用 wkhtmltopdf 将 HTML 文件转换为 PDF。

为此,我在 Debian 机器上安装了 wkhtmltopdf 并尝试运行此命令:

/usr/bin/xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf /path/convert.1303714349.4961.html.tmp /path/convert.1303714349.4961.pdf.tmp

我收到以下错误:

Loading page (1/2)
Error: Failed loading page file:///path/convert.1303714349.4961.html.tmp (sometimes it will work just to ignore this error with --ignore-load-errors)

该文件确实存在于 /path/convert.1303714349.4961.html.tmp 中,并且权限设置正确。它与前面带有 file:// 的路径有什么关系吗?

I am trying to convert a HTML file to PDF using wkhtmltopdf.

For that purpose I have installed wkhtmltopdf on a Debian box and am trying to run this command:

/usr/bin/xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf /path/convert.1303714349.4961.html.tmp /path/convert.1303714349.4961.pdf.tmp

I get the following error:

Loading page (1/2)
Error: Failed loading page file:///path/convert.1303714349.4961.html.tmp (sometimes it will work just to ignore this error with --ignore-load-errors)

The file does exist in /path/convert.1303714349.4961.html.tmp, and the permissions are set correctly. Can it have anything to do with the path being prepended with file://?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-11-10 18:10:57

我相信 wkhtmtopdf 不处理具有非典型扩展名的本地文件。尝试将输入文件重命名为 *.html

请参阅 https://web.archive.org/web/20131124022426/http://code.google.com/p/wkhtmltopdf/issues/detail?id=486

I believe wkhtmtopdf does not handle local files with non-typical extensions. Try renaming the input file to *.html.

See issue and comments at https://web.archive.org/web/20131124022426/http://code.google.com/p/wkhtmltopdf/issues/detail?id=486.

羞稚 2024-11-10 18:10:57

另一个有用的提示是用“_”等字符替换文件名中的空格,因为当文件名包含空格时,进程加载页面失败

Another useful tip is to replace the spaces in the filename by some char like "_", because of when the filename has spaces the process fails loading page

許願樹丅啲祈禱 2024-11-10 18:10:57

您的问题是由文件系统、xvfb-run wkhtmltopdf 和无头 X11 服务器之间的用户权限协商引起的。

用户运行此命令:

cd /home/user;
touch somehtml.html;
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" 
     wkhtmltopdf somehtml.html preview.pdf;

wkhtmltopdf 会产生警告:

警告:加载页面失败
file:///home/user/somehtml.html(忽略)

目标 PDF 文件不存在、缺少 somefile.html 的内容或者为空。

如何重现问题,sscce:

条件:

64 bit Fedora Linux 17, PC Desktop
Install via a 'sudo yum install wkhtmltopdf'
wkhtmltopdf has version 0.10.0
From a terminal, using xvfb-run

somehtml.html 中的违规 HTML:

<html>
<head>head tag</head>
  <body>
    <h1>this should be H1<h1>
    Words words words in a paragraph.
  </body>
</html>

位于用户目录中并运行

cd /home/user;
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf /home/user/somehtml.html /home/user/preview.pdf

wkhtmltopdf 产生警告

警告:加载页面失败
file:///home/user/somehtml.html(忽略)

/home/user/preview.pdf 丢失或为空。

导致问题的原因:

您的 xvfb-runwkhtmlpdf 限制给用户,并且无权读取和/或写入 /home/user/somehtml .pdf/home/user/preview.pdf

要查看这是否是您的问题,请询问 xvfb-run 我们拥有什么样的权限?

/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" whoami > /tmp/secret.txt; cat /tmp/secret.txt

解决方法:

/tmp 通常比 /home/user/ 具有更宽松的权限,因此在 /tmp 内完成所有工作

cd /tmp;
touch /tmp/somehtml.html;
#put above html in somehtml.html
cat /tmp/somehtml.html
    <html>
    <head>head tag</head>
      <body>
        <h1>this should be H1<h1>
        Words words words in a paragraph.
      </body>
    </html>
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf /tmp/somehtml.html /tmp/preview.pdf

并且您应该得到此标准输出:

loaded the Generic plugin 
Loading page (1/2)
Printing pages (2/2)
Done

和 Preview.pdf

chrome_wkhtmltopdf_and_xvfb-run

为什么 wkhtmltopdf 不更聪明并为我处理这个问题?

这比看起来更难,因为 wkhtmltopdf 必须与敌对的第 3 方专有 PDF 渲染软件(由 adobe 制造?)抗衡,并且他们与浏览器开发人员合作来混淆并使其难以访问他们的引擎,因为许多大公司都靠他们的钱他们的绝密源代码。 wkhtmltopdf 正在进入这个世界,以便开源人员无需在高层办公楼中支付现金即可拥有它。这就是为什么 adobe 向浏览器开发者付钱让我们很难做到这一点。我们正在稀释他们的利润机制,因此他们会尽可能地向我们抛出停止棒来进行报复。就是这样。我们在世界范围内向新手传播得越多,第三者就越难阻止,因为你正在阻止其他人获利。

Your trouble is caused by the negotiation of user permission between the filesystem, xvfb-run wkhtmltopdf and headless X11 server.

The User runs this command:

cd /home/user;
touch somehtml.html;
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" 
     wkhtmltopdf somehtml.html preview.pdf;

wkhtmltopdf produces a Warning:

Warning: Failed loading page
file:///home/user/somehtml.html (ignored)

The target PDF file doesn't exist, has missing content from somefile.html or is blank.

How to reproduce the problem, an sscce:

Conditions:

64 bit Fedora Linux 17, PC Desktop
Install via a 'sudo yum install wkhtmltopdf'
wkhtmltopdf has version 0.10.0
From a terminal, using xvfb-run

The offending HTML in somehtml.html:

<html>
<head>head tag</head>
  <body>
    <h1>this should be H1<h1>
    Words words words in a paragraph.
  </body>
</html>

Be in a user's directory and run

cd /home/user;
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf /home/user/somehtml.html /home/user/preview.pdf

wkhtmltopdf produces a warning

Warning: Failed loading page
file:///home/user/somehtml.html (ignored)

/home/user/preview.pdf is missing or blank.

What is causing the problem:

Your xvfb-run jails wkhtmlpdf to the user and doesn't have permission to read and or write to /home/user/somehtml.pdf or /home/user/preview.pdf

To see if this is your problem, ask xvfb-run what kind of permissions we have?

/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" whoami > /tmp/secret.txt; cat /tmp/secret.txt

Workaround:

/tmp typically has looser permissions than /home/user/, so do all your work inside /tmp

cd /tmp;
touch /tmp/somehtml.html;
#put above html in somehtml.html
cat /tmp/somehtml.html
    <html>
    <head>head tag</head>
      <body>
        <h1>this should be H1<h1>
        Words words words in a paragraph.
      </body>
    </html>
/usr/bin/xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf /tmp/somehtml.html /tmp/preview.pdf

And you should get this stdout:

loaded the Generic plugin 
Loading page (1/2)
Printing pages (2/2)
Done

And preview.pdf

chrome_wkhtmltopdf_and_xvfb-run

Why is not wkhtmltopdf smarter and handling this problem for me?

This is harder than it looks, because wkhtmltopdf has to contend with hostile 3rd party proprietary PDF rendering software (made by adobe?) and they work in concert with browser developers to obfuscate and make access to their engines hard because many large corporations by them money for their top secret source code. wkhtmltopdf is pricking into that world so that open source people can have it without paying suits in tall office buildings cash money. That's why adobe pay money to developers of browsers to make this hard for us. We are diluting their profit mechanism, so they retaliate by throwing stop sticks at us where they can. This is that. The more we spreads this round the world to novices, the harder the 3rd party throws stop sticks, since you are preventing profit in others.

淡淡绿茶香 2024-11-10 18:10:57

我的 Windows 7 机器上的“file://”路径没有遇到任何问题。

您是否尝试过错误消息 --igonore-load-errors 中的建议?

I haven't had any problem with "file://" paths on my Windows 7 box.

Did you try the advice in the error message, --igonore-load-errors?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文