private void InvokeWkHtmlToPdf(HtmlToPdfConverter.PdfSettings pdfSettings, string inputContent, Stream outputStream) { string lastErrorLine = string.Empty; DataReceivedEventHandler receivedEventHandler = (DataReceivedEventHandler)((o, args) => { if (args.Data == null) { return; } if (!string.IsNullOrEmpty(args.Data)) { lastErrorLine = args.Data; } // ISSUE: reference to a compiler-generated field if (this.LogReceived != null) { // ISSUE: reference to a compiler-generated field this.LogReceived((object)this, args); } }); byte[] buffer = inputContent != null?Encoding.UTF8.GetBytes(inputContent) : (byte[])null; try { this.WkHtmlToPdfProcess = Process.Start(new ProcessStartInfo(this.GetToolExePath(), this.ComposeArgs(pdfSettings)) { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, UseShellExecute = false, WorkingDirectory = Path.GetDirectoryName(this.PdfToolPath), RedirectStandardInput = buffer != null, RedirectStandardOutput = outputStream != null, RedirectStandardError = true }); this.WkHtmlToPdfProcess.ErrorDataReceived += receivedEventHandler; this.WkHtmlToPdfProcess.BeginErrorReadLine(); if (buffer != null) { this.WkHtmlToPdfProcess.StandardInput.BaseStream.Write(buffer, 0, buffer.Length); this.WkHtmlToPdfProcess.StandardInput.BaseStream.Flush(); this.WkHtmlToPdfProcess.StandardInput.Close(); } long num = 0; if (outputStream != null) { num = (long)this.ReadStdOutToStream(this.WkHtmlToPdfProcess, outputStream); } this.WaitWkHtmlProcessForExit(); if (outputStream == null && File.Exists(pdfSettings.OutputFile)) { num = new FileInfo(pdfSettings.OutputFile).Length; } this.CheckExitCode(this.WkHtmlToPdfProcess.ExitCode, lastErrorLine, num > 0L); } finally { this.EnsureWkHtmlProcessStopped(); } }
private void InvokeWkHtmlToPdfInBatch(HtmlToPdfConverter.PdfSettings pdfSettings) { string lastErrorLine = string.Empty; DataReceivedEventHandler receivedEventHandler = (DataReceivedEventHandler)((o, args) => { if (args.Data == null) { return; } if (!string.IsNullOrEmpty(args.Data)) { lastErrorLine = args.Data; } // ISSUE: reference to a compiler-generated field if (this.LogReceived != null) { // ISSUE: reference to a compiler-generated field this.LogReceived((object)this, args); } }); if (this.WkHtmlToPdfProcess == null || this.WkHtmlToPdfProcess.HasExited) { this.WkHtmlToPdfProcess = Process.Start(new ProcessStartInfo(this.GetToolExePath(), "--read-args-from-stdin") { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true, UseShellExecute = false, WorkingDirectory = Path.GetDirectoryName(this.PdfToolPath), RedirectStandardInput = true, RedirectStandardOutput = false, RedirectStandardError = true }); this.WkHtmlToPdfProcess.BeginErrorReadLine(); } this.WkHtmlToPdfProcess.ErrorDataReceived += receivedEventHandler; try { if (File.Exists(pdfSettings.OutputFile)) { File.Delete(pdfSettings.OutputFile); } this.WkHtmlToPdfProcess.StandardInput.WriteLine(this.ComposeArgs(pdfSettings).Replace('\\', '/')); bool flag = true; while (flag) { Thread.Sleep(25); if (this.WkHtmlToPdfProcess.HasExited) { flag = false; } if (File.Exists(pdfSettings.OutputFile)) { flag = false; this.WaitForFile(pdfSettings.OutputFile); } } if (!this.WkHtmlToPdfProcess.HasExited) { return; } this.CheckExitCode(this.WkHtmlToPdfProcess.ExitCode, lastErrorLine, File.Exists(pdfSettings.OutputFile)); } finally { if (this.WkHtmlToPdfProcess != null && !this.WkHtmlToPdfProcess.HasExited) { this.WkHtmlToPdfProcess.ErrorDataReceived -= receivedEventHandler; } else { this.EnsureWkHtmlProcessStopped(); } } }
private void GeneratePdfInternal(HtmlToPdfConverter.WkHtmlInput[] htmlFiles, string inputContent, string coverHtml, string outputPdfFilePath, Stream outputStream) { if (!this.batchMode) { this.EnsureWkHtmlLibs(); } this.CheckWkHtmlProcess(); string tempPath = this.GetTempPath(); HtmlToPdfConverter.PdfSettings pdfSettings = new HtmlToPdfConverter.PdfSettings() { InputFiles = htmlFiles, OutputFile = outputPdfFilePath }; List <string> tempFilesList = new List <string>(); pdfSettings.CoverFilePath = !string.IsNullOrEmpty(coverHtml) ? this.CreateTempFile(coverHtml, tempPath, tempFilesList) : (string)null; pdfSettings.HeaderFilePath = !string.IsNullOrEmpty(this.PageHeaderHtml) ? this.CreateTempFile("<!DOCTYPE html><html><head>< meta http - equiv = \"content-type\" content = \"text/html; charset=utf-8\" />< script >function subst() {{ var vars = { { } }; var x = document.location.search.substring(1).split('&'); for(var i in x) { { var z = x[i].split('=', 2); vars[z[0]] = unescape(z[1]); } }var x =['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection']; for(var i in x) { { var y = document.getElementsByClassName(x[i]); for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]]; } } }}</ script ></ head >< body style = \"border:0; margin: 0;\" onload = \"subst()\" >{ this.PageHeaderHtml}</ body ></ html >", tempPath, tempFilesList) : (string)null; pdfSettings.FooterFilePath = !string.IsNullOrEmpty(this.PageFooterHtml) ? this.CreateTempFile("<!DOCTYPE html><html><head>< meta http - equiv = \"content -type\" content = \"text/html; charset=utf-8\" /> < script > function subst() { { var vars = { { } }; var x = document.location.search.substring(1).split('&'); for(var i in x) { { var z = x[i].split('=', 2); vars[z[0]] = unescape(z[1]); } } var x =['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection']; for(var i in x) { { var y = document.getElementsByClassName(x[i]); for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]]; } } }} </ script ></ head >< body style = \"border:0; margin: 0;\" onload = \"subst()\" >{ this.PageFooterHtml}</ body ></ html >", tempPath, tempFilesList) : (string)null; if (pdfSettings.InputFiles != null) { foreach (HtmlToPdfConverter.WkHtmlInput inputFile in pdfSettings.InputFiles) { inputFile.HeaderFilePath = !string.IsNullOrEmpty(inputFile.PageHeaderHtml) ? this.CreateTempFile("<!DOCTYPE html><html><head> < meta http - equiv = \"content -type\" content = \"text/html; charset=utf-8\" /> < script > function subst() { { var vars = { { } }; var x = document.location.search.substring(1).split('&'); for(var i in x) { { var z = x[i].split('=', 2); vars[z[0]] = unescape(z[1]); } } var x =['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection']; for(var i in x) { { var y = document.getElementsByClassName(x[i]); for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]]; } } } }</ script ></ head >< body style = \"border:0; margin: 0;\" onload = \"subst()\" >{ input.PageHeaderHtml}</ body ></ html > ", tempPath, tempFilesList) : (string)null; inputFile.FooterFilePath = !string.IsNullOrEmpty(inputFile.PageFooterHtml) ? this.CreateTempFile("<!DOCTYPE html><html><head> < meta http - equiv = \"content -type\" content = \"text/html; charset=utf-8\" /> < script > function subst() { { var vars = { { } }; var x = document.location.search.substring(1).split('&'); for(var i in x) { { var z = x[i].split('=', 2); vars[z[0]] = unescape(z[1]); } } var x =['frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection']; for(var i in x) { { var y = document.getElementsByClassName(x[i]); for (var j = 0; j < y.length; ++j) y[j].textContent = vars[x[i]]; } } } } </ script ></ head >< body style = \"border:0; margin: 0;\" onload = \"subst()\" >{ input.PageFooterHtml}</ body ></ html >", tempPath, tempFilesList) : (string)null; } } try { if (inputContent != null) { pdfSettings.InputFiles = new HtmlToPdfConverter.WkHtmlInput[1] { new HtmlToPdfConverter.WkHtmlInput(this.CreateTempFile(inputContent, tempPath, tempFilesList)) } } ; if (outputStream != null) { pdfSettings.OutputFile = this.CreateTempFile((string)null, tempPath, tempFilesList); } if (this.batchMode) { this.InvokeWkHtmlToPdfInBatch(pdfSettings); } else { this.InvokeWkHtmlToPdf(pdfSettings, (string)null, (Stream)null); } if (outputStream == null) { return; } using (FileStream fileStream = new FileStream(pdfSettings.OutputFile, FileMode.Open, FileAccess.Read, FileShare.Read)) this.CopyStream((Stream)fileStream, outputStream, 65536); } catch (Exception ex) { if (!this.batchMode) { this.EnsureWkHtmlProcessStopped(); } throw new Exception("Cannot generate PDF: " + ex.Message, ex); } finally { foreach (string filePath in tempFilesList) { this.DeleteFileIfExists(filePath); } } }
private string ComposeArgs(HtmlToPdfConverter.PdfSettings pdfSettings) { StringBuilder stringBuilder = new StringBuilder(); if (this.Quiet) { stringBuilder.Append(" -q "); } if ((uint)this.Orientation > 0U) { stringBuilder.AppendFormat(" -O {0} ", (object)this.Orientation.ToString()); } if ((uint)this.Size > 0U) { stringBuilder.AppendFormat(" -s {0} ", (object)this.Size.ToString()); } if (this.LowQuality) { stringBuilder.Append(" -l "); } if (this.Grayscale) { stringBuilder.Append(" -g "); } if (this.PageWidth.HasValue) { stringBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, " --page-width {0}", new object[1] { (object)this.PageWidth }); } if (this.PageHeight.HasValue) { stringBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, " --page-height {0}", new object[1] { (object)this.PageHeight }); } if (pdfSettings.HeaderFilePath != null) { stringBuilder.AppendFormat(" --header-html \"{0}\"", (object)pdfSettings.HeaderFilePath); } if (pdfSettings.FooterFilePath != null) { stringBuilder.AppendFormat(" --footer-html \"{0}\"", (object)pdfSettings.FooterFilePath); } if (!string.IsNullOrEmpty(this.CustomWkHtmlArgs)) { stringBuilder.AppendFormat(" {0} ", (object)this.CustomWkHtmlArgs); } if (pdfSettings.CoverFilePath != null) { stringBuilder.AppendFormat(" cover \"{0}\" ", (object)pdfSettings.CoverFilePath); if (!string.IsNullOrEmpty(this.CustomWkHtmlCoverArgs)) { stringBuilder.AppendFormat(" {0} ", (object)this.CustomWkHtmlCoverArgs); } } if (this.GenerateToc) { stringBuilder.Append(" toc "); if (!string.IsNullOrEmpty(this.TocHeaderText)) { stringBuilder.AppendFormat(" --toc-header-text \"{0}\"", (object)this.TocHeaderText.Replace("\"", "\\\"")); } if (!string.IsNullOrEmpty(this.CustomWkHtmlTocArgs)) { stringBuilder.AppendFormat(" {0} ", (object)this.CustomWkHtmlTocArgs); } } foreach (HtmlToPdfConverter.WkHtmlInput inputFile in pdfSettings.InputFiles) { stringBuilder.AppendFormat(" \"{0}\" ", (object)inputFile.Input); string str = inputFile.CustomWkHtmlPageArgs ?? this.CustomWkHtmlPageArgs; if (!string.IsNullOrEmpty(str)) { stringBuilder.AppendFormat(" {0} ", (object)str); } if (inputFile.HeaderFilePath != null) { stringBuilder.AppendFormat(" --header-html \"{0}\"", (object)inputFile.HeaderFilePath); } if (inputFile.FooterFilePath != null) { stringBuilder.AppendFormat(" --footer-html \"{0}\"", (object)inputFile.FooterFilePath); } if ((double)this.Zoom != 1.0) { stringBuilder.AppendFormat((IFormatProvider)CultureInfo.InvariantCulture, " --zoom {0} ", new object[1] { (object)this.Zoom }); } } stringBuilder.AppendFormat(" \"{0}\" ", (object)pdfSettings.OutputFile); return(stringBuilder.ToString()); }