Click here to Skip to main content
15,881,882 members
Articles / Web Development / HTML
Tip/Trick

Convert HTML to PDF

Rate me:
Please Sign up or sign in to vote.
4.90/5 (11 votes)
15 Mar 2013CPOL 209.2K   18   12
Application converts HTML link into PDF file format

Introduction

This tip is simple and is based on WkHtmlToXSharp library which converts HTML to PDF, here we have made few enhancements to fulfill our needs, hope this effort will help you in some way.

Using the Code

The code is really simple, you just need to create and object and pass the URL to GetUrlPDF() function described below, which returns byte array, from here, it can be converted to your desire stream. Here, we used file stream to put up the show.

C#
WkHtmlToPdfConverter wol = new WkHtmlToPdfConverter();

           // Get PDF in bytes
           Byte[] bufferPDF = wol.GetUrlPDF(url);

           // Convert bytes to stream
           System.IO.FileStream writeStream = new System.IO.FileStream
            ("sample.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write);
           writeStream.Write(bufferPDF, 0, Convert.ToInt32(bufferPDF.Length));
           writeStream.Close();

           // Open PDF file
           System.Diagnostics.Process.Start(@"sample.pdf");

History

  • 14th March, 2013: Initial version
  • 15th March, 2013: Update

License

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


Written By
Software Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation

33 members

Comments and Discussions

 
QuestionGetUrlPDF(URL) does not work fine Pin
Shakeel Abbasi30-Sep-15 9:17
Shakeel Abbasi30-Sep-15 9:17 
QuestionWhere's the source code to your addition? Pin
jnegron21518-Sep-15 5:44
professionaljnegron21518-Sep-15 5:44 
AnswerRe: Where's the source code to your addition? Pin
Syed Hasan Hyder20-Sep-15 21:26
Syed Hasan Hyder20-Sep-15 21:26 
QuestionWhat did you change? Pin
Ihtsham ul haq9-Sep-14 1:34
Ihtsham ul haq9-Sep-14 1:34 
AnswerRe: What did you change? Pin
Syed Hasan Hyder9-Sep-14 6:20
Syed Hasan Hyder9-Sep-14 6:20 
QuestionHow to support Chinese characters or UTF-8 characters ? Pin
gotolnc6-Jul-13 14:42
gotolnc6-Jul-13 14:42 
AnswerRe: How to support Chinese characters or UTF-8 characters ? Pin
gotolnc6-Jul-13 14:47
gotolnc6-Jul-13 14:47 
AnswerRe: How to support Chinese characters or UTF-8 characters ? Pin
Samuel-Summers5-Jul-15 23:30
Samuel-Summers5-Jul-15 23:30 
GeneralMy vote of 5 Pin
Prasad Khandekar15-Mar-13 4:52
professionalPrasad Khandekar15-Mar-13 4:52 
GeneralRe: My vote of 5 Pin
Syed Hasan Hyder17-Mar-13 22:50
Syed Hasan Hyder17-Mar-13 22:50 
GeneralMy vote of 4 Pin
Nerijus_Vax14-Mar-13 3:47
Nerijus_Vax14-Mar-13 3:47 
Overall its nice, but why stop only on parsing url? Why i cant pass a local HTML file or string containing HTML what would be really nice.
GeneralRe: My vote of 4 Pin
Syed Hasan Hyder14-Mar-13 19:14
Syed Hasan Hyder14-Mar-13 19:14 

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.