ASP.NET HTML String to PDF



Best library is select.pdf compared to itextsharp.

Install the package through nuget. but still it will left one file 'Select.Html.dep' which is required for converting html string to pdf. Download it manually from below link. Extract zip file and copy the Select.Html.dep to your bin directory.

How to fix: Conversion failure. Could not find ‘Select.Html.dep’


http://selectpdf.com/download/SelectPdf-HtmlToPdf-v2.4.0.zip



Code to convert inner html inside panel to PDF is as follows:
Imports SelectPdf
Imports System.IO

Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        Dim sw As New StringBuilder()
        Dim tw As New StringWriter(sw)
        Dim hw As New HtmlTextWriter(tw)
        pnlPrint.RenderControl(hw)
        Dim html = sw.ToString()
        ' instantiate a html to pdf converter object
        Dim converter As New HtmlToPdf()

        ' set converter options
        Dim pdf_page_size As String = "A4"
        Dim pageSize As PdfPageSize = DirectCast([Enum].Parse(GetType(PdfPageSize), pdf_page_size, True), PdfPageSize)
        Dim pdfOrientation As PdfPageOrientation = DirectCast([Enum].Parse(GetType(PdfPageOrientation),
                    "Portrait", True), PdfPageOrientation)
        converter.Options.PdfPageSize = pageSize
        converter.Options.PdfPageOrientation = pdfOrientation
        'converter.Options.WebPageWidth = webPageWidth
        'converter.Options.WebPageHeight = webPageHeight
        SelectPdf.GlobalProperties.HtmlEngineFullPath = HttpContext.Current.Server.MapPath("~/bin/Select.Html.dep")
        Dim doc As PdfDocument = converter.ConvertHtmlString(html, "")

        ' save pdf document
        doc.Save(Response, False, "print" & Session("formid") & ".pdf")

        ' close pdf document
        doc.Close()

    End Sub

Create a button and panel on your aspx page.

Comments

Popular Posts