Converts HTML string to PDF byte array

Namespace:  SautinSoft
Assembly:  SautinSoft.PdfVision (in SautinSoft.PdfVision.dll) Version:


 C#  Visual Basic 
public byte[] ConvertHtmlStringToPDFStream(
	string inputHtmlString
Public Function ConvertHtmlStringToPDFStream ( _
	inputHtmlString As String _
) As Byte()


Any string in HTML format, framed by <html> ... </html> tags

Return Value

PDF document as byte array - in case of converting successfully
null - in case of converting failed


If HTML string contains links to .css files or to images, please be sure that links are not relative.
Links have to be full, like this: <img src="" >.


CopyASP.Net - Print ASPX to PDF in C#
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        string id = Request.QueryString["id"];
        HouseDescription hd = null;

        if (id != null)
            // Recreate the page state
            hd = (HouseDescription)Application[id];

        // First time loading. Set all to default.
            if (!IsPostBack)
                SetPageState(new HouseDescription());
    protected void GetPDF_Click(object sender, EventArgs e)
        // 1. Save the current page state
        // 1.1. Create ID
        string id = Guid.NewGuid().ToString();

        // 1.2 Save the current state in the Application State.
        HouseDescription hd = GetPageState();
        Application[id] = hd;

        // 2. Create url
        string pageUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        string pageUrlWithQuerry = string.Format("{0}?id={1}", pageUrl, id);

        // 3. Get PDF
        SautinSoft.PdfVision v = new SautinSoft.PdfVision();

        // Set "Edge mode" to support all modern CSS.
        byte[] pdf = v.ConvertHtmlFileToPDFStream(pageUrlWithQuerry);

        // 4. Show PDF result
        if (pdf != null)
            Response.Buffer = true;
            Response.ContentType = "application/PDF";
            //Response.AddHeader("content-disposition", "attachment; filename=Result.pdf");
            Response.AddHeader("content-disposition", "inline; filename=Result.pdf");
    protected void SetPageState(HouseDescription hd)
        txtHouseSpace.Text = hd.HouseSpace.ToString();
        lstFloorsNum.SelectedValue = hd.FloorsNumber.ToString();
        chkBathroom.Checked = hd.IsBathroom;
        chkToilet.Checked = hd.IsToilet;
        chkSwimmingPool.Checked = hd.IsSwimmingPool;
    protected HouseDescription GetPageState()
        HouseDescription hd = new HouseDescription();

        float houseSpace = 0f;

        if (float.TryParse(txtHouseSpace.Text, out houseSpace))
            hd.HouseSpace = houseSpace;

        int floorsNum = 1;
        if (Int32.TryParse(lstFloorsNum.SelectedValue, out floorsNum))
            hd.FloorsNumber = floorsNum;

        hd.IsBathroom = chkBathroom.Checked;
        hd.IsToilet = chkToilet.Checked;
        hd.IsSwimmingPool = chkSwimmingPool.Checked;
        return hd;
CopyASP.Net - Print ASPX to PDF in VB.Net
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Net
Imports System.Text

Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim id As String = Request.QueryString("id")
        Dim hd As HouseDescription = Nothing

        If id IsNot Nothing Then
            ' Recreate the page state
            hd = CType(Application(id), HouseDescription)
            ' First time loading. Set all to default.
            If Not IsPostBack Then
                SetPageState(New HouseDescription())
            End If
        End If
    End Sub
    Protected Sub GetPDF_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' 1. Save the current page state
        ' 1.1. Create ID
        Dim id As String = Guid.NewGuid().ToString()

        ' 1.2 Save the current state in the Application State.
        Dim hd As HouseDescription = GetPageState()
        Application(id) = hd

        ' 2. Create url
        Dim pageUrl As String = System.Web.HttpContext.Current.Request.Url.AbsoluteUri
        Dim pageUrlWithQuerry As String = String.Format("{0}?id={1}", pageUrl, id)

        ' 3. Get PDF
        Dim v As New SautinSoft.PdfVision()

        ' Set "Edge mode" to support all modern CSS.
        Dim pdf() As Byte = v.ConvertHtmlFileToPDFStream(pageUrlWithQuerry)

        ' 4. Show PDF result
        If pdf IsNot Nothing Then
            Response.Buffer = True
            Response.ContentType = "application/PDF"
            'Response.AddHeader("content-disposition", "attachment; filename=Result.pdf")
            Response.AddHeader("content-disposition", "inline; filename=Result.pdf")
        End If
    End Sub
    Protected Sub SetPageState(ByVal hd As HouseDescription)
        txtHouseSpace.Text = hd.HouseSpace.ToString()
        lstFloorsNum.SelectedValue = hd.FloorsNumber.ToString()
        chkBathroom.Checked = hd.IsBathroom
        chkToilet.Checked = hd.IsToilet
        chkSwimmingPool.Checked = hd.IsSwimmingPool
    End Sub
    Protected Function GetPageState() As HouseDescription
        Dim hd As New HouseDescription()

        Dim houseSpace As Single = 0.0F

        If Single.TryParse(txtHouseSpace.Text, houseSpace) Then
            hd.HouseSpace = houseSpace
        End If

        Dim floorsNum As Integer = 1
        If Int32.TryParse(lstFloorsNum.SelectedValue, floorsNum) Then
            hd.FloorsNumber = floorsNum
        End If

        hd.IsBathroom = chkBathroom.Checked
        hd.IsToilet = chkToilet.Checked
        hd.IsSwimmingPool = chkSwimmingPool.Checked
        Return hd
    End Function
End Class

See Also