C# examples to convert HTML to PDF in .Net

Get Started

Don’t waste your day reinventing the wheel, be converting in 3 minutes from now

If you are looking for a C# webpage to pdf converter, you can use our free web service, in use by commercial applications and platforms. You do not need to install or download any libraries or components to make this work.

Especially great for reporting, brochures and invoicing – create one HTML webpage and use it as both as a source to generate your downloadable PDF or for users to view directly.

The service will convert webpages to pdfs online by reading your webpage and dynamically generate a PDF or an image. It will handle fairly complex documents and layouts including images and executing javascript, although as always the simpler the better.

See example #5 for a the new ASPNET 5 code.

Example #1 – How to stream a PDF directly to the end user with ASP.NET C# MVC

By commenting out line #17 you can switch between showing the file inside the browser window itself (if supported by the browser), and downloading as an attachment.

Example #2 – How to convert an HTML string to a PDF using ASP.NET C#

Line #12 allows you to change the default output format so the html is converted into an image. You can use “png”, “bmp” or “svg” formats for images. You could add line #12 to the above example to steam the image to the user (or as an attachment), or you can use this example which instead saves the image to your disk.

Example #3 – How to convert an HTML string to a PDF using ASP.NET C#

You can also convert an HTML string to a PDF by supplying the HTML directly. It can be as complex as you like and may include image references and stylesheeet information — just make sure it is valid HTML

Example #4 – Using C# MVC to convert a URL and add a footer

By default page margins are set to zero. In this example, we increase the bottom margin to 30 to allow space for a page footer. Note you could use a Url to the file, or you could pass the whole file as a string with the FooterHtml parameter

Here is an example custom HTML footer file with page numbers. Do not forget the doctype.

This is an example which hides the header on page 1 and 3

Example #5 – How to convert a webpage to an PDF and stream it to the browser using ASP.NET C#

Line #20 allows you to stream the bytes directly to the browser to use as an attachment

Example #6 – ASP.NET 5 and C# MVC 6

The WebClient has been replaced with HttpClient in ASP.NET 5. Other aspects follow the same format.

Example #7 – Batch/Asynchronous Process

For PDF conversion taking more than 30 seconds or input/output files more than 6 Mb, you need to use the batch/asynchronous parameters.

Need another PHP example? Would you mind dropping us a quick one line note to say which example you would have liked and we’ll put the most commonly used ones here.

About HTML2PDF Rocket

Html2PDF Rocket is based on an open source technology. It is a free and easy to use web service to convert html webpages into PDF files or images. HTML2PDF Rocket is trusted by over 10,500 companies worldwide.

What is an HTML to PDF API?

An HTML to PDF API is a web service that will read your webpage and dynamically generate a PDF or an image. It will convert the contents of a webpage to a PDF file. It handles fairly complex documents and layouts, including images.

Why Convert HTML to PDF?

Converting HTML to PDF is great for reporting, creating brochures, and printing invoices. Just create one HTML or PHP webpage, and use it as a source to generate downloadable PDF or viewable PDF pages. PDF is a fixed-layout document created by Adobe Systems. It is a perfect document for downloading text or HTML content in a file, with consistent layout that does not depend on the device for viewing and does not require internet connection.

Try HTML2PDF Rocket for FREE

and start converting HTML to PDF instantly!

Conversion Tip

The number #1 reason a conversion fails to come out how you expect is because it contains references to external links or images that don’t exist – or you are trying to link to localhost! If there is a conversion error, please check the page using your web browsers ‘developer’ network tab to ensure there are no 404 errors.