Performance Issue for HTML to PDF Conversion using Apose PDF

Hi,


we are using a lincesed version Aspose PDF for HTML to PDF conversion. For this conversion it is taking, in most of the cases, more than 1 minute, which is not acceptable for day to day usage.

Following the source code we are using for HTML to PDF conversion.

Aspose.Pdf.License license = new Aspose.Pdf.License();
license.SetLicense("d:\\xxxxx\\Aspose.Pdf.lic");
// Instantiate an object PDF class
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
// add the section to PDF document sections collection
Aspose.Pdf.Generator.Section section = pdf.Sections.Add();
// Create Header Section of the document
Aspose.Pdf.Generator.HeaderFooter header = new Aspose.Pdf.Generator.HeaderFooter(section);

//Create Footer Section of the document
Aspose.Pdf.Generator.HeaderFooter footer = new Aspose.Pdf.Generator.HeaderFooter(section);

Aspose.Pdf.Generator.MarginInfo marginInfo = new Aspose.Pdf.Generator.MarginInfo();
marginInfo.Left = 30;
section.PageInfo.Margin = marginInfo;
// set the Odd Footer for the PDF file
section.OddFooter = footer;
// Set the Even Footer for the PDF file
section.EvenFooter = footer;
// set the Odd header for the PDF file
section.OddHeader = header;
// Set the Even Header for the PDF file
section.EvenHeader = header;
Aspose.Pdf.Generator.Text txt = new Aspose.Pdf.Generator.Text(section, headerText.ToString());
Aspose.Pdf.Generator.Text txtFooter = new Aspose.Pdf.Generator.Text(section, FooterText.ToString());
//txt.IfHtmlTagSupportedOverwriteHtmlFontNames = true;
txt.UseTextInfoStyle = true;
txt.Margin.Bottom = 5;
txt.Margin.Top = 50;
txt.Margin.Left = 1;
txt.TextInfo.FontSize = 9;
txt.TextInfo.IsTrueTypeFontBold = true;
txt.TextInfo.FontName = "Arial";


txtFooter.Margin.Bottom = 5;
txtFooter.Margin.Top = 20;
txtFooter.Margin.Left = 30;
txtFooter.TextInfo.FontSize = 8;
txtFooter.TextInfo.IsTrueTypeFontBold = true;
txtFooter.TextInfo.FontName = "Arial";


// Add text to Header section of the Pdf file
header.Paragraphs.Add(txt);

// Add text to Footer section of the Pdf file
footer.Paragraphs.Add(txtFooter);

// Read the contents of HTML file into StreamReader object
StreamReader r = File.OpenText(strTemplateRead);
//Create text paragraphs containing HTML text
Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
// enable the property to display HTML contents within their own formatting
text2.IsHtmlTagSupported = true;
//Add the text paragraphs containing HTML text to the section
section.Paragraphs.Add(text2);

Context.Response.ClearHeaders();
Context.Response.ContentType = "application/pdf";
Context.Response.Clear();
Context.Response.AppendHeader("Content-Disposition", "attachment");
pdf.Save("NewDoc.pdf", Apdf.SaveType.OpenInBrowser, Context.Response);
r.Close();
Context.Response.Close();

Also attaching HTML for sample.

Saw lot of post on performance issues for HTML to PDF conversion.
Please suggest how it could be improved.

Thanks
Dinesh

Hi Dinesh,


Thanks for using our products.

I am afraid I cannot find any HTML with your earlier post.

I have tested the scenario using Aspose.Pdf for .NET 8.5.0 where I have used the attached HTML file and as per my observations, the process to convert HTML file to PDF format using VisualStudio 2010 application running over Windows 7(x64) is taking 3.4 seconds. Can you please again try sharing the HTML file so that we can test the scenario at our end. We are sorry for this inconvenience.


PS, please rename the attached file from .txt to .html

Hi,

We are still getting performance issues with the following error when converting HTML to PDF:

Logging started

29/10/2013 2:49:50 PM ReportsPDF - ExportToPDF - : Template Read/ARMS/HTMFiles/rptIndex_of_Organisation_And_Agency_Names_read.htm

System.NullReferenceException

Object reference not set to an instance of an object.

at  [1](String, Pdf, Section , Text )

at  [1](Pdf , Section , Cell , Text , HeaderFooter ,  [1] , ‹ )

at  [1](Pdf , Section , Table , Cell , Text , HeaderFooter ,  [1] , ‹ )

at  [1](Pdf , Section , HeaderFooter , Table , Row , Cell , Text ,  [1] , ‹ , Boolean )

at  [1](Pdf , Section ,  [1] )

at  [1](Pdf )

at  [1](, Pdf )

at Aspose.Pdf.Generator.Pdf.Save(String fileName, SaveType saveType, HttpResponse response)

at ArmsRepository.ARMSReportHelper.ExportToPDF(String readTemplate, HttpResponse response, String[] sHeaderFooter, String sReportNumber) in E:\ArMS_Bh\ECS.ArMS.Repository\ARMSReportHelper.cs:line 3734

Logging ended

Logging started

29/10/2013 2:49:50 PM ReportsPDF - ReportGridView_RowCommand

System.NullReferenceException

Object reference not set to an instance of an object.

at  [1](String, Pdf, Section , Text )

at  [1](Pdf , Section , Cell , Text , HeaderFooter ,  [1] , ‹ )

at  [1](Pdf , Section , Table , Cell , Text , HeaderFooter ,  [1] , ‹ )

at  [1](Pdf , Section , HeaderFooter , Table , Row , Cell , Text ,  [1] , ‹ , Boolean )

at  [1](Pdf , Section ,  [1] )

at  [1](Pdf )

at  [1](, Pdf )

at Aspose.Pdf.Generator.Pdf.Save(String fileName, SaveType saveType, HttpResponse response)

at ArmsRepository.ARMSReportHelper.ExportToPDF(String readTemplate, HttpResponse response, String[] sHeaderFooter, String sReportNumber) in E:\ArMS_Bh\ECS.ArMS.Repository\ARMSReportHelper.cs:line 3785

at ArmsRepository.ArMSRepository.ExportToPDF(String readTemplate, HttpResponse response, String[] sHeaderFooter, String sReportNumber) in E:\ArMS_Bh\ECS.ArMS.Repository\ArMSRepository.cs:line 409

at ECS.ArMS.ReportsPdf.ReportGridView_RowCommand(Object sender, GridViewCommandEventArgs e) in E:\ArMS_Bh\ECS.ArMS\ReportsPdf.aspx.cs:line 197

Logging ended

The code which we are using for HTML to PDF conversion is:

public static void ExportToPDF(string readTemplate, HttpResponse response, String[] sHeaderFooter, string sReportNumber)
{
    StreamReader r = File.OpenText(readTemplate);
    try
    {
        Aspose.Pdf.License license = new Aspose.Pdf.License();
        license.SetLicense(System.Web.HttpContext.Current.Server.MapPath("bin/Aspose.Pdf.lic"));

        // Instantiate an object PDF class
        Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();

        // add the section to PDF document sections collection
        Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

        // Create Header Section of the document
        Aspose.Pdf.Generator.HeaderFooter header = new Aspose.Pdf.Generator.HeaderFooter(section);
        // set the Odd header for the PDF file
        section.OddHeader = header;
        // Set the Even Header for the PDF file
        section.EvenHeader = header;

        // add the section to PDF document sections collection
        Aspose.Pdf.Generator.Section section1 = pdf.Sections.Add();

        // Create Footer Section of the document
        Aspose.Pdf.Generator.HeaderFooter footer = new Aspose.Pdf.Generator.HeaderFooter(section1);

        // set the Odd footer for the PDF file
        section.OddFooter = footer;
        // Set the Even footer for the PDF file
        section.EvenFooter = footer;

        // add the section to PDF document sections collection
        Aspose.Pdf.Generator.Section section2 = pdf.Sections.Add();

        Aspose.Pdf.Generator.Text txt = new Aspose.Pdf.Generator.Text(section, sHeaderFooter[0].ToString());
        Aspose.Pdf.Generator.Text txtFooter = new Aspose.Pdf.Generator.Text(section1, sHeaderFooter[1].ToString());

        txt.Margin.Top = 50;
        txt.Margin.Left = 8;
        txt.TextInfo.FontSize = 9;
        txt.TextInfo.IsTrueTypeFontBold = true;
        txt.TextInfo.FontName = "Arial";

        txtFooter.Margin.Bottom = 5;
        txtFooter.Margin.Top = 20;
        txtFooter.Margin.Left = 30;
        txtFooter.TextInfo.FontSize = 8;
        txtFooter.TextInfo.IsTrueTypeFontBold = true;
        txtFooter.TextInfo.FontName = "Arial";

        // Add text to footer section of the Pdf file
        footer.Paragraphs.Add(txtFooter);
        // Add text to Header section of the Pdf file
        header.Paragraphs.Add(txt);

        //Create text paragraphs containing HTML text
        Aspose.Pdf.Generator.Text text2 = new Aspose.Pdf.Generator.Text(section, r.ReadToEnd());
        // enable the property to display HTML contents within their own formatting
        //Start : Added the property to bind the Fontsize & Name (Rajesh.L)
        text2.IsHtmlTagSupported = true;
        text2.TextInfo.FontSize = 8;
        text2.TextInfo.FontName = "Arial";
        text2.TextInfo.IsTrueTypeFontBold = true;
        text2.IfHtmlTagSupportedOverwriteHtmlFontNames = true;
        text2.IfHtmlTagSupportedOverwriteHtmlFontSizes = true;

        if (string.Compare(sReportNumber, "07") == 0)
        {
            text2.TextInfo.IsTrueTypeFontBold = false;
        }

        if (string.Compare(sReportNumber, "05") == 0 || string.Compare(sReportNumber, "01") == 0
                        || string.Compare(sReportNumber, "02") == 0 || string.Compare(sReportNumber, "03") == 0
                        || string.Compare(sReportNumber, "04") == 0 || string.Compare(sReportNumber, "06") == 0
                        || string.Compare(sReportNumber, "08") == 0 || string.Compare(sReportNumber, "10") == 0
                        || string.Compare(sReportNumber, "11") == 0 || string.Compare(sReportNumber, "13") == 0
                        || string.Compare(sReportNumber, "14") == 0 || string.Compare(sReportNumber, "15") == 0
                        || string.Compare(sReportNumber, "16") == 0 || string.Compare(sReportNumber, "17") == 0
                        || string.Compare(sReportNumber, "19") == 0 || string.Compare(sReportNumber, "20") == 0)
        {
            text2.TextInfo.FontSize = 9;
        }

        //End : Added the property to bind the Fontsize & Name (Rajesh.L)

        //Add the text paragraphs containing HTML text to the section
        section.Paragraphs.Add(text2);
        response.ClearHeaders();
        response.ContentType = "application/pdf";
        response.Clear();
        response.AppendHeader("Content-Disposition", "attachment");
        r.Close();
        pdf.Save("NewDoc.pdf", Aspose.Pdf.SaveType.OpenInBrowser, response);
        response.Close();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    catch (NotSupportedException notsupex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : Not Supported Exception :", notsupex).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (UnauthorizedAccessException unauthex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template :Unauthorized access Exception :", unauthex).WriteLog(HttpContext.Current.User.Identity.Name);
    }

   catch (DirectoryNotFoundException direx)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : Directory not found Exception :", direx).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (ArgumentNullException argnull)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : Arguments Null Exception :", argnull).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (ArgumentOutOfRangeException argsoorex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : Arguments Out of range Exception :", argsoorex).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (ArgumentException argex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : Arguments Exception :", argex).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (PathTooLongException pathex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : IO Path Too long Exception :", pathex).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (HttpException httpex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - Template : Http Exception :", httpex).WriteLog(HttpContext.Current.User.Identity.Name);
    }
    catch (Exception ex)
    {
        new CustomExceptionHandler("ReportsPDF - ExportToPDF - : Template Read" + readTemplate, ex).WriteLog(HttpContext.Current.User.Identity.Name);
        throw;
    }
    finally
    {
        wordHeaderText.Remove(0, wordHeaderText.Length);
        excelHeaderText.Remove(0, wordHeaderText.Length);
        headerText.Remove(0, wordHeaderText.Length);
    }
}

Hi Luke,

Thanks for sharing the details.

I have tested the scenario and have observed that the conversion process suddenly terminates and the resultant PDF is 0KB. I am afraid I am unable to notice any exception message in the output console. For the sake of correction, I have separately logged it in our issue tracking system as PDFNEWNET-35980. We will investigate this issue in detail and will keep you updated on the status of a correction.

We apologize for your inconvenience.

The issues you have found earlier (filed as PDFNEWNET-35979;PDFNEWNET-35980) have been fixed in Aspose.Pdf for .NET 9.5.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi Luke,


Thanks for your patience. Your reported issue has been fixed in new DOM approach of HTML to PDF conversion. Please check following documentation link for details and code snippet.


Please feel free to contact us for any further assistance.

Best Regards,