Exemple #1
0
        public static void ConvertHtmlToPdf(PdfDocument document, String outputPdfFilePath, Action <PdfDocument, byte[]> OutputCallback)
        {
            StringBuilder paramsBuilder;

            StringBuilder output = new StringBuilder();
            StringBuilder error  = new StringBuilder();

            ParsePdfParameters(document, ref outputPdfFilePath, out paramsBuilder);

            using (Process process = new Process())
            {
                // create a new process of the WKHTMLPDF's executable
                process.StartInfo.FileName  = WkHtmlWkHtmlPdfConverter.WKHTMLPDF_EXE_PATH;
                process.StartInfo.Arguments = paramsBuilder.ToString();

                // don't use the shell mode
                process.StartInfo.UseShellExecute = false;

                // tell the process to redirect the I/O to the application
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError  = true;
                process.StartInfo.RedirectStandardInput  = true;

                using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                    using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                    {
                        DataReceivedEventHandler outputHandler = (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                outputWaitHandle.Set();
                            }
                        };

                        DataReceivedEventHandler errorHandler = (sender, e) =>
                        {
                            if (e.Data == null)
                            {
                                errorWaitHandle.Set();
                            }
                            else
                            {
                                error.AppendLine(e.Data);
                            }
                        };

                        process.OutputDataReceived += outputHandler;
                        process.ErrorDataReceived  += errorHandler;

                        try
                        {
                            // start the process in another thread
                            process.Start();
                            process.BeginOutputReadLine();
                            process.BeginErrorReadLine();

                            // write the HTML document to the standard input
                            using (var stream = process.StandardInput)
                            {
                                byte[] buffer = Encoding.UTF8.GetBytes(document.Html);
                                stream.BaseStream.Write(buffer, 0, buffer.Length);
                                stream.WriteLine();
                            }

                            // wait for the thread, for a minute (by default)
                            if (process.WaitForExit(WkHtmlWkHtmlPdfConverter.TIMEOUT_MS) &&
                                outputWaitHandle.WaitOne(WkHtmlWkHtmlPdfConverter.TIMEOUT_MS) &&
                                errorWaitHandle.WaitOne(WkHtmlWkHtmlPdfConverter.TIMEOUT_MS))
                            {
                                if (process.ExitCode != 0 && !File.Exists(outputPdfFilePath))
                                {
                                    throw new WkHtmlPdfConverterException(
                                              String.Format("Html to PDF conversion failed. Wkhtmltopdf output:\n{0}", error)
                                              );
                                }
                            }
                            else
                            {
                                // kill the process if it is still running in background
                                if (!process.HasExited)
                                {
                                    process.Kill();
                                }

                                throw new WkHtmlPdfConverterTimeoutException();
                            }
                        }
                        finally
                        {
                            // unset the I/O handlers
                            process.OutputDataReceived -= outputHandler;
                            process.ErrorDataReceived  -= errorHandler;
                        }
                    }
            }

            // if the user gave a output callback, write the standard output to the callback Stream
            if (OutputCallback != null)
            {
                byte[] pdfFileBytes = File.ReadAllBytes(outputPdfFilePath);
                OutputCallback(document, pdfFileBytes);
            }
        }
Exemple #2
0
 public static void ConvertHtmlToPdf(PdfDocument document, String outputPdfFilePath)
 {
     WkHtmlWkHtmlPdfConverter.ConvertHtmlToPdf(document, outputPdfFilePath, null);
 }