I use ABCpdf .NET to print web page as pdf:
- Add reference ABCpdf.dll
- Render web page in pdf in landscape or portrait format
- Write pdf to browser or save to disk
- Save HTML as pdf or save the whole web page as pdf by given URL
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="PrintToPDF._Default" %>
<!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">
<div>
<asp:Button ID="btnWriteToBrowserByHTML" runat="server" Text="Write To Browser By HTML"/>
<asp:Button ID="btnWriteToDiskByHTML" runat="server" Text="Write To Disk By HTML"/>
<asp:Button ID="btnWriteToBrowserByURL" runat="server" Text="Write To Browser By URL"/>
<asp:Button ID="btnWriteToDiskByURL" runat="server" Text="Write To Disk By URL"/>
</div>
</form>
</body>
</html>
Imports WebSupergoo.ABCpdf6
Imports System.Text
Imports System.IO
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Function GenerateQuoteInHTML() As String
Dim sb As StringBuilder = New StringBuilder
Dim sw As StringWriter = New StringWriter(sb)
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
Dim litTop As New Literal
Dim litTopText As New StringBuilder()
'the width on div makes the table readable in landscape format
litTopText.AppendLine("<div style='width:1300px'><b>Title</b><br /><br />")
litTop.Text = litTopText.ToString()
Dim litBottom As New Literal
Dim litBottomText As New StringBuilder()
litBottomText.AppendFormat("</div>")
litBottom.Text = litBottomText.ToString()
litTop.RenderControl(hw)
litBottom.RenderControl(hw)
Return sb.ToString
End Function
Private Sub GeneratePDF(ByVal source As String, ByVal exportType As ExportType, ByVal sourceType As SourceType)
'set properties
Dim theDoc As New Doc
theDoc.HtmlOptions.PageCacheEnabled = False
theDoc.HtmlOptions.BrowserWidth = 0
theDoc.HtmlOptions.ImageQuality = 101
theDoc.MediaBox.Width = 1000
theDoc.Rect.Width = 1000
Dim theID As Integer
If sourceType = PrintToPDF.SourceType.HTML Then
'Renders a web page specified as HTML.
theID = theDoc.AddImageHtml(source)
ElseIf sourceType = PrintToPDF.SourceType.URL Then
'Renders a web page specified by URL.
theID = theDoc.AddImageUrl(source)
End If
'paging
'http://www.websupergoo.com/helppdf5/default.html?page=source%2F4-examples%2F08-landscape.htm
Do While True
theDoc.FrameRect()
If (theDoc.Chainable(theID) = False) Then
Exit Do
End If
theDoc.Page = theDoc.AddPage()
theID = theDoc.AddImageToChain(theID)
Loop
theDoc.HtmlOptions.LinkPages()
For x As Integer = 1 To theDoc.PageCount
theDoc.PageNumber = x
theDoc.Flatten()
Next
If exportType = PrintToPDF.ExportType.Browser Then
'write to browser
Dim theData As Byte() = theDoc.GetData()
Response.Expires = -1000
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", theData.Length.ToString())
Dim timestamp As String = String.Format("{0}_{1}_{2}_{3}_{4}_{5}", Date.Now.Year.ToString, Date.Now.Month.ToString, Date.Now.Day.ToString, Date.Now.Hour.ToString, Date.Now.Minute.ToString, Date.Now.Second.ToString)
Response.AddHeader("content-disposition", "attachment; filename=MyPDF" + timestamp + ".PDF")
Response.BinaryWrite(theData)
theDoc.Clear()
ElseIf exportType = PrintToPDF.ExportType.Disk Then
'write to disk
Dim file As String = Path.Combine(Server.MapPath("CSVUploaded"), "test.pdf")
theDoc.Save(file)
End If
End Sub
Private Sub btnWriteToBrowserByHTML_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWriteToBrowserByHTML.Click
Dim strHTML As String = GenerateQuoteInHTML()
GeneratePDF(strHTML, ExportType.Browser, SourceType.HTML)
End Sub
Private Sub btnWriteToBrowserByURL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWriteToBrowserByURL.Click
GeneratePDF("http://www.google.com.au", ExportType.Browser, SourceType.URL)
End Sub
Private Sub btnWriteToDiskByHTML_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWriteToDiskByHTML.Click
Dim strHTML As String = GenerateQuoteInHTML()
GeneratePDF(strHTML, ExportType.Disk, SourceType.HTML)
End Sub
Private Sub btnWriteToDiskByURL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWriteToDiskByURL.Click
GeneratePDF("http://www.google.com.au", ExportType.Disk, SourceType.URL)
End Sub
End Class
Enum SourceType
URL
HTML
End Enum
Enum ExportType
Browser
Disk
End Enum
Reference: