Create pdf with wkhtmltopdf and rendering javascript

Viewed 21171

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.

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

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.

Thanks.

3 Answers
Related