I Am using WkhtmlToImage to render a web page to an image. When I run it from the command line everything works great. However, when I run it from a process started from my web app it doesn't.
I have verified that the arguments I am using are the same. The only difference I can see is that when I run it from the command line I save the file to disk, and when I do it from the web app I am using stdOut and returning the byte array. Does anyone know why this is happening? I am using 11.0-rc2
//taken from the Rotativa library - https://github.com/webgio/Rotativa/
private static byte[] Convert(string wkhtmltopdfPath, string switches, string html)
{
// switches:
// "-q" - silent output, only errors - no progress messages
// " -" - switch output to stdout
// "- -" - switch input to stdin and output to stdout
switches = "-q " + switches + " -";
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
switches += " -";
html = SpecialCharsEncode(html);
}
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltoimage.exe"),
Arguments = switches,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
WorkingDirectory = wkhtmltopdfPath,
CreateNoWindow = true
}
};
proc.Start();
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
using (var sIn = proc.StandardInput)
{
sIn.WriteLine(html);
}
}
var ms = new MemoryStream();
using (var sOut = proc.StandardOutput.BaseStream)
{
byte[] buffer = new byte[4096];
int read;
while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
string error = proc.StandardError.ReadToEnd();
if (ms.Length == 0)
{
throw new Exception(error);
}
proc.WaitForExit();
return ms.ToArray();
}
update I found it is a known issue with the library when using stdOut in windows. If anyone has any ideas I am all ears.
http://code.google.com/p/wkhtmltopdf/issues/detail?id=335&q=wkhtmltoimage%20stdout http://code.google.com/p/wkhtmltopdf/issues/detail?id=998&q=wkhtmltoimage%20stdout
You'd better use I/O files for the wkhtmltoimage.exe
process instead of I/O streams:
public static byte[] Convert(string wkhtmltopdfPath, string switches, string html)
{
using (var tempFiles = new TempFileCollection())
{
var input = tempFiles.AddExtension("htm");
var output = tempFiles.AddExtension("jpg");
File.WriteAllText(input, html);
switches += string.Format(" -f jpeg {0} {1}", input, output);
var psi = new ProcessStartInfo(Path.Combine(wkhtmltopdfPath, "wkhtmltoimage.exe"))
{
UseShellExecute = false,
CreateNoWindow = true,
Arguments = switches
};
using (var process = Process.Start(psi))
{
process.WaitForExit((int)TimeSpan.FromSeconds(30).TotalMilliseconds);
}
return File.ReadAllBytes(output);
}
}
and then:
byte[] result = Convert(
@"c:\Program Files (x86)\wkhtmltopdf",
"",
File.ReadAllText("test.htm")
)