Generating Web Page to PDF

Background:

Easiest, simplest, fastest and of course free way to generate PDF or images for web pages:

– Tool is named as “wkhtmltopdf” and “wkhtmltoimage”
– Both are in exe format.
– Written in C++ and also its source is open and licensed under open source.
– Because it is an exe, we can use this tool in both windows and web applications. And it is in C++  so it can be used for windows platform as well as in Linux based systems.

In Details:

I will be explaining how to use it in web applications to generate PDF and push that PDF to client. Rest can be taken care by considering this as an example.
You can download the exe from  https://code.google.com/p/wkhtmltopdf/downloads/list. And trust me this is really cool kit as it provides a lot of flexibility to hide or extend the content of the page in nearly every manner.

Now as we know we can invoke any exe from a process object in c# or in vb.net. I am going to take example of vb.net. Because my current project is in vb.net

Here is some code(vb.net):

‘url of the page to be printed
fileToPrintURL  = “www.somesite.com/pagename”
‘this will be the name of the file “.pdf” generated one
Dim filename As String = "Page" + TimeOfDay.Ticks.ToString() + ".pdf"
‘shared folder with write permissions and file name to be generated at which place
Dim filenameWithSharedPAth As String = Common.PDFSharedFolder + filename
‘create an object of process
Dim process As New Process
‘set some initails
process.StartInfo.UseShellExecute = False
process.StartInfo.CreateNoWindow = True
‘now the path of the exe downloaded from the link above
process.StartInfo.FileName = Server.MapPath("~/bin/PrintPDF/") + "wkhtmltopdf.exe"
‘and some arguments (based on req) as I need to pass it from authentication so user name and password
process.StartInfo.Arguments = "--username " + username + " --password " + password + " """ + fileToPrintURL + """ " + """" + filenameWithSharedPAth + """"
‘again some setting
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
‘start the process
process.Start()
‘end the process
process.WaitForExit()

And your file is generated at the specified path with all the UI CSS stuff and images.If you wish to hide some content on a web page on which you are having development control. then pass this CSS to that page (hint :-> I am not spoon feeding :)).

@media print
{
#header { display:none;}
.bar-menu { display:none;}
}

And a lot of help is available on http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html

Written By: Ajay Kumar, ASP.Net Developer, Mindfire Solutions

Advertisements

Posted on September 5, 2013, in ASP.Net and tagged , , , , , , , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: