Convert from Html To Pdf in ASP.NET MVC (C# and VB.NET)

html to pdf mvc asp net
SelectPdf for .NET is a professional PDF Library that can be used for creating, writing, editing, handling and reading PDF files without any external dependencies within .NET applications. Using this .NET PDF library, you can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely through C#/VB.NET in ASP.NET MVC (ASPX or Razor views) without installing Adobe Acrobat.


Many rich features can be supported by the .NET PDF SDK, such as security setting (including digital signatures), PDF merge/split, text, html and image drawing into PDF and many more. Besides, SelectPdf for .NET component can be used to easily convert HTML to PDF with C#/VB.NET in high quality.

It’s very easy to use SelectPdf SDK for .NET in ASP.NET MVC applications. Take a look at the simple code below.

Convert from HTML to PDF in ASP.NET MVC C# Sample Code

[HttpPost]
public ActionResult Convert(FormCollection collection)
{
    // read parameters from the webpage
    string url = collection["TxtUrl"];

    string pdf_page_size = collection["DdlPageSize"];
    PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);

    string pdf_orientation = collection["DdlPageOrientation"];
    PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(
        typeof(PdfPageOrientation), pdf_orientation, true);

    int webPageWidth = 1024;
    try
    {
        webPageWidth = System.Convert.ToInt32(collection["TxtWidth"]);
    }
    catch { }

    int webPageHeight = 0;
    try
    {
        webPageHeight = System.Convert.ToInt32(collection["TxtHeight"]);
    }
    catch { }

    // instantiate a html to pdf converter object
    HtmlToPdf converter = 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
    PdfDocument doc = converter.ConvertUrl(url);

    // save pdf document
    byte[] pdf = doc.Save();

    // close pdf document
    doc.Close();

    // return resulted pdf document
    FileResult fileResult = new FileContentResult(pdf, "application/pdf");
    fileResult.FileDownloadName = "Document.pdf";
    return fileResult;
}

Convert from HTML to PDF in ASP.NET MVC VB.NET Sample Code

 _
Public Function Convert(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([Enum].Parse(GetType(PdfPageOrientation), _
        pdf_orientation, True), PdfPageOrientation)

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

    Dim webPageHeight As Integer = 0
    Try
        webPageHeight = System.Convert.ToInt32(collection("TxtHeight"))
    Catch
    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
    doc.Close()

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

The code above works the same for both commercial SelectPdf Library and also for the Free Html To Pdf Converter – Community Edition.