Skip to main content

Converting Current Web Page to PDF

Version: 2.0

Converting Current Web Page to PDF

Simple HTML to PDF Converting​

Converting HTML content from a URL, an HTML file or even just a string of HTML content is handled very easily using our Simple Converting techniques outlined in the above help topic. 

The below section will describe another scenario of HTML to PDF converting.  In this section we will go over how to take the current content from a web page and convert that to PDF directly from within that page.

Converting the Current Web Page to PDF​

Inheriting from ceTe.DynamicPDF.Conversion.PdfConversionPage​

Adding the ability to convert any web page (System.Web.UI.Page) directly to PDF is made very simple in the DynamicPDF Converter API. This functionality could be useful in situations where a direct hard coded URL to a particular web page can not be given (for instance after some authentication, after some data is filled out on a page, etc.). 

Two things must be in place in order to quickly convert any web page to PDF:

  • Inherit from PdfConversionPage - Your System.Web.UI.Page should inherit from the ceTe.DynamicPDF.Conversion.PdfConversionPage.
  • Set ConvertToPdf to True - By default, ConvertToPdf is set to False. When set to False, the web page will output the HTML as would typically be expected. When the ConvertToPdf is set to True (maybe in a button click on in a link) then the HTML is converted to PDF and output to the current web page.

The below example shows how easy it is to convert the content on the current System.Web.UI.Page to a PDF document:

public partial class _Default : PdfConversionPage
{
protected void btnConvertToPDF_Click(object sender, EventArgs e)
{
ConvertToPdf = true;
}
}

Partial Public Class _Default
Inherits PdfConversionPage

Protected Sub btnConvertToPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConvertToPDF.Click
ConvertToPdf = True
End Sub
End Class

When the System.Web.UI.Page can not inherit from ceTe.DynamicPDF.Conversion.PdfConversionPage​

The above steps are the most simple way to convert the current web page to PDF. That method does however require your web page (System.Web.UI.Page) inherit from ceTe.DynamicPDF.Conversion.PdfConversionPage.  If inheriting from ceTe.DynamicPDF.Conversion.PdfConversionPage is not an option (for instance if your web page already inherits from something different) then review the below example for manually setting up your web page to be able to convert it to PDF.

Basically, if the current web page can not inherit from ceTe.DynamicPDF.Conversion.PdfConversionPage, then the Render method on the current System.Web.UI.Page object needs to be overwritten to output the converted PDF to the browser instead of outputting the HTML code (based on whether a boolean has been set to True). The below example demonstrates this functionality:

public partial class _Default : System.Web.UI.Page
{
private bool convertToPdf = false;
protected override void Render(HtmlTextWriter writer)
{
if (convertToPdf)
{
TextWriter textWriter = new StringWriter();
HtmlTextWriter hTextWriter = new HtmlTextWriter(textWriter);
base.Render(hTextWriter);
string htmlContent = textWriter.ToString();
Converter.ConvertHtmlStringToWeb(htmlContent);
}
else
base.Render(writer);
}

protected void btnConvertToPDF_Click(object sender, EventArgs e)
{
convertToPdf = true;
}
}
Partial Public Class _Default
Inherits System.Web.UI.Page

Private ConvertToPDF As Boolean = False
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

If (ConvertToPDF) Then
Dim textWriter As System.IO.TextWriter = New System.IO.StringWriter()
Dim hTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(textWriter)
MyBase.Render(hTextWriter)
Dim htmlContent As String = textWriter.ToString()
Converter.ConvertHtmlStringToWeb(htmlContent)
Else
MyBase.Render(writer)
End If

End Sub

Protected Sub btnConvertToPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertToPDF.Click
ConvertToPDF = True
End Sub

End Class