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.
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.
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?
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.