65.9K
CodeProject is changing. Read more.
Home

Convert HTML to PDF

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.90/5 (10 votes)

Mar 14, 2013

CPOL
viewsIcon

210754

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.

 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