与wkhtmltopdf和渲染JavaScript的创建PDF [英] Create pdf with wkhtmltopdf and rendering javascript

查看:397
本文介绍了与wkhtmltopdf和渲染JavaScript的创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个JavaScript图表我有一个示范窗口的PDF(图我是JavaScript和CSS在.aspx视图的组合)。在渲染PDF文件的唯一事情是从窗口静态内容,实际的JavaScript图表是不存在的。

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.

我的呼吁创建PDF是如下:

My call to create the PDF is as follows:

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;
    }

这是我怎么能抢的javascript图表任何想法?也许.NET版本会更适合或者我必须将生成的页面保存到一个文件,并传递到工具中。

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.

感谢。

推荐答案

在我们的项目中,我们做一些simular,成功。我们使用wkhtmltopdf 0.9.9结合 Highcharts 的时刻。与jQuery海军报,我们有一个小调整后,成功了。在我们的项目中,我们首先渲染视图为字符串,并通过它使用它的标准输入到wkhtml。然后,我们赶wkhtml的输出和传递回浏览器。

In our project we do something simular, with success. We use wkhtmltopdf 0.9.9 in combination with Highcharts at 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.

您wkhtml设定似乎是正确的,但我们使用标准输入和stdout。不知道这是否是一个问题。

Your wkhtml-setting seems to be right, except we use stdin and stdout. Don't know if that can be a problem.

如果您使用的这些图表,我想我可以帮你的一个。您正在使用什么图?

If you use one of those charts I think I can help you. What chart are you using?

最后一个音符:Wkhtmltopdf 0.10rc2似乎有从本地主机的一些问题加载外部资源(JS / CSS)利用端口80个不同的端口号的时

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和渲染JavaScript的创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆