Question HTML to PDF

Stormspace

New member
Joined
Feb 24, 2012
Messages
3
Programming Experience
3-5
I have written a small and simple VB.net app to read in names and data from a spreadsheet and output the result as an attendance certificate. The certificate is currently output as an HTML file and attached to an e-mail and mailed via gmail. I'd like to convert this html doc/string to a PDF. The current HTML string contains quite a bit of complex CSS to make the certificate layout work, so I need a solution that can handle complex HTML documents/strings. I've tried Winnovative's solution and it works great, however since this project is for a small non-profit we don't have the 650-850 dollars for a license. Are there any free options other than writing my own PDF conversion utility?
 
There are several free .Net Pdf libraries (web search). iTextSharp for example I see have some html support, but haven't tried that myself.
Another option that may save you lots of work is to render the html in WebBrowser control and print from there to a Pdf printer (web search that too, several free).
 
I've tried iTextSharp and while it works with simple HTML documents, it does not work with those that use CSS. I've googled quite a bit and iTextsharp is the only free option I've found, so I was asking in case I had somehow overlooked one.
 
While I was looking for a way to convert HTML to a PDF, the ultimate goal was to create a PDF copy of a certificate customized for each attendee and e-mailed to them. I discovered a way to use Office 2010, specifically Word 2010, to do the conversion as long as I started with a Doc file. (Note: The same can be done with an HTML file, however Word doesn't always retain all the formatting of the original HTML document.

VB.NET:
Public Sub makePDF(ByVal fName As String, ByVal lName As String, ByVal companyName As String, _
                       ByVal strDate As String, ByVal strCity As String, ByVal strOrganization As String, ByVal strState As String, ByVal strPresident As String)
        On Error GoTo makePDFerr
        Dim docLocation As String = App_Path() & "\Certificate.doc"
        Dim pdfFile As String = App_Path() & "\Certificate.pdf"


        'Write out DOC to the PDF File using office 2010
        '***************************************************************************************************


        'Clear the last PDF
        Kill(App_Path() & "\Certificate.pdf")


        Dim wordApp As New Microsoft.Office.Interop.Word.Application
        Dim wordDoc As New Microsoft.Office.Interop.Word.Document
        Dim oFalse As Object = False
        Dim oTrue As Object = True
        Dim oNothing As Object = Nothing


        If System.IO.File.Exists(docLocation) = False Then
            MsgBox("The word doc file " & Chr(34) & docLocation & Chr(34) & " does not exist.")
            End
        End If


        wordApp = New Microsoft.Office.Interop.Word.Application
        wordApp.Visible = False

        Dim doWord = wordApp.Documents.Open(docLocation, oFalse, oTrue)

        'Find and Replace routine
        '*******************************************************************
        doWord.Content.Find.Execute(FindText:="<<First_Name>>", ReplaceWith:=StrConv(fName, VbStrConv.Uppercase), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        doWord.Content.Find.Execute(FindText:="<<Last_Name>>", ReplaceWith:=StrConv(lName, VbStrConv.Uppercase), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        doWord.Content.Find.Execute(FindText:="<<date>>", ReplaceWith:=strDate, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        doWord.Content.Find.Execute(FindText:="<<companyOrganization>>", ReplaceWith:=companyName, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        doWord.Content.Find.Execute(FindText:="<<city>>", ReplaceWith:=StrConv(strCity, VbStrConv.ProperCase), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        doWord.Content.Find.Execute(FindText:="<<state>>", ReplaceWith:=StrConv(strState, VbStrConv.Uppercase), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        doWord.Content.Find.Execute(FindText:="<<president>>", ReplaceWith:=StrConv(strPresident, VbStrConv.ProperCase), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
        '*******************************************************************
        doWord.ExportAsFixedFormat(pdfFile, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF)
        doWord.Close(oFalse, oNothing, oNothing)
        wordApp.Quit()
        '***************************************************************************************************
        Exit Sub
makePDFerr:
        Select Case Err.Number
            Case Else
                MsgBox(Err.Number & " " & Err.Description)
        End Select
        End
    End Sub
 
Back
Top