使用 wkhtmltopdf 创建 pdf 并渲染 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6949685/
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
Create pdf with wkhtmltopdf and rendering javascript
提问by Zero Cool
I'm attempting to create a PDF of a javascript chart that I have in a model window (my chart is a combination of javascript and css in an .aspx view). The only thing in the rendered PDF file is the static content from the window, the actual javascript chart is not there.
我正在尝试创建模型窗口中的 javascript 图表的 PDF(我的图表是 .aspx 视图中的 javascript 和 css 的组合)。渲染的 PDF 文件中唯一的内容是窗口中的静态内容,实际的 javascript 图表不存在。
My call to create the PDF is as follows:
我创建 PDF 的调用如下:
public byte[] WKHtmlToPdf(string url)
{
var fileName = " - ";
var wkhtmlDir = "C:\Temp\wkhtml";
var wkhtml = "C:\Temp\wkhtml\wkhtmltopdf.exe";
var p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = wkhtml;
p.StartInfo.WorkingDirectory = wkhtmlDir;
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 0mm --margin-bottom 0mm --margin-right 0mm --margin-left 0mm ";
switches += "--page-size Letter ";
p.StartInfo.Arguments = switches + " " + url + " " + fileName;
p.Start();
//read output
byte[] buffer = new byte[32768];
byte[] file;
using (var ms = new MemoryStream())
{
while (true)
{
int read = p.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);
if (read <= 0)
{
break;
}
ms.Write(buffer, 0, read);
}
file = ms.ToArray();
}
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
return returnCode == 0 ? file : null;
}
Any ideas on how I could grab the javascript chart? Perhaps the .Net version would be more appropriate or I have to save the generated page to a file and pass that into the tool.
关于如何获取 javascript 图表的任何想法?也许 .Net 版本更合适,或者我必须将生成的页面保存到文件中并将其传递到工具中。
Thanks.
谢谢。
采纳答案by Rick van Hal
In our project we do something simular, with success. We use wkhtmltopdf 0.9.9 in combination with Highchartsat the moment. With jQuery flot we had success too after a little tweaking. In our project we first render the view to a string and pass it to wkhtml using it's stdin. Then we catch the stdout of wkhtml and pass it back to the browser.
在我们的项目中,我们做了一些类似的事情,并取得了成功。目前我们将 wkhtmltopdf 0.9.9 与Highcharts结合使用。使用 jQuery flot 我们在稍微调整后也取得了成功。在我们的项目中,我们首先将视图呈现为一个字符串,并使用它的标准输入将它传递给 wkhtml。然后我们捕获 wkhtml 的标准输出并将其传递回浏览器。
Your wkhtml-setting seems to be right, except we use stdin and stdout. Don't know if that can be a problem.
您的 wkhtml 设置似乎是正确的,除了我们使用 stdin 和 stdout。不知道这会不会有问题。
If you use one of those charts I think I can help you. What chart are you using?
如果您使用这些图表之一,我想我可以帮助您。你用的是什么图表?
One last note: Wkhtmltopdf 0.10rc2 seems to have some problems loading external resources (js/css) from localhost when using a portnumber different from port 80.
最后一个注意事项:Wkhtmltopdf 0.10rc2 在使用不同于端口 80 的端口号时,从本地主机加载外部资源 (js/css) 似乎存在一些问题。
回答by Simon Elliston Ball
It looks like you're trying to get the output of a chart, which judging by the tags is from an Extjs 4 script.
看起来您正在尝试获取图表的输出,根据标签判断该输出来自 Extjs 4 脚本。
The ext script is probably using some of the chart's animation, and will certainly be waiting for javascript events to execute and render the chart. It's probably therefore not done by the time the default time (200 ms) is done.
ext 脚本可能正在使用一些图表的动画,并且肯定会等待 javascript 事件来执行和呈现图表。因此,在默认时间(200 毫秒)完成时可能还没有完成。
A quick fix would be to add the javascript-delay page option to the command line:
一个快速的解决方法是在命令行中添加 javascript-delay 页面选项:
wkhtmltopdf http://dev.sencha.com/deploy/ext-4.0.2a/examples/charts/Mixed.html --javascript-delay=2000 test.pdf
will certainly work on *nix, and a similar thing should work on windows.
wkhtmltopdf http://dev.sencha.com/deploy/ext-4.0.2a/examples/charts/Mixed.html --javascript-delay=2000 test.pdf
肯定会在 *nix 上工作,类似的事情也应该在 Windows 上工作。
回答by Dan Benamy
I was using wkhtmltopdf 0.9.9 and the pdfkit ruby gem, and having a similar problem.
我正在使用 wkhtmltopdf 0.9.9 和 pdfkit ruby gem,并且遇到了类似的问题。
I fixed it by changing all tags to use absolute urls. It seems that wkhtmltopdf didn't know what url the page was accessed via so relative resources weren't being loaded. I don't know if that's a limitation of the pdfkit or wkhtmltopdf.
我通过将所有标签更改为使用绝对 url 来修复它。似乎 wkhtmltopdf 不知道页面是通过什么网址访问的,因此没有加载相关资源。我不知道这是否是 pdfkit 或 wkhtmltopdf 的限制。
I got the idea from https://github.com/mileszs/wicked_pdf.
我从https://github.com/mileszs/wicked_pdf得到了这个想法。