HTML to PDF Converter

In any of the business application a scenario comes when html file is available
and we need to covert that html file in PDF format

i.e.  if we have provided invoice template to user for modification and we need to create a final PDF as final result based on that provided html template then we need to required logic/code to convert html file or string in PDF file

So below code main targeting to resolve above problem
statement. I have implemented below code which is convert html file in PDF by
iTextSharp.

Below are the code for this. (VB.NET)

Private Sub CovertHTMLTOPDF()
        Dim htmlstr As StreamReader = New StreamReader("Enter your html files physical path")
        Dim strline As String

        strline = htmlstr.ReadToEnd
        htmlstr.Close()

        ' Code to convert to pdf
        Dim doc As New Document(PageSize.A4, 8, 80, 50, 30, 65)
        Dim fsNew As New StringReader(strline)
        Dim document As New Document(PageSize.A4, 80, 50, 30, 65)

        Using ofilestream As New FileStream(Request.PhysicalApplicationPath + "Output related PDF file", FileMode.Create)
            PdfWriter.GetInstance(document, ofilestream)
            Using stringReader As New StringReader(strline)
                Dim parsedList As New ArrayList()
                parsedList = html.simpleparser.HTMLWorker.ParseToList(stringReader, Nothing)
                document.Open()
                ' parse each html object and add it to the pdf document
                For Each item As Object In parsedList
                    document.Add(DirectCast(item, IElement))
                Next

                document.Close()

            End Using

        End Using
    End Sub

Please insert a comment if you required any more clarification.

Thanks,
Amit Patel
“Enjoy Programming”
amitpatel.it@gmail.com

About these ads
Follow

Get every new post delivered to your Inbox.

Join 239 other followers