Generating PDF document from HTML in VB.Net

If you are wondering how to convert HTML to PDF in VB.Net then read on. There are quite a few paid libraries available to generate PDF documents in VB.Net.

PDFSharp is a leading free and open source library for generating PDF documents in .Net. However PDFSharp cannot convert HTML code to PDF. PDFSharp gives you complete control over creating a PDF document from scratch, building it as you want but if you are looking to quickly convert HTML to PDF in VB.Net then PDFSharp won't work.

Headless Chrome is the preferred way to convert raw HTML to PDF. Nothing can render HTML better than a browser and nothing beats Chromium when it comes to rendering HTML. Chrome has inbuilt capability to convert any HTML page to PDF. Another benefit of generating PDF documents from HTML is that you can style the document using your familiar CSS framework. You don't have to learn a new styling language to get the out put you want. You can focus on creating the data for the PDF you want and leave the rest to our API.

We at PDFMark have done the hard work of managing the headless Chrome and wrapped it into an easy to use REST API so you can simply call the API from your VB.Net application and pass in the HTML or a URL and get it converted to a PDF document. PDFMark will even upload it to cloud and give you a CDN link so you can simply serve that link to your end users without having to worry about saving the document yourself.

Following is the code for calling the PDFMark HTML to PDF REST API in VB.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.IO
Imports System.Net

Private Function SendRequest(Endpoint As String, Key As String, Payload As Byte()) As String
  Dim Response As String
  Dim Request As Net.WebRequest

  Request = Net.WebRequest.Create(New Uri(Endpoint))
  Request.ContentLength = Payload.Length
  Request.ContentType = "application/json"
  Request.Method = "POST"
  Request.Headers.Add("key", Key)

  Using RequestStream = Request.GetRequestStream
    RequestStream.Write(Payload, 0, Payload.Length)
    RequestStream.Close()

    Using ResponseStream = Request.GetResponse.GetResponseStream
      Using Reader As New StreamReader(ResponseStream)
	  Response = Reader.ReadToEnd()
      End Using
    End Using
  End Using

  Return Response
End Function

Sub Main()
  Dim APIKey As String = "YOUR_API_KEY"
  Dim Endpoint As String = "https://api.pdfmark.com/pdf"
  Dim Data As New JObject()
  Data.Add("html", "<html> <body> <h1> Hello World! </h1> </body> </html>")
  Dim Payload As String = Data.ToString()

  Console.Write(SendRequest(Endpoint, APIKey, Encoding.UTF8.GetBytes(Payload)))
End Sub

Try out PDFMark API with your own HTML or URL

HTML
URL
JSON
   Preview    Try <qr>http://example.com</qr>

Sign up for PDFMark and get your API key. You can start converting your HTML to PDF in next one minute.

Convert HTML to PDF in your favourite programming language. Click on the icon below to view the code

HTML to PDF in C#

HTML to PDF in Php

HTML to PDF in Python

HTML to PDF in Go


HTML to PDF in NodeJS

HTML to PDF in Java

HTML to PDF in VB.Net

HTML to PDF in Ruby