PDF.co Web API - HTML To PDF API - VB.NET - Generate PDF Invoice From HTML Template - ByteScout

PDF.co Web API – HTML To PDF API – VB.NET – Generate PDF Invoice From HTML Template

  • Home
  • /
  • Articles
  • /
  • PDF.co Web API – HTML To PDF API – VB.NET – Generate PDF Invoice From HTML Template

PDF.co Web API – HTML To PDF API – VB.NET – Generate PDF Invoice From HTML Template

Module1.vb

Imports System.IO
Imports System.Net
Imports Newtonsoft.Json.Linq

Module Module1

	' The authentication key (API Key).
	' Get your own by registering at https://app.pdf.co/documentation/api
	Const API_KEY As String = "***********************************"

	' HTML template
	Dim template As String = File.ReadAllText(".\invoice_template.html")
	' Data to fill the template
	Dim templateData As String = File.ReadAllText(".\invoice_data.json")
	' Destination PDF file name
	Const DestinationFile As String = ".\result.pdf"

	Sub Main()

		' Create standard .NET web client instance
		Dim webClient As WebClient = New WebClient()

		' Set API Key
		webClient.Headers.Add("x-api-key", API_KEY)

		Try
			' Prepare URL for HTML to PDF API call
			Dim request As String = Uri.EscapeUriString(String.Format(
				"https://api.pdf.co/v1/pdf/convert/from/html?name={0}",
				Path.GetFileName(DestinationFile)))

			' Prepare request body in JSON format
			Dim jsonObject As JObject = New JObject(
				New JProperty("html", template),
				New JProperty("templateData", templateData))

			webClient.Headers.Add("Content-Type", "application/json")

			' Execute request
			Dim response As String = webClient.UploadString(request, jsonObject.ToString())

			' Parse JSON response
			Dim json As JObject = JObject.Parse(response)

			If json("error").ToObject(Of Boolean) = False Then

				' Get URL of generated PDF file
				Dim resultFileUrl As String = json("url").ToString()

				webClient.Headers.Remove("Content-Type") ' remove the header required for only the previous request

				' Download the PDF file
				webClient.DownloadFile(resultFileUrl, DestinationFile)

				Console.WriteLine("Generated PDF document saved as ""{0}"" file.", DestinationFile)

			End If

		Catch ex As WebException
			Console.WriteLine(ex.ToString())
		End Try

		webClient.Dispose()


		Console.WriteLine()
		Console.WriteLine("Press any key...")
		Console.ReadKey()

	End Sub

End Module


  Click here to get your Free Trial version of the SDK

Tutorials:

prev
next