コード例 #1
0
ファイル: PdfConvert.cs プロジェクト: ajsjunior/wkhtmltopdf-1
        /// <summary>
        /// Converts one or more HTML pages into a PDF document.
        /// </summary>
        /// <param name="document">Document definitions.</param>
        /// <param name="environment">Environment definitions.</param>
        /// <param name="woutput">Output definitions.</param>
        public static void ConvertHtmlToPdf(PdfDocument document, PdfConvertEnvironment environment, PdfOutput woutput)
        {
            if ((document.Pages == null) || (document.Pages.Count == 0))
            {
                throw new PdfConvertException("You must supply at least one page");
            }

            if (document.Pages.OfType <PdfPage>().Any(p => string.IsNullOrEmpty(p.Html)))
            {
                throw new PdfConvertException("You must supply a HTML string or a URL for all pages");
            }

            if (document.Pages.OfType <PdfCover>().Any(p => string.IsNullOrEmpty(p.Html)))
            {
                throw new PdfConvertException("You must supply a HTML string or a URL for all cover pages");
            }

            if (environment == null)
            {
                environment = new PdfConvertEnvironment();
            }

            if (!environment.Timeout.HasValue)
            {
                environment.Timeout = 60000;
            }

            if (string.IsNullOrEmpty(environment.TempFolderPath))
            {
                environment.TempFolderPath = Path.GetTempPath();
            }

            if (string.IsNullOrEmpty(environment.WkHtmlToPdfPath))
            {
                environment.WkHtmlToPdfPath = GetWkhtmlToPdfExeLocation();
            }

            string outputPdfFilePath;
            bool   delete;

            if (woutput.OutputFilePath != null)
            {
                outputPdfFilePath = woutput.OutputFilePath;
                delete            = false;
            }
            else
            {
                outputPdfFilePath = Path.Combine(environment.TempFolderPath, string.Format("{0}.pdf", Guid.NewGuid()));
                delete            = true;
            }

            // Se for um caminho completo verifica se o executável existe, do contrário considera que ele estará no PATH para rodar corretamente
            if ((environment.WkHtmlToPdfPath.IndexOf(@"\", StringComparison.CurrentCulture) >= 0) && (!File.Exists(environment.WkHtmlToPdfPath)))
            {
                throw new PdfConvertException(string.Format("File '{0}' not found. Check if wkhtmltopdf application is installed.", environment.WkHtmlToPdfPath));
            }

            StringBuilder  paramsBuilder = new StringBuilder();
            WorkEnviroment workEnv       = new WorkEnviroment()
            {
                TempFolderPath = environment.TempFolderPath
            };

            document.GetCmdArguments(paramsBuilder, workEnv);
            paramsBuilder.AppendFormat("\"{0}\" ", outputPdfFilePath);

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

                using (Process process = new Process())
                {
                    process.StartInfo.FileName               = environment.WkHtmlToPdfPath;
                    process.StartInfo.Arguments              = paramsBuilder.ToString().TrimEnd();
                    process.StartInfo.UseShellExecute        = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError  = true;
                    process.StartInfo.RedirectStandardInput  = true;
                    process.StartInfo.WorkingDirectory       = AppDomain.CurrentDomain.BaseDirectory;

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

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

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

                            Debug.Print("Converting to PDF: {0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
                            try
                            {
                                process.Start();

                                process.BeginOutputReadLine();
                                process.BeginErrorReadLine();

                                //TODO:Usar parâmetro --read-args-from-stdin para otimizar geração de vários PDFs em processo batch
                                if (workEnv.StdinArguments.Count > 0) // Se precisa passar algum HTML por stdin
                                {
                                    using (var stream = process.StandardInput)
                                    {
                                        workEnv.StdinArguments.ForEach(input =>
                                        {
                                            byte[] buffer = Encoding.UTF8.GetBytes(input);
                                            stream.BaseStream.Write(buffer, 0, buffer.Length);
                                            stream.WriteLine();
                                        });
                                    }
                                }
                                ;

                                if (process.WaitForExit(environment.Timeout.Value) && outputWaitHandle.WaitOne(environment.Timeout.Value) && errorWaitHandle.WaitOne(environment.Timeout.Value))
                                {
                                    if (process.ExitCode != 0 && !File.Exists(outputPdfFilePath))
                                    {
                                        throw new PdfConvertException(string.Format("Html to PDF conversion failed. Wkhtmltopdf output: \r\n{0}\r\nCommand line: {1} {2}", error, process.StartInfo.FileName, process.StartInfo.Arguments));
                                    }
                                }
                                else
                                {
                                    if (!process.HasExited)
                                    {
                                        process.Kill();
                                    }

                                    throw new PdfConvertTimeoutException();
                                }
                            }
                            finally
                            {
                                process.OutputDataReceived -= outputHandler;
                                process.ErrorDataReceived  -= errorHandler;
                            }
                        }
                }

                if (woutput.OutputStream != null)
                {
                    using (Stream fs = new FileStream(outputPdfFilePath, FileMode.Open))
                    {
                        byte[] buffer = new byte[32 * 1024];
                        int    read;

                        while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            woutput.OutputStream.Write(buffer, 0, read);
                        }
                    }
                }

                if (woutput.OutputCallback != null)
                {
                    byte[] pdfFileBytes = File.ReadAllBytes(outputPdfFilePath);
                    woutput.OutputCallback(document, pdfFileBytes);
                }
            }
            finally
            {
                if (delete && File.Exists(outputPdfFilePath))
                {
                    File.Delete(outputPdfFilePath);
                }

                foreach (var tmpFile in workEnv.TempFiles)
                {
                    if (File.Exists(tmpFile))
                    {
                        File.Delete(tmpFile);
                    }
                }
            }
        }
コード例 #2
0
ファイル: PdfConvert.cs プロジェクト: ajsjunior/wkhtmltopdf-1
 /// <summary>
 /// Converts one or more HTML pages into a PDF document.
 /// </summary>
 /// <param name="document">Document definitions.</param>
 /// <param name="output">Output definitions.</param>
 public static void ConvertHtmlToPdf(PdfDocument document, PdfOutput output)
 {
     ConvertHtmlToPdf(document, null, output);
 }