Generate PDF from html, webpage using pechkin in asp.net c#

Pechkin is a .NET Wrapper for WkHtmlToPdf DLL, library that uses Webkit engine to convert HTML pages to PDF.

Pechkin have a class named Factory which having a method SetPageUri you can pass webpage url in it then it will download the content and convert it to pdf and return byte of array, you can save this pdf byte of array using file system (system.io) or direct write to response stream to get it download in client system.

Webpage to pdf :-
using Pechkin;

var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
                        .SetLoadImages(true).SetZoomFactor(1.5)
                        .SetPrintBackground(true)
                        .SetScreenMediaType(true)
                        .SetCreateExternalLinks(true)

                        .SetPageUri(tbUrl.Text));

Or you can pass direct html in Factory Create method to convert it to pdf  byte of array
Html to pdf :-
using Pechkin;

var pechkin = Factory.Create(new GlobalConfig());
var pdf = pechkin.Convert(new ObjectConfig()
                        .SetLoadImages(true).SetZoomFactor(1.5)
                        .SetPrintBackground(true)
                        .SetScreenMediaType(true)
                        .SetCreateExternalLinks(true), html);

You can also get html from webpage through .net WebClient class and modify as you need and pass it to pechking.

Webpage to html :-
string html;

#region Read the HTML content

if (string.IsNullOrEmpty(tbUrl.Text.Trim()))
{
    throw new ApplicationException("The URL is empty.");
}

using (var client = new WebClient())
{
    html = client.DownloadString(tbUrl.Text);
}

#endregion


Dwonload pdf byte of array :-

Response.ContentType = "application/pdf";

Response.AddHeader("Content-Disposition"string.Format("attachment;filename=pechkintest.pdf; size={0}", pdf.Length));

Response.BinaryWrite(pdf);


You have to set your enable 32 bit option true in your application pool setting, because pechkin working in 32 bit system.

Find more about pechkin, and download latest dll or code from pechkin site link https://pechkinwebtest.codeplex.com/

Download Sample Code Click Here

Post a Comment

0 Comments

Close Menu