Convert webpage into PDF

I was working on a project and have been asked to convert the webpage into a pdf document. I have found a solution in a forum, but it was in c#, I have converted it in VB.net. See below method to convert a webpage into a PDF document.

First, you need to download iTextSharp. iTextSharp is a port of the iText open source java library  which allows you to generate PDF files on the fly.

Once you downloaded the library, you need to reference it in your project. To reference the downloaded the library, follow the below steps:

  1. From the Website menu, select “Add Reference”, then you will be prompted with the add reference dialog
  2. Click on the browse button to locate your iTextSharp library (itextsharp.dll)

once you have done that, you should see a reference to your class library in Solution Explorer window.

Now, Add the below code in your project.

pdf.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pdf.aspx.vb" Inherits="Pdf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="PlaceholderPdf" runat="server"></asp:PlaceHolder>
<div>
<table border="1">
<tr>
<td colspan="2">
This is a test document, credit goes to srinivaskotra
</td></tr><tr><td>cell1
</td><td>cell2</td></tr>
<tr><td colspan="2">
<asp:Label ID="lblLabel" runat="server" Text="Label Test"></asp:Label>
</td></tr></table></div>
</form>
</body>
</html>

pdf.aspx.vb


Imports System
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.IO
Imports System.Text.RegularExpressions
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html
Imports iTextSharp.text.xml
Imports System.Xml
Imports iTextSharp.text.html.simpleparser
Public Partial Class Pdf
Inherits System.Web.UI.Page
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dim mem As New MemoryStream()
Dim twr As New StreamWriter(mem)
Dim myWriter As New HtmlTextWriter(twr)
MyBase.Render(myWriter)
myWriter.Flush()
myWriter.Dispose()
Dim strmRdr As New StreamReader(mem)
strmRdr.BaseStream.Position = 0
Dim pageContent As String = strmRdr.ReadToEnd()
strmRdr.Dispose()
mem.Dispose()
writer.Write(pageContent)
'---------------------------------------------------
CreatePDFDocument(pageContent)
End Sub
Public Sub CreatePDFDocument(ByVal strHtml As String)
'---------------------------------------------------
Dim strFileName As String = HttpContext.Current.Server.MapPath("test.pdf")
' step 1: creation of a document-object
Dim document As New Document()
' step 2:
' we create a writer that listens to the document
PdfWriter.GetInstance(document, New FileStream(strFileName, FileMode.Create))
Dim se As New StringReader(strHtml)
Dim obj As New HTMLWorker(document)
document.Open()
obj.Parse(se)
document.Close()
'---------------------------------------------------
ShowPdf(strFileName)
End Sub
Public Sub ShowPdf(ByVal strFileName As String)
Response.ClearContent()
Response.ClearHeaders()
Response.AddHeader("Content-Disposition", "inline;filename=" & strFileName)
Response.ContentType = "application/pdf"
Response.WriteFile(strFileName)
Response.Flush()
Response.Clear()
End Sub
End Class

That’s it. Now when you run the program, you will see the pdf file instead opening the webpage.