locked
Convert HTML string to PDF with ITextSharp RRS feed

  • Question

  • User-1620559168 posted

    I am trying to convert a HTML string to pdf using the ITextSharp.dll and ITextSharp.XMLWorker.dll.

    When using an overloaded function I am getting following error:

    Value of type 'iTextSharp.text.Document' cannot be converted to 'System.IO.Stream'.
    Value of type 'System.IO.StringReader' cannot be converted to 'System.Text.Encoding'.

    Here is my Code:

    Private Sub test(ByVal html As String)
            Dim strHtml As String
            Dim memStream As New MemoryStream()
    
            strHtml = html
    
            Dim strFileShortName As String = "test" & DateTime.Now.Ticks & ".pdf"
            Dim strFileName As String = HttpContext.Current.Server.MapPath("~/invoices/" & strFileShortName)
            Dim docWorkingDocument As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate(), 1, 1, 0, 0)
            Dim srdDocToString As StringReader = Nothing
            Try
                Dim pdfWrite As PdfWriter
                pdfWrite = PdfWriter.GetInstance(docWorkingDocument, New FileStream(strFileName, FileMode.Create))
                srdDocToString = New StringReader(strHtml)
                docWorkingDocument.Open()
                XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, docWorkingDocument, srdDocToString)
            Catch ex As Exception
                Response.Write(ex.Message)
            Finally
                If Not docWorkingDocument Is Nothing Then
                    docWorkingDocument.Close()
                End If
                If Not srdDocToString Is Nothing Then
                    srdDocToString.Close()
                    srdDocToString.Dispose()
                End If
            End Try
        End Sub

    Any help much appreciated!

    Thursday, June 19, 2014 8:06 AM