如何使用和改进wakhtmltopdf性能? [英] How to use and improve wakhtmltopdf performance?

查看:326
本文介绍了如何使用和改进wakhtmltopdf性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的aspx页面,而一些逻辑是写在code后面使用wkhtmltopdf ASPX pages.Now的数据绑定我发送这些文件​​转换成PDF files.Its很好地工作当数据规模较小但是当数据进入更大方该页面的wkhtmltopdf停止工作,犯规创建任何PDF文件。

I have my aspx pages , and some logic is written in code behind to bind the data of aspx pages.Now using wkhtmltopdf i am sending these files to convert into pdf files.Its work very well when the data is smaller in size however when the data comes in larger side for that page the wkhtmltopdf stops working and doesnt create any pdf file.

您能否提供任何方法来克服这个问题。我有我的尝试是限制了数据。例如中继器控制我的网页上,如果该控件绑定350记录我只取20条记录,而且如果这些20条记录尺寸很大碰巧相同

Can you suggest any way to overcome this problem. What i tried was limiting the data.. for example i have repeater control on my page if that controls binds 350 record i am only taking 20 records , but also if size of those 20 records are large it happens the same

在也下一个选项我想是我给的参数里面

the also next option i tried is my giving the parameter inside

工艺myProcess =的Process.Start(StartInfo的);
                myProcess.WaitForExit(几秒钟);

Process myProcess = Process.Start(startInfo); myProcess.WaitForExit(few seconds);

但它仍然不工作

请提示

推荐答案

的原因,我出完整的解决方案是,wkhtmltopdf是创建动态的PDF文件非常好,但是这是免费的工具,以及和因而非常有限的文件这个..

The reason i am showing complete solution is that , wkhtmltopdf is very good for creating dynamic pdf files , but this is free tool as well and hence very limited documentation for this ..

私人无效ProcessHtmlToPdf(名单lstExportToPdfCategories)
        {
            尝试
            {
                字符串pdfExportPath =的String.Empty;
                字符串PDFdeletePath =的String.Empty;
                字符串deletePath =的String.Empty;
                字符串PdfSavePath =的String.Empty;

private void ProcessHtmlToPdf(List lstExportToPdfCategories) { try { string pdfExportPath = string.Empty; string PDFdeletePath = string.Empty; string 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);
        }
    }

这篇关于如何使用和改进wakhtmltopdf性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆