Click here to Skip to main content
13,505,141 members
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 7 Sep 2013

Web Page to PDF or Image

, 7 Sep 2013
Rate this:
Please Sign up or sign in to vote.
How to convert a web page to PDF or image


This tip shows you how to generate a web page to PDF. This is the easiest, simplest, fastest and of course, free way to generate PDF or images for web pages. The tool is named as “wkhtmltopdf” and “wkhtmltoimage”. Both are in EXE format. It is 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 because it is in C++, it can be used for Windows platform as well as in Linux based systems.

I will be explaining how to use it in web applications to generate PDF and push that PDF to client.

The rest can be taken care of by considering this as an example.

You can download the EXE from here.

And trust me, this is a 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 an example of VB.NET because my current project is in VB.NET. Here is some code:

url of the page to be printed
Dim fileToPrintURL As String = ""
Dim PDFSharedFolder As String = "e:\\"

'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 = PDFSharedFolder + filename

'create an object of process 
Dim process As New Process

'set some initials
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

'end the process

Your file is now 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 dev control, then pass this CSS to that page (Hint: I am not spoonfeeding).

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

And a lot of help is available here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chief Technology Officer Infocouture Solutions Pvt Ltd
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Answerusing this you can convert multiple as well. Pin
me.ajaykumar9-Jan-14 19:20
memberme.ajaykumar9-Jan-14 19:20 
QuestionWhat if I want to convert a group of web pages into a multi-page PDF file Pin
Member 105101417-Jan-14 19:57
memberMember 105101417-Jan-14 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 7 Sep 2013
Article Copyright 2013 by me.ajaykumar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid