public static void ConvertHtmlToPdf(PdfDocument document, PdfConvertEnvironment environment, PdfOutput woutput) { if (environment == null) environment = Environment; String outputPdfFilePath; bool delete = false; if (woutput.OutputFilePath != null) outputPdfFilePath = woutput.OutputFilePath; else { outputPdfFilePath = Path.Combine(environment.TempFolderPath, String.Format("{0}.pdf", Guid.NewGuid())); delete = true; } if (!File.Exists(environment.WkHtmlToPdfPath)) throw new PdfConvertException(String.Format("File '{0}' not found. Make sure wkhtmltopdf files exist.", environment.WkHtmlToPdfPath)); var paramsBuilder = BuildParameters(document, outputPdfFilePath); var si = new ProcessStartInfo { CreateNoWindow = !environment.Debug, FileName = environment.WkHtmlToPdfPath, Arguments = paramsBuilder.ToString(), UseShellExecute = false, RedirectStandardError = !environment.Debug }; try { using (var process = new Process()) { ProcessHtmlToPdf(si, process, environment, document); if (!File.Exists(outputPdfFilePath)) throw new PdfConvertException(String.Format("Html to PDF conversion of '{0}' failed. Reason: Output file '{1}' not found.", document.Url, outputPdfFilePath)); WriteOutput(woutput, outputPdfFilePath); ExecuteCallback(document, woutput, outputPdfFilePath); } } finally { if (delete && File.Exists(outputPdfFilePath)) File.Delete(outputPdfFilePath); } }
private static void WriteOutput(PdfOutput woutput, string outputPdfFilePath) { if (woutput.OutputStream != null) { using (Stream fs = new FileStream(outputPdfFilePath, FileMode.Open)) { var buffer = new byte[32*1024]; int read; while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) woutput.OutputStream.Write(buffer, 0, read); } } }
public static void ConvertHtmlToPdf(PdfDocument document, PdfOutput output) { ConvertHtmlToPdf(document, null, output); }
private static void ExecuteCallback(PdfDocument document, PdfOutput woutput, string outputPdfFilePath) { if (woutput.OutputCallback != null) { woutput.OutputCallback(document, File.ReadAllBytes(outputPdfFilePath)); } }