Last active
May 9, 2018 18:07
-
-
Save chrisfcarroll/5312bc4cb883efc5272d900184a91629 to your computer and use it in GitHub Desktop.
.Net wrapper for the very excellent https://wkhtmltopdf.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Uncomment and paste into LinqPad: void Main() | |
//{ | |
// var pathToWkHtmlToPdfExe=@"C:\yourpathto\wkhtmltopdf.exe"; | |
// var outputPath=new FileInfo(Path.Combine(System.IO.Path.GetTempPath(), "htmlto.pdf")); | |
// var html="<html><head></head><body><style>body{font-family:\"Helvetica\"}</style><h1>Hello World</h1></body></html>"; | |
// using(var outputstream= new MemoryStream(10000)) | |
// { | |
// HtmlToPdfWriter.GeneratePdf(new StringReader(html), outputstream, new Size{Width=210,Height=297}, pathToWkHtmlToPdfExe ); | |
// Encoding.UTF8.GetString(outputstream.GetBuffer()).Dump(); | |
// using (var fs=outputPath.OpenWrite()){ outputstream.CopyTo(fs); } | |
// } | |
// outputPath.FullName.Dump(); | |
//} | |
public static class HtmlToPdfWriter | |
{ | |
public static string defaultExePath = @".\wkhtmltopdf.exe"; | |
public static bool GeneratePdf(TextReader html, Stream pdf, Size pageSizeInMM, string pathToExe = null) | |
{ | |
pathToExe = pathToExe??defaultExePath; | |
ProcessStartInfo psi = new ProcessStartInfo | |
{ | |
FileName = pathToExe, | |
WorkingDirectory = Path.GetDirectoryName(pathToExe), | |
UseShellExecute = false, | |
CreateNoWindow = true, | |
RedirectStandardInput = true, | |
RedirectStandardOutput = true, | |
RedirectStandardError = true, | |
Arguments = | |
"-q --disable-smart-shrinking --no-print-media-type " + | |
(pageSizeInMM.Equals( default(Size) ) | |
? "" | |
: "--page-width " + pageSizeInMM.Width + "mm --page-height " + pageSizeInMM.Height + "mm") + | |
" - -" | |
/* note we tell wkhtmltopdf to be quiet and not run scripts */ | |
}; | |
try | |
{ | |
using (Process p = Process.Start(psi)) | |
{ | |
try | |
{ | |
StreamWriter stdin = p.StandardInput; | |
stdin.AutoFlush = true; | |
stdin.Write(html.ReadToEnd()); | |
stdin.Dispose(); | |
CopyStream(p.StandardOutput.BaseStream, pdf); | |
p.StandardOutput.Close(); | |
pdf.Position = 0; | |
var result = p.WaitForExit(10000); | |
var exitCode = p.ExitCode; | |
return result; | |
} | |
catch | |
{ | |
return false; | |
} | |
} | |
} | |
catch (System.ComponentModel.Win32Exception e) | |
{ | |
throw new ApplicationException("Failed to run the pdf generator because " + e.Message ,e); | |
} | |
} | |
static void CopyStream(Stream input, Stream output) | |
{ | |
byte[] buffer = new byte[32768]; | |
int read; | |
while ((read = input.Read(buffer, 0, buffer.Length)) > 0) | |
{ | |
output.Write(buffer, 0, read); | |
} | |
} | |
} | |
public struct Size { public int Height ; public int Width; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependency:
wkhtmltopdf.exe