How to use and improve wakhtmltopdf performance?

I have my aspx pages, and some logic is written in code to bind data to aspx pages. Now using wkhtmltopdf I am sending these files for conversion to pdf files. It works great when the data is smaller, however, when the data comes from the larger side for this page, wkhtmltopdf stops working and does not create any pdf file.

Can you suggest any way to overcome this problem. I tried to limit the data. For example, I have a repeater control on my page if this control links 350 entries. I only deal with 20 records, but also if the size of these 20 records is large, it happens the same

also the next parameter i tried is my parameter containing inside

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

but it still does not work

Please suggest

0
source share
2 answers
Process myProcess = Process.Start(startInfo);
                string error = myProcess.StandardError.ReadToEnd();
                myProcess.WaitForExit();
                myProcess.Close();
                objZip.AddFile(destinationFile, "Files");
                myProcess.Dispose();
                myProcess = null;

This is the answer => what happens when you start the process and wait for exit (), both create a dead end that clogs performance .. by adding the readtoend () method before waitforexit () clears the dead end and then continues further processing ...

this solves my problem.

0
source

, , , wkhtmltopdf pdf , , , .

private void ProcessHtmlToPdf ( lstExportToPdfCategories)       {                      {               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);
        }
    }
0

Source: https://habr.com/ru/post/1546567/


All Articles