Forget tedious coding or server management: SelectPdf is a simple API that makes it easy to convert HTML, CSS and JavaScript to PDF. Easily generate PDF documents from HTML code with SelectPdf powerful online REST API.
SelectPdf API is the best when it comes to converting web pages to pdf. Just send your url or html string to our API and get back the perfect PDF document. Externalize the load from your servers. Outsource the html to pdf conversion to SelectPdf REST API and get the best results no matter the platform you are using: Java, PHP, .NET, Ruby, Perl or anything else.
The following examples are in VB.NET for SelectPdf Online API. Make sure that you are interested in the online API, because if you are interested in the .NET library, there are other places where you can find VB.NET samples for SelectPdf .NET Library.
Until recently, we did not provide any .NET samples for the online API, because the recommended product if .NET was available, was our SelectPdf Library for .NET. What changed is that more and more users are moving to systems where the full .NET Framework (required by our .NET library) is not available (they use Azure websites or .NET Core or Mono), but they still code in VB.NET or C#. Here, the online Html to Pdf API comes into play.
There are a variety of methods in .NET that you can use to access a REST API. We will show you 3 of them in this article. One important thing to remember is that the PDF is returned to the client in binary form. Trying to read this binary format as text will cause problems and your PDF will not be saved correctly. So, remember, binary mode for PDF reading.
Later Edit: The recommended approach to access SelectPdf Online REST API from VB.NET is to use the dedicated .NET Client Library for SelectPdf API. Alternatively, you can write your own code to access the API. Below are some examples for this approach.
Example #1 – How to use VB.NET to convert a webpage to PDF and save it on the disk using HttpWebRequest / HttpWebResponse
This code converts an url to pdf in VB.NET using SelectPdf REST API through a POST request. It uses HttpWebRequest / HttpWebResponse (and Newtonsoft for JSON serialization). The content is saved into a file on the disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
Imports System.Net Imports System.IO Imports System.Text Imports Newtonsoft.Json Module SelectPdfTest Sub Main() ' POST JSON example using HttpWebRequest / HttpWebResponse (and Newtonsoft for JSON serialization) SelectPdfPostWithHttpWebRequest() End Sub ' POST JSON example using HttpWebRequest / HttpWebResponse (and Newtonsoft for JSON serialization) Sub SelectPdfPostWithHttpWebRequest() System.Console.WriteLine("Starting conversion with HttpWebRequest ...") Dim apiEndpoint As String = "https://selectpdf.com/api2/convert/" Dim apiKey As String = "your license key here" Dim testUrl As String = "https://selectpdf.com" ' set parameters Dim params As New SelectPdfParameters params.key = apiKey params.url = testUrl ' JSON serialize parameters Dim jsonData As String = JsonConvert.SerializeObject(params) Dim byteData As Byte() = Encoding.UTF8.GetBytes(jsonData) ' create request Dim request As HttpWebRequest = DirectCast(WebRequest.Create(apiEndpoint), HttpWebRequest) request.ContentType = "application/json" request.Method = "POST" request.Credentials = CredentialCache.DefaultCredentials ' POST parameters Dim dataStream As Stream = request.GetRequestStream() dataStream.Write(byteData, 0, byteData.Length) dataStream.Close() ' GET response (if response code is not 200 OK, a WebException is raised) Try Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) Dim responseStream As Stream = response.GetResponseStream() If response.StatusCode = HttpStatusCode.OK Then ' all ok - read PDF and write on disk (binary read!!!!) Dim ms As MemoryStream = BinaryReadStream(responseStream) ' write to file Dim file As FileStream = New FileStream("test1.pdf", FileMode.Create, FileAccess.Write) ms.WriteTo(file) file.Close() Else ' error - get error message System.Console.WriteLine("Error code: " + response.StatusCode.ToString()) End If responseStream.Close() Catch webEx As WebException ' an error occurred System.Console.WriteLine("Error: " + webEx.Message) Dim response As HttpWebResponse = DirectCast(webEx.Response, HttpWebResponse) Dim responseStream As Stream = response.GetResponseStream() ' get details of the error message if available (text read!!!) Dim readStream As StreamReader = New StreamReader(responseStream) Dim message As String = readStream.ReadToEnd() System.Console.WriteLine("Error Message: " + message) Catch ex As Exception System.Console.WriteLine("Error: " + ex.Message) End Try System.Console.WriteLine("Finished.") End Sub ' Binary read from Stream into a MemoryStream Public Function BinaryReadStream(input As Stream) As MemoryStream Dim bytesNumber As Integer Dim bytes(1024) As Byte Dim stream As New MemoryStream() Dim reader As New BinaryReader(input) Do bytesNumber = reader.Read(bytes, 0, bytes.Length) If bytesNumber > 0 Then stream.Write(bytes, 0, bytesNumber) Loop While bytesNumber > 0 stream.Position = 0 Return stream End Function ' API parameters - add the rest here if needed Partial Public Class SelectPdfParameters Public Property key As String Public Property url As String Public Property html As String Public Property base_url As String End Class End Module |
Example #2 – How to use VB.NET to convert a webpage to PDF and save it on the disk using WebClient
This code converts an url to pdf in VB.NET using SelectPdf REST API through a POST request. It uses WebClient (and Newtonsoft for JSON serialization). The content is saved into a file on the disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
Imports System.Net Imports System.IO Imports System.Text Imports Newtonsoft.Json Module SelectPdfTest Sub Main() ' POST JSON example using WebClient (and Newtonsoft for JSON serialization) SelectPdfPostWithWebClient() End Sub ' POST JSON example using WebClient (and Newtonsoft for JSON serialization) Sub SelectPdfPostWithWebClient() System.Console.WriteLine("Starting conversion with WebClient ...") Dim apiEndpoint As String = "https://selectpdf.com/api2/convert/" Dim apiKey As String = "your license key here" Dim testUrl As String = "https://selectpdf.com" ' set parameters Dim params As New SelectPdfParameters params.key = apiKey params.url = testUrl ' JSON serialize parameters Dim jsonData As String = JsonConvert.SerializeObject(params) Dim byteData As Byte() = Encoding.UTF8.GetBytes(jsonData) ' create WebClient object Dim webClient As New WebClient() webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json") ' POST parameters (if response code is not 200 OK, a WebException is raised) Try Dim result As Byte() = webClient.UploadData(apiEndpoint, "POST", byteData) ' all ok - read PDF and write on disk (binary read!!!!) Dim ms As MemoryStream = New MemoryStream(result) ' write to file Dim file As FileStream = New FileStream("test2.pdf", FileMode.Create, FileAccess.Write) ms.WriteTo(file) file.Close() Catch webEx As WebException ' an error occurred System.Console.WriteLine("Error: " + webEx.Message) Dim response As HttpWebResponse = DirectCast(webEx.Response, HttpWebResponse) Dim responseStream As Stream = response.GetResponseStream() ' get details of the error message if available (text read!!!) Dim readStream As StreamReader = New StreamReader(responseStream) Dim message As String = readStream.ReadToEnd() System.Console.WriteLine("Error Message: " + message) Catch ex As Exception System.Console.WriteLine("Error: " + ex.Message) End Try System.Console.WriteLine("Finished.") End Sub ' API parameters - add the rest here if needed Partial Public Class SelectPdfParameters Public Property key As String Public Property url As String Public Property html As String Public Property base_url As String End Class End Module |
Example #3 – How to use VB.NET to convert a webpage to PDF and save it on the disk using RestSharp
This code converts an url to pdf in VB.NET using SelectPdf REST API through a POST request. It uses RestSharp for the HTTP communication with the API. The content is saved into a file on the disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
Imports System.Net Imports System.IO Imports System.Text Module SelectPdfTest Sub Main() ' POST JSON example using RestSharp (Simple REST and HTTP API Client for .NET) SelectPdfPostWithRestSharp() End Sub ' POST JSON example using RestSharp (Simple REST and HTTP API Client for .NET) Sub SelectPdfPostWithRestSharp() System.Console.WriteLine("Starting conversion with RestSharp ...") Dim apiEndpoint As String = "https://selectpdf.com/api2/convert/" Dim apiKey As String = "your license key here" Dim testUrl As String = "https://selectpdf.com" ' set parameters Dim params = New With { _ Key .key = apiKey, _ Key .url = testUrl _ } ' create request Dim client As RestSharp.RestClient = New RestSharp.RestClient(apiEndpoint) Dim request As RestSharp.RestRequest = New RestSharp.RestRequest(RestSharp.Method.POST) request.RequestFormat = RestSharp.DataFormat.Json request.AddHeader("Content-Type", "application/json") ' add parameters to request request.AddJsonBody(params) ' execute request Try Dim response = client.Execute(request) If response.StatusCode = HttpStatusCode.OK Then ' all ok - read PDF and write on disk (binary read!!!!) Dim result As Byte() = response.RawBytes() Dim ms As MemoryStream = New MemoryStream(result) ' write to file Dim file As FileStream = New FileStream("test3.pdf", FileMode.Create, FileAccess.Write) ms.WriteTo(file) file.Close() Else ' an error occurred System.Console.WriteLine("Error code: " + response.StatusCode.ToString()) System.Console.WriteLine("Error message: " + response.StatusDescription) System.Console.WriteLine("Error details: " + response.Content) End If Catch ex As Exception System.Console.WriteLine("Error: " + ex.Message) End Try System.Console.WriteLine("Finished.") End Sub End Module |
The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF API page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.
Hopefully these examples will help you to be on your way using the API for URL to PDF conversion in VB.NET.