【问题标题】:How to use and improve wakhtmltopdf performance?如何使用和提高 wkhtmltopdf 性能?
【发布时间】:2015-02-08 10:32:21
【问题描述】:

我有我的 aspx 页面,并且在代码中编写了一些逻辑来绑定 aspx 页面的数据。现在使用 wkhtmltopdf 我发送这些文件以转换为 pdf 文件。当数据较小时它工作得很好。但是,当该页面的数据较大时,wkhtmltopdf 将停止工作并且不会创建任何 pdf 文件。

您能否提出解决此问题的任何方法。我尝试的是限制数据..例如,如果该控件绑定 350 条记录,我的页面上有中继器控件,我只需要 20 条记录,但如果这 20 条记录的大小很大,它也会发生同样的情况

我尝试的下一个选项是我在里面给出参数

进程 myProcess = Process.Start(startInfo); myProcess.WaitForExit(几秒);

但是还是不行

请推荐

【问题讨论】:

  • 试试这个:在浏览器中将您提供给 wkhtmltopdf 的确切结果呈现出来。如果它在那里运行良好,请将 HTML 手动保存到一个简单的 HTML 文件中,然后尝试从命令行转换它,而无需额外的命令行参数。这可能会帮助您缩小结果范围。这将有助于查看导致粘贴在某处的问题的呈现 HTML...
  • 我试过这个,我尝试先将文件转换为 html,然后我尝试将该 html 文件发送到 wkhtmltopdf 进行转换......但是发生的情况是 html 到 pdf 的渲染有时无法正常工作...

标签: asp.net wkhtmltopdf


【解决方案1】:
Process myProcess = Process.Start(startInfo);
                string error = myProcess.StandardError.ReadToEnd();
                myProcess.WaitForExit();
                myProcess.Close();
                objZip.AddFile(destinationFile, "Files");
                myProcess.Dispose();
                myProcess = null;

这就是答案 => 当你启动进程并等待 exit() 时会发生什么情况,这两者都会创建一个有时会影响性能的死锁 .. 通过在 waitforexit() 清除死锁然后继续之前添加 readtoend() 方法进一步处理...

这解决了我的问题。

【讨论】:

    【解决方案2】:

    我展示完整解决方案的原因是,wkhtmltopdf 非常适合创建动态 pdf 文件,但这也是免费工具,因此用于此的文档非常有限..

    private void ProcessHtmlToPdf(列出 lstExportToPdfCategories) { 尝试 { 字符串 pdfExportPath = string.Empty; 字符串 PDFdeletePath = string.Empty; 字符串 deletePath = string.Empty; string PdfSavePath = string.Empty;

                if (lstExportToPdfCategories != null)
                {
                    foreach (var item in lstExportToPdfCategories)
                    {
                        path = "";
                        pdfExportPath = profile + ApConfig.CurrentCompany.CompId + "/" + objExpDetails.AddedForId.ToString() + "/" + item.HeaderCategory;
                        PDFdeletePath = profile + ApConfig.CurrentCompany.CompId + "/" + objExpDetails.AddedForId.ToString();
                        PdfSavePath = profile + ApConfig.CurrentCompany.CompId + "/" + objExpDetails.AddedForId.ToString() + "/" + item.HeaderCategory;
                        htmlpath = profile + ApConfig.CurrentCompany.CompId + "/" + objExpDetails.AddedForId.ToString() + "/" + item.HeaderCategory;
    
                        deletePath = Server.MapPath(PDFdeletePath);
                        string ClearDirectory = Server.MapPath(PdfSavePath);
                        if (Directory.Exists(deletePath))
                        {
                            //Directory.Delete(ClearDirectory, true);
                            //Directory.Delete(deletePath, true);
                        }
    
    
                        if (!Directory.Exists(Server.MapPath(pdfExportPath)))
                        {
                            Directory.CreateDirectory(Server.MapPath(pdfExportPath));
                        }
                        string name =
                            pdfExportPath = pdfExportPath + "/" + objExpDetails.FirstName + "." + objExpDetails.LastName + "-" + item.HeaderCategory + "_" + (drpYear.SelectedValue != "0" ? Convert.ToString(drpYear.SelectedValue) : System.DateTime.Now.Year.ToString()) + ".pdf";
                        objpath = Server.MapPath(pdfExportPath);
    
                        //this will create html mockup 
                        //item.WebsiteUrl = CreateTemportHtmlFile(item.HeaderCategory, PdfSavePath, item.WebsiteUrl);
    
                        if (path == "")
                        {
                            path = CreateTemportHtmlFile(PdfSavePath, item.HeaderCategory);
                        }
    
                        HtmlToPdf(item.WebsiteUrl, objpath, path);
                    }
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + objExpDetails.FirstName + "." + objExpDetails.LastName + "-actusdocs.zip");
                    Response.ContentType = "application/zip";
                    objZip.Save(Response.OutputStream);
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                //SendEmail.SendErrorMail("ProcessHtmlToPdf method : ", ex.Message + ex.StackTrace, objExpDetails);
            }
        }
    
        //Method overloading
    
        //this is for testing html pages(not in used during main running)
        private string CreateTemportHtmlFile(string categoryName, string htmlMockupSavingPath, string websiteURL)
        {
            try
            {
                string sessionId = Session.SessionID;
                int employeeId = Convert.ToInt32(hdnEmployeeId.Value);
    
                htmlMockupSavingPath = Server.MapPath(htmlMockupSavingPath) + "\\" + categoryName + ".html";
                StreamWriter sw;
    
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(websiteURL);
                myRequest.Method = "GET";
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();
    
                sw = File.CreateText(htmlMockupSavingPath);
                sw.WriteLine(result);
                sw.Close();
                Response.WriteFile(htmlMockupSavingPath);
            }
    
            catch (Exception ex)
            {
                SendEmail.SendErrorMail("CreateTemportHtmlFile method : ", ex.Message + ex.StackTrace, objExpDetails);
            }
    
            return htmlMockupSavingPath;
        }
    
        private string CreateTemportHtmlFile(string PdfSavePath, string categoryName)
        {
            try
            {
                string sessionId = Session.SessionID;
                int employeeId = Convert.ToInt32(hdnEmployeeId.Value);
    
                PdfSavePath = Server.MapPath(PdfSavePath) + "\\BindHeader.html";
                htmlpath = htmlpath.Substring(1);
                htmlpath = ConfigurationManager.AppSettings["pdfUrlPath"].ToString() + htmlpath + "/BindHeader.html";
                string exportedYear = (drpYear.SelectedValue == "0" ? System.DateTime.Now.Year.ToString() : drpYear.SelectedValue);
    
                StreamWriter sw;
                if (categoryName == "MidYearAppraisal" || categoryName == "EndYearAppraisal")
                {
                    myRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["pdfUrlPath"] + "/User/UserPdfReports/UserPdfHeaderforAppraisal.aspx?session=" + sessionId + "&empId=" + employeeId + "&catName=" + categoryName + "&expYear=" + exportedYear);
                }
                else
                {
                    myRequest = (HttpWebRequest)WebRequest.Create(ConfigurationManager.AppSettings["pdfUrlPath"] + "/User/UserPdfReports/UserPdfHeader.aspx?session=" + sessionId + "&empId=" + employeeId + "&catName=" + categoryName + "&expYear=" + exportedYear);
                }
    
                myRequest.Method = "GET";
                WebResponse myResponse = myRequest.GetResponse();
                StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string result = sr.ReadToEnd();
    
                sw = File.CreateText(PdfSavePath);
                sw.WriteLine(result);
                sw.Close();
                Response.WriteFile(PdfSavePath);
            }
    
            catch (Exception ex)
            {
                SendEmail.SendErrorMail("CreateTemportHtmlFile method : ", ex.Message + ex.StackTrace, objExpDetails);
            }
    
            return htmlpath;
        }
    
        private void HtmlToPdf(string website, string destinationFile, string path)
        {
            try
            {
                string hrmlPath = ConfigurationManager.AppSettings["pdfUrlPath"].ToString() + "/user/UserPdfReports/FooterPdfReports.html";
                ProcessStartInfo startInfo = new ProcessStartInfo();
                string switches = "";
                switches += "--header-html " + path + " --footer-html " + hrmlPath;
    
                startInfo.UseShellExecute = false;
    
                startInfo.RedirectStandardOutput = true;
    
                startInfo.RedirectStandardInput = true;
    
                startInfo.RedirectStandardError = true;
    
                startInfo.CreateNoWindow = true;
    
                startInfo.FileName = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
    
                startInfo.Arguments = switches + " " + website + " " + destinationFile;
    
                Process myProcess = Process.Start(startInfo);
                string error = myProcess.StandardError.ReadToEnd();
                myProcess.WaitForExit();
                myProcess.Close();
                objZip.AddFile(destinationFile, "Files");
                myProcess.Dispose();
                myProcess = null;
    
            }
            catch (Exception ex)
            {
                //SendEmail.SendErrorMail("HtmlToPdf : ", ex.Message + ex.StackTrace, objExpDetails);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多