Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vb.net - iText7 Performance Issue Compared With iTextSharp

I have tested iTextsharp and iText7 for HTML to PDF conversion. Based on the performance iTextsharp is taking 3 minutes for 10000 PDF creation. But iText7 taking 17 minutes for 10000 PDF creation. Since iText7 is new Version compared to iTextsharp,i decided to use iText7 for Commercial Purpose. But Performance wise iText7 is Low.So Please Tell me How to improve performance of HTML to PDF conversion in iText7?

Testing in iText7

  For i As Integer = 0 To 10000 
        HTML = ReadFile '=> Read HTML file from particular location
        'HTML = Replace(HTML) => To Replace the content dynamically
         Dim writer As PdfWriter
          Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes("a")
          writer = New PdfWriter(FileName, New WriterProperties().SetStandardEncryption(array, array, EncryptionConstants.ALLOW_PRINTING,
                            EncryptionConstants.ENCRYPTION_AES_256))
           HtmlConverter.ConvertToPdf(HTML, writer)
    Next

Testing In iTextSharp

   Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.pdfa
Imports System.IO
Imports iTextSharp.text.html.simpleparser
Imports System.Text
Imports iTextSharp.tool.xml.html
Imports iTextSharp.tool.xml
Imports iTextSharp.tool.xml.pipeline.html

 For i As Integer = 0 To 10000
    HTML = ReadFile '=> Read HTML file from particular location
        'HTML = Replace(HTML) => To Replace the content dynamically
    Dim bPDF As Byte()
        Dim ms As New MemoryStream
        Dim doc As Document
        doc = New Document(PageSize.A4, 25, 25, 25, 25)
        Dim txtReader As New StringReader(Html)   
        Dim oPdfWriter As PdfWriter
        oPdfWriter = PdfWriter.GetInstance(doc, ms)
        oPdfWriter.SetEncryption(iTextSharp.text.pdf.PdfWriter.ENCRYPTION_AES_128, "q", "a", 2)
        Dim htmlWorker As New HTMLWorker(doc)       
        doc.Open()
        htmlWorker.StartDocument()      
        htmlWorker.Parse(txtReader)
        htmlWorker.EndDocument()
        htmlWorker.Close()
        doc.Close()
        bPDF = ms.ToArray()
        Dim FIleName As String = "D:ItextSharp_" & Now.ToString("ddMMyyyyHHMMssffffff") & ".pdf"
        File.WriteAllBytes(FIleName, bPDF)
Next



Function ReadFile()
        Dim stringReader As String = ""
        Dim objReader As New System.IO.StreamReader("D:AS1-RevampTestHTMLest.html")
        Do While objReader.Peek() <> -1
            stringReader = stringReader & objReader.ReadLine() & vbNewLine
        Loop
        ReadFile = stringReader
End Function

I used the above Code to test performance...iText7 Tacking More time to place the pdf file in mentioned Path Compared to iTextSharp.

EDIT: copy/paste of the HTML in that other question:

Based on My Question in the path iText7 Performance Issue Compared With iTextSharp I Have Sent HTML File For MR.Amedee Van Gasse. So Please Tell me How to Improve Performance of iText7..

<div id = "headerdiv" style="width:540px; float:left; background:#ededed; padding:30px; overflow:hidden;">
<br>
<br>
<br>
<div>
<img border='0' src='D:AS1-RevampTestHTML
ewlog.bmp' width='100' height='40'>
</div>
<p style="color:Red;align=center;" >                         Details</p>
<br>
<br>
<table >
<tr  border='0'>
<td  bgcolor='Green'>
<font size="3" color="white">
SDetails
</font>
</td>
</td>
</tr>
<tr border='0'>
<td>
<div id="dvKYC">
<table  border='1'>

<tr>
<td><#lsName#></td>
<td>No:<#lsno#></td>
</tr> 

<tr  border='1'>
<td width=500><#lsAddess#></td>
<td></td>
</tr>

<tr>
<td><#lsContacts#></td>
<td> </td>
</tr> 
</table>
</div>
</td>
</tr>
</table>

<br>

<div >
<table >
<tr  border='0'>
<td  bgcolor='Green'>
<font size="3" color="white">
Status
</font>
</td>
</td>
</tr>
</table>
<table style="width:100%;">
<tr  bgcolor=gray >
<td style="width:30%;text-align: left; font-weight: bold;">UUH  </td>
<td style="width:20%;text-align: left; font-weight: bold;">PN</td>
<td style="width:20%;text-align: left; font-weight: bold;">KC </td>
<td style="width:20%;text-align: left; font-weight: bold;">CC</td>
</tr>
<tr>
<td  style"width:200px;"><#lsHs#></td>
<td ><#lsPN#></td>
<td><#lsKC#></td>
<td><#lsCC#></td>
</tr>
</table>
 </div>



<div >
<table >
<tr  border='0'>
<td  bgcolor='Green'>
<font size="3" color="white">
STD
</font>
</td>
</td>
</tr>
</table>


 <##TT##>


</div>

After i have Applied Following code two Error Comes in ConverterProperties

1.setCreateAcroForm is not a member of iText.Html2pdf.ConverterProperties

2.setOutlineHandler is not a member of iText.Html2pdf.ConverterProperties

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
           Dim converterProperties As ConverterProperties = New ConverterProperties
            With converterProperties
                .SetBaseUri(".")
                .setCreateAcroForm(False)
                .SetCssApplierFactory(New DefaultCssApplierFactory())
                .SetFontProvider(New DefaultFontProvider())
                .SetMediaDeviceDescription(MediaDeviceDescription.CreateDefault())
                .setOutlineHandler(New OutlineHandler())
                .SetTagWorkerFactory(New DefaultTagWorkerFactory())
            End With
Dim HTML = ReadFile("Input_Template")
            For i = 0 To 10000
                LicenseKey.LoadLicenseFile("C:iText7itextkey-0.xml")
                Dim PDF = "E:iTextestpdf " & i & ".pdf"
                Dim m As New MemoryStream
                Dim writer As PdfWriter
                Dim array() As Byte = System.Text.Encoding.ASCII.GetBytes("a")
                writer = New PdfWriter(PDF, New WriterProperties().SetStandardEncryption(array, array, EncryptionConstants.ALLOW_PRINTING,
                                  EncryptionConstants.ENCRYPTION_AES_256))
                HtmlConverter.ConvertToPdf(HTML, writer, converterProperties)
            Next
        End Sub

If i Comment That two Lines of code and running my program an Error comes in the line of converter Code i.e(HtmlConverter.ConvertToPdf(HTML, writer, converterProperties))

The Error is:"Pdf indirect object belongs to other PDF document. Copy object to current pdf document."

since coverterproperties is in out of loop this error comes. if i put this all properties within the loop it works fine...but is this correct for performance wise..?

Please Help me for these Three Errors..?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The answer to your question is simple: at iText Group, we are constantly improving the iText software, and there is certainly room for improving the performance. However, we won't ever be able to make the pdfHTML add-on as fast as the obsolete HTMLWorker. The reason is simple: HTMLWorker didn't support CSS, HTMLWorker only supported a small selection of tags, and so on... HTMLWorker was very simple and was only to be used for simple needs.

We have created the pdfHTML add-on to support CSS (including functionality to add headers, footer, page numbers, etc...). We support plenty of HTML tags that weren't supported in HTMLWorker. We support absolute positioning of elements in pdfHTML. All of this functionality comes with a cost. That cost is CPU.

It is intellectually unfair of you to compare the CPU use by HTMLWorker with the CPU use by pdfHTML.

This being said: you can already save plenty of time by using ConverterProperties. Right now, you don't provide any ConverterProperties. This means that iText has to instantiate the default properties for every PDF you are creating. If you would create the ConverterProperties up-front, and reuse them, you could already save plenty of time, but you have to understand that the extra functionality provided by pdfHTML comes with a cost in CPU.

This is how you create a ConverterProperties instance:

ConverterProperties converterProperties = new ConverterProperties()
    .setBaseUri(".")
    .setCreateAcroForm(false)
    .setCssApplierFactory(new DefaultCssApplierFactory())
    .setFontProvider(new DefaultFontProvider())
    .setMediaDeviceDescription(MediaDeviceDescription.createDefault())
    .setOutlineHandler(new OutlineHandler())
    .setTagWorkerFactory(new DefaultTagWorkerFactory());

As you can see, we create plenty of default objects: the default CCS Applier factory, the default font provider, the default media description, the default outline handler, and the default tag worker factory. The creation of all of these objects costs a tiny little bit of time, but when you multiply that time by 10,000 because you create 10,000 documents, the CPU needed to create those default objects can become significant, and that what happens when you convert an HTML file to PDF like this:

HtmlConverter.convertToPdf(
    new FileInputStream("resources/test.html"),
    new FileOutputStream("results/test.pdf"));

Since you are not adding a ConverterProperties parameter, iText will create a new instance of ConverterProperties internally for every document that you convert. All the default components of the ConverterProperties will be null, which means that for every document you create new instances of the CSS Applier factory, the font provider, etc... need to be created.

It will save you some time (but not that much) if you create a ConverterProperties up-front (only once), as well as all the components. It is then important that you reuse that object when converting HTML to PDF:

HtmlConverter.convertToPdf(
    new FileInputStream("resources/test.html"),
    new FileOutputStream("results/test.pdf"),
    converterProperties);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...