◆C# / ASP.NET SAMPLE

HTML Code to PDF Converter

Paste your HTML code, choose the page size and click Create PDF. Free, no signup. The C# code behind this converter is below.

Html Code:


Base Url:

Pdf Page Size:


Pdf Page Orientation:


Web Page Width:
px

Web Page Height:
px
(leave empty to auto detect,
or set it to leave out the content below)

Rendering Engine:

Free to use, no signup. Every page of the PDF carries a “Demo Version” watermark, which a SelectPdf library license removes.

Do this in your own app

ANY LANGUAGE

HTML to PDF API

Get a free API key: 7-day trial, 200 conversions, no credit card.

curl https://selectpdf.com/api2/convert/ \
  -d "key=YOUR_KEY" \
  --data-urlencode "html=<h1>Hello World</h1>" \
  -o document.pdf
Get a free API key
C# / VB.NET

.NET library

Install from NuGet and convert in a few lines.

HtmlToPdf converter = new HtmlToPdf();
PdfDocument doc =
    converter.ConvertHtmlString(htmlString, baseUrl);
doc.Save("document.pdf");
doc.Close();
See the .NET library

Free Community Edition

Free for commercial use, up to 5 pages per PDF.

Get the Community Edition

About this HTML to PDF converter

This converter turns the HTML you paste into a PDF document, using the same engine as the SelectPdf library for .NET. The default Chromium engine renders HTML5, CSS3 and JavaScript; the converter also offers the older WebKit engines.

Choose the PDF page size (A1 to A5, Letter, Half Letter, Ledger or Legal) and orientation, and the width of the browser window your HTML is laid out in: 1,024 pixels by default. Leave the height empty to convert all of the content, or enter a height to leave out what is below it. If the HTML loads images, style sheets or scripts by relative paths, enter the address they come from as the base URL.

Questions

Paste the HTML into the box above, choose the page size and orientation, and click Create PDF. The PDF downloads as soon as the conversion is done.
Yes. The online converter is free to use and needs no signup. Every page of the PDF carries a “Demo Version” watermark, which a SelectPdf library license removes.
Yes. The default Chromium engine renders HTML5, CSS3 and JavaScript. The converter also offers the older WebKit and WebKit Restricted engines.
If your HTML refers to images, style sheets or scripts by relative paths, the base URL tells the converter where to load them from, for example https://example.com/.
Create an HtmlToPdf object, call ConvertHtmlString with your HTML and base URL, and save the PdfDocument it returns. The complete C# code of this page is below. From any other language, use the HTML to PDF API.

C# / ASP.NET code for this converter

Sample code · C#
using SelectPdf;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SelectPdf.Samples
{
    public partial class convert_html_code_to_pdf : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TxtHtmlCode.Text = @"<html>
 <body>
  Hello World from selectpdf.com.
 </body>
</html>
";
            }
        }

        protected void BtnCreatePdf_Click(object sender, EventArgs e)
        {
            RenderingEngine renderingEngine =
                (RenderingEngine)Enum.Parse(typeof(RenderingEngine), DdlRenderingEngine.SelectedValue, true);

            // read parameters from the webpage
            string htmlString = TxtHtmlCode.Text;
            string baseUrl = TxtBaseUrl.Text;

            string pdf_page_size = DdlPageSize.SelectedValue;
            PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), 
                pdf_page_size, true);

            string pdf_orientation = DdlPageOrientation.SelectedValue;
            PdfPageOrientation pdfOrientation = 
                (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation), 
                pdf_orientation, true);

            int webPageWidth = 1024;
            try
            {
                webPageWidth = Convert.ToInt32(TxtWidth.Text);
            }
            catch { }

            int webPageHeight = 0;
            try
            {
                webPageHeight = Convert.ToInt32(TxtHeight.Text);
            }
            catch { }

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
            converter.Options.MaxPageLoadTime = Global.MaxPageLoadTime;

            converter.Options.RenderingEngine = renderingEngine;

            // set converter options
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth = webPageWidth;
            converter.Options.WebPageHeight = webPageHeight;
            // a height alone only sizes the viewport; fixing the size cuts the content to it
            converter.Options.WebPageFixedSize = webPageHeight > 0;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

            // save pdf document
            doc.Save(Response, false, "Sample.pdf");

            // close pdf document
            doc.Close();
        }
    }
}