Converts HTML string to PDF byte array

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

Syntax

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

Parameters

inputHtmlString
String
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

Remarks

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="http://www.mysite.com/images/picture1.jpg" >.

Examples

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"];

        if (id != null)
        {
            // Recreate the page state
            HouseDescription hd = (HouseDescription)Application[id];
            if (hd != null)
                SetPageState(hd);
        }
    }
    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.
        SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry();
        byte[] pdf = v.ConvertHtmlFileToPDFStream(pageUrlWithQuerry);

        // 4. Show PDF result
        if (pdf != null)
        {
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "application/PDF";
            //Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf");
            Response.AddHeader("Content-Disposition:", "inline; filename=Result.pdf");
            Response.BinaryWrite(pdf);
            Response.Flush();
            Response.End();
        }
    }
    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;

        chkBathroom.Checked = hd.IsBathroom;
        chkToilet.Checked = hd.IsToilet;
        chkSwimmingPool.Checked = hd.IsSwimmingPool;

        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")

        If id IsNot Nothing Then
            ' Recreate the page state
            Dim hd As HouseDescription = CType(Application(id), HouseDescription)
            If hd IsNot Nothing Then
                SetPageState(hd)
            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.
        SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry()
        Dim pdf() As Byte = v.ConvertHtmlFileToPDFStream(pageUrlWithQuerry)

        ' 4. Show PDF result
        If pdf IsNot Nothing Then
            Response.Buffer = True
            Response.Clear()
            Response.ContentType = "application/PDF"
            'Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf");
            Response.AddHeader("Content-Disposition:", "inline; filename=Result.pdf")
            Response.BinaryWrite(pdf)
            Response.Flush()
            Response.End()
        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

        chkBathroom.Checked = hd.IsBathroom
        chkToilet.Checked = hd.IsToilet
        chkSwimmingPool.Checked = hd.IsSwimmingPool

        Return hd
    End Function
End Class

See Also