VB.NET guidev22.0.0
Convert HTML to PDF in VB.NET
The ExpertPdf HTML to PDF Converter converts web pages, HTML files and HTML strings to PDF from VB.NET: in ASP.NET Web Forms, ASP.NET MVC, Windows Forms and console applications, on .NET Framework and on .NET Core and .NET 5 to .NET 11.
1. Install the NuGet package
Or reference the assembly from the evaluation download.
Install-Package ExpertPdfHtmlToPdf
2. Convert a URL to PDF
GetPdfBytesFromUrl returns the PDF as a byte array.
Imports ExpertPdf.HtmlToPdf Dim converter As New PdfConverter() Dim pdf As Byte() = converter.GetPdfBytesFromUrl("https://www.example.com/") System.IO.File.WriteAllBytes("example.pdf", pdf)
3. Convert an HTML string
Give a base URL when the HTML uses relative paths for images or CSS.
Imports ExpertPdf.HtmlToPdf Dim html As String = "<h1>Invoice 1024</h1><p>Total: <b>$250.00</b></p>" & "<img src=""images/logo.png"">" Dim converter As New PdfConverter() ' The base URL resolves relative paths such as images/logo.png and CSS files Dim pdf As Byte() = converter.GetPdfBytesFromHtmlString(html, "https://www.example.com/") System.IO.File.WriteAllBytes("invoice.pdf", pdf)
4. Page size, orientation and a footer
SavePdfFromUrlToFile writes the PDF straight to a file.
Dim converter As New PdfConverter() converter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4 converter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Landscape converter.PdfDocumentOptions.LeftMargin = 20 converter.PdfDocumentOptions.RightMargin = 20 converter.PdfDocumentOptions.TopMargin = 20 converter.PdfDocumentOptions.BottomMargin = 20 converter.PdfDocumentOptions.ShowFooter = True converter.PdfFooterOptions.FooterText = "Quarterly report" converter.PdfFooterOptions.ShowPageNumber = True converter.SavePdfFromUrlToFile("https://www.example.com/report", "report.pdf")
5. In ASP.NET and Windows Forms
Send the PDF to the browser from a Web Forms page, or save it from a desktop application. For ASP.NET MVC see HTML to PDF in ASP.NET MVC.
Imports ExpertPdf.HtmlToPdf Protected Sub BtnPdf_Click(sender As Object, e As EventArgs) Handles BtnPdf.Click Dim converter As New PdfConverter() Dim pdf As Byte() = converter.GetPdfBytesFromUrl("https://www.example.com/report") Response.Clear() Response.ContentType = "application/pdf" Response.AddHeader("Content-Disposition", "attachment; filename=report.pdf") Response.BinaryWrite(pdf) Response.End() End Sub
HTML to PDF in VB.NET: questions
Is the VB.NET API the same as the C# one?
Yes. It is the same .NET library: the
PdfConverter class, its options and its methods are used the same way from VB.NET and C#. See HTML to PDF in C#.Which .NET versions can I use?
.NET Framework, with the
ExpertPdfHtmlToPdf package, and .NET Core and .NET 5 to .NET 11, with ExpertPdf.HtmlToPdf.NetCore.Are there VB.NET samples?
Yes. The download includes VB.NET samples for ASP.NET, ASP.NET MVC and Windows Forms.
Can I try it for free?
Yes. The evaluation is fully functional and has no time limit.
Convert HTML to PDF in your VB.NET application.
Download the free evaluation with VB.NET samples for ASP.NET, ASP.NET MVC and Windows Forms.