2

hkhtmltopdf を使用して PDF を生成する方法があります。

public string GeneratePdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls,
            string[] options,
            string pdfHtmlToPdfExePath)
        {
            string urlsSeparatedBySpaces = string.Empty;
            try
            {
                //Determine inputs
                if ((urls == null) || (urls.Length == 0))
                    throw new Exception("No input URLs provided for HtmlToPdf");
                else
                    urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs

                //string outputFolder = pdfOutputLocation;
                string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name

                var p = new System.Diagnostics.Process();

                p.StartInfo.FileName = pdfHtmlToPdfExePath;
                p.StartInfo.Arguments = ((options == null) ? "" : String.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename;
                p.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none
                p.StartInfo.WorkingDirectory = string.Concat(HttpContext.Current.Server.MapPath(""), "\\", pdfOutputLocation, "\\");
                p.Start();

                // read the output here...
                var output = p.StandardOutput.ReadToEnd();
                var errorOutput = p.StandardError.ReadToEnd();

                // ...then wait n milliseconds for exit (as after exit, it can't read the output)
                p.WaitForExit(600000);

                // read the exit code, close process
                int returnCode = p.ExitCode;
                p.Close();


                // if 0 or 2, it worked so return path of pdf
                if ((returnCode == 0) || (returnCode == 2))
                {
                    linkPdfDownload = pathPdf + outputFilename;

                    return outputFilename;
                }
                else
                    throw new Exception(errorOutput);
            }
            catch (Exception ex)
            {
                throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, ex);
            }
        }

この方法は問題なく、必要な PDF が生成されます。しかし、PDFのすべてのページにフッターを追加する必要があるため、この質問を見つけて実行しようとしました。上記のコードは、次のコマンド ラインを呼び出しています。

wkhtmltopdf.exe -T 50mm --footer-html http://localhost:5001/HTML/cabecalho.html http://localhost:5001/HTML/arquivo56784325.html Modelo__2013-10-31-01-53-46-757.PDF

上記のコードからこれを呼び出すと、永久に実行され、プロセスを停止すると、フォルダーに (HTML が適用されていない) 不正な形式の PDF が生成されます。それ以外の場合は、そのコマンドをコマンド ラインから直接呼び出すと、正しい PDF が 10 秒以内に生成されます。

その方法で何が間違っている可能性がありますか? どうもありがとうございました。

4

0 に答える 0