php 在 Windows 上使用 wkhtmltopdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13791196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using wkhtmltopdf on Windows
提问by AnchovyLegend
I am trying to set up the nifty HTML to image plugin called wkhtmltopdf and I am having a really difficult time.
我正在尝试将漂亮的 HTML 设置为名为 wkhtmltopdf 的图像插件,但我遇到了非常困难的时间。
What I did so far:
到目前为止我做了什么:
Downloaded wkhtmltopdf zip package and upacked the file in my websites root folder
As a test I included the following code in my index.php file, which I would expect to save the contents of bbc.com website as a .jpg image in my websites root folder: ..
shell_exec('./wkhtmltopdf --quality 50 http://www.bbc.combbc.jpg');
下载 wkhtmltopdf zip 包并将文件打包到我的网站根文件夹中
作为测试,我在我的 index.php 文件中包含了以下代码,我希望将 bbc.com 网站的内容作为 .jpg 图像保存在我的网站根文件夹中:..
shell_exec('./wkhtmltopdf --quality 50 http://www.bbc.combbc.jpg');
Nothing seems to happen. PHP is notrunning in safe mode, so that isn't the problem. shell_exec is executing, I was able to create a .txt document using it.
似乎什么都没有发生。PHP没有在安全模式下运行,所以这不是问题。shell_exec 正在执行,我能够使用它创建一个 .txt 文档。
I have been researching this for days, I am offering 100 bountyto anyone that can come up with with a clear, simplified step-by-step working guide on how to set up wkhtmltopdf on Widows to run using PHP.
我一直在研究这个好几天,我向任何人提供100 悬赏金,以提供有关如何在 Widows 上设置 wkhtmltopdf 以使用 PHP 运行的清晰、简化的分步工作指南。
More specifically I am looking for simplifiedwalk-through on how to:
更具体地说,我正在寻找有关如何的简化演练:
Set up wkhtmltopdf to run on Windows using php (include details on common problems and how to overcome them).
On click of a button or link: print the 'current' web page to pdf, including all filled out inputvalues (include sample code for this part). Webpages with events that change the content of the page, should print out the currentversion of the page, notthe initial state of the page.
Create a download link of the .pdf created, onClick of the same function mentioned above that creates and prints to pdf (include sample code for this part).
使用 php 设置 wkhtmltopdf 以在 Windows 上运行(包括常见问题的详细信息以及如何克服它们)。
单击按钮或链接:将“当前”网页打印为 pdf,包括所有填写的输入值(包括此部分的示例代码)。具有更改页面内容的事件的网页应该打印出页面的当前版本,而不是页面的初始状态。
创建 .pdf 的下载链接,onClick 与上面提到的创建和打印到 pdf 相同的功能(包括这部分的示例代码)。
回答by Nenotlep
It's kind of hard to make out what is the actual issue here but I made a blag post tutorial on getting wkhtmltopdf to respond in a Windows 7 / PHP Environment. Ask away if you need further details - I'm sure that this does not cover your issue.
很难弄清楚这里的实际问题是什么,但我制作了一个关于让 wkhtmltopdf 在 Windows 7/PHP 环境中响应的 blag post 教程。如果您需要更多详细信息,请询问 - 我确定这不会涵盖您的问题。
Like I said in the comments as well, your function call shell_exec('./wkhtmltopdf --quality 50 http://www.bbc.com bbc.jpg');will fail because wkhtmltopdf and wkhtmltoimage are different programs and accept different command line options. Replace wkhtmltopdf with wkhtmltoimage in that command and you might get an output file generated. I believe that in your case a test execution like shell_exec('c:\wkhtmltopdf\wkhtmltopdf www.google.com goog.pdf 2>> err3.txt 1>> out3.txt');would be most interesting as there you can see all the different input/output that wkhtmltopdf does.
就像我在评论中所说的那样,您的函数调用shell_exec('./wkhtmltopdf --quality 50 http://www.bbc.com bbc.jpg');将失败,因为 wkhtmltopdf 和 wkhtmltoimage 是不同的程序并接受不同的命令行选项。在该命令中用 wkhtmltoimage 替换 wkhtmltopdf,您可能会生成一个输出文件。我相信在你的情况下,像这样的测试执行shell_exec('c:\wkhtmltopdf\wkhtmltopdf www.google.com goog.pdf 2>> err3.txt 1>> out3.txt');会最有趣,因为你可以看到 wkhtmltopdf 所做的所有不同的输入/输出。
回答by Joshua Walcher
I realize that this is an old question, but since it is the only library I've found that will print from HTML to PDF and retain images and background colors, I use the Windows Command Prompt. I used to use PDF Architect, but it never was able to handle background colors.
我意识到这是一个老问题,但由于它是我发现的唯一一个可以从 HTML 打印到 PDF 并保留图像和背景颜色的库,因此我使用 Windows 命令提示符。我曾经使用 PDF Architect,但它永远无法处理背景颜色。
When you install wkhtmltopdf, it will go into either c:\Program Files\wkhtmltopdf\ or c:\Program Files (x86)\wkhtmltopdf.
当您安装 wkhtmltopdf 时,它将进入 c:\Program Files\wkhtmltopdf\ 或 c:\Program Files (x86)\wkhtmltopdf。
Let's say that you have a local PHP environment where http://localhost/is set up as your starting point. Let's also say that you have a file there called html_invoice.html and that you want to print it to a file called dev_invoice_200.pdf and put it in your invoices folder inside the xampp web server. To do that, you would fire up the Windows command prompt (i.e. Start -> All Programs -> Accessories -> Command Prompt or Start -> Run -> cmd) and type:
假设您有一个本地 PHP 环境,http://localhost/并将其设置为起点。假设您有一个名为 html_invoice.html 的文件,并且您想将其打印到名为 dev_invoice_200.pdf 的文件中,并将其放入 xampp Web 服务器内的发票文件夹中。为此,您需要启动 Windows 命令提示符(即开始 -> 所有程序 -> 附件 -> 命令提示符或开始 -> 运行 -> cmd)并键入:
c:\Program Files\wkhtmltopdf\bin\wkhtmltopdf http://localhost/html_invoice.html c:\xampp\htdocs\invoices\dev_invoice_200.pdf
This will create the PDF in the right place.
这将在正确的位置创建 PDF。
Since we're using the Windows command prompt, there's really no reason to use PHP. Instead, we can use a batch file, and if we need to automate it, we can use Windows Task Scheduler to call the batch file (Start -> All Programs -> Accessories -> System Tools -> Task Scheduler).
由于我们使用的是 Windows 命令提示符,因此真的没有理由使用 PHP。相反,我们可以使用批处理文件,如果我们需要自动化,我们可以使用 Windows 任务计划程序来调用批处理文件(开始 -> 所有程序 -> 附件 -> 系统工具 -> 任务计划程序)。
If you've never done batch files, you just type out the commands you want to do into a plain text file and save it with a .bat extension.
如果您从未做过批处理文件,您只需将您想要执行的命令输入到一个纯文本文件中,并使用 .bat 扩展名保存它。
So let's say that we want to always print the file html_invoice.html at that same spot into the invoices folder with a name of dev_invoice.pdf and that we're going to change the name after it gets there. You'd just put the line above minus the "_200" into a text file and save it as .bat and then double-click on it in Windows or run it through Task Scheduler.
因此,假设我们希望始终将同一位置的 html_invoice.html 文件打印到名为 dev_invoice.pdf 的发票文件夹中,并且我们将在它到达后更改名称。您只需将上面减去“_200”的行放入一个文本文件中并将其另存为 .bat,然后在 Windows 中双击它或通过任务计划程序运行它。

