SelectPdf for .NET - Convert from Html to Pdf - MVC Razor - VB.NET / ASP.NET MVC Sample

This sample shows how to use SelectPdf html to pdf converter to convert an url to pdf, also setting a few properties.


Pdf Page Size:

Pdf Page Orientation:

Web Page Width:

Web Page Height:
(leave empty to auto detect)

Sample Code VB.NET

Imports SelectPdf

Namespace Controllers
    Public Class ConvertUrlToPdfController
        Inherits Controller

        ' GET: /ConvertUrlToPdf/
        Public Function Index() As ActionResult
            Return View()
        End Function

        Public Function SubmitAction(collection As FormCollection) As ActionResult
            ' read parameters from the webpage
            Dim url As String = collection("TxtUrl")

            Dim pdf_page_size As String = collection("DdlPageSize")
            Dim pageSize As PdfPageSize = DirectCast([Enum].Parse(GetType(PdfPageSize),
                pdf_page_size, True), PdfPageSize)

            Dim pdf_orientation As String = collection("DdlPageOrientation")
            Dim pdfOrientation As PdfPageOrientation = DirectCast(
                pdf_orientation, True), PdfPageOrientation)

            Dim webPageWidth As Integer = 1024
                webPageWidth = System.Convert.ToInt32(collection("TxtWidth"))
            End Try

            Dim webPageHeight As Integer = 0
                webPageHeight = System.Convert.ToInt32(collection("TxtHeight"))
            End Try

            ' instantiate a html to pdf converter object
            Dim converter As New HtmlToPdf()

            ' set converter options
            converter.Options.PdfPageSize = pageSize
            converter.Options.PdfPageOrientation = pdfOrientation
            converter.Options.WebPageWidth = webPageWidth
            converter.Options.WebPageHeight = webPageHeight

            ' create a new pdf document converting an url
            Dim doc As PdfDocument = converter.ConvertUrl(url)

            ' save pdf document
            Dim pdf As Byte() = doc.Save()

            ' close pdf document

            ' return resulted pdf document
            Dim fileResult As FileResult = New FileContentResult(pdf, "application/pdf")
            fileResult.FileDownloadName = "Document.pdf"
            Return fileResult
        End Function
    End Class
End Namespace