Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
I need to convert Html file into PDF file using c#
 
please, help ASAP.
Thank you.
Posted 20-Jul-12 3:22am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can convert html page to pdf using wkhtmltopdf opensource library.
 
here is the implementation
Html to PDF in c#
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Use itextsharp library to convert simple html to pdf.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There are many solutions can convert html to PDF. However they have different effects. For some converters, they are quick and free, but many can export only simple html webpage without dynamic images, while for other non converters, such as iTexsharp or other libraries, they can export complex webpage to PDF. As far as I know, the simplest solution that I ever seen is the code as below:
 
namespace htmltopdf
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            String url = “Http://apple.com/”;
            doc.LoadFromHTML(url, false, true, true);
            doc.SaveToFile(“webpageaspdf.pdf”);
            doc.Close();
            System.Diagnostics.Process.Start(“webpageaspdf.pdf”);
        }
    }
}
 
You can see details about which tool is best suitable for you:
Save Webpage as PDF[^]
 
I belive this works only for Windows 8.1.
  Permalink  
v2
Comments
Pro Idiot at 28-Nov-12 22:54pm
   
Vote of 5 .
Lacy00 at 28-Nov-12 22:57pm
   
Thanks!
nika2008 at 17-Mar-13 18:55pm
   
simple way, best way +5
swift.D at 23-Oct-14 5:28am
   
Will this need a dll?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 14

Below is an example that turns the google homepage into a PDF.  I have created a few extra examples in different languages (php, ruby etc) on my website plus some extra c sharp examples, including MVC, converting html to pdf but streaming the file direct to the user, or as an attachment.  The API I created is used in commercial products and can be used freely for personal or commercial use.  The documentation is here:
http://www.html2pdfrocket.com/Examples/CSharp
public void Run()
{
    string apiKey = "ABCD-1234";
    string value = "http://www.google.com"; // a url or html string

    using (var client = new WebClient())
    {
        // Build the conversion options
        NameValueCollection options = new NameValueCollection();
        options.Add("apikey", apiKey);
        options.Add("value", value);
 
        // Call the free API convert to an image
        byte[] result = client.UploadValues("http://api.html2pdfrocket.com/pdf", options);
 
        // Save the image to disk
        System.IO.File.WriteAllBytes(Server.MapPath(Path.Combine("~/", @"c:\temp\myimage.jpg")), result);
    }
}
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 21 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100