Catalin Florea
Catalin Florea

Reputation: 508

HTML to PDF in c#

I'm trying to create an application that converts a file from the HTML format to the PDF format.

The approach I am using is:

  1. HTML to XHTML
  2. XHTML to Formatting Object
  3. Formatting Object to PDF

I'm having a bit of trouble with the whole XHTML to FO(or xsl).

Can you please tell me how to transform the XHTML to FO?

Or maybe a different approapch to the whole HTML to PDF?

Thanks, Catalin

Upvotes: 3

Views: 5952

Answers (5)

Hardik Solgama
Hardik Solgama

Reputation: 13

here I give you one code which provide html to pdf file in .net. Install one nuget package which is the Select.HtmlToPdf.NetCore.

private static void Main(string[] args)
{
    // instantiate the html to pdf converter
    HtmlToPdf converter = new HtmlToPdf();
    
    //get html file path
    var html = File.ReadAllText("C:\\Users\\..\\index.html");

    // convert the url to pdf
    PdfDocument doc = converter.ConvertHtmlString(html);
    
    //save pdf file path
    var file = "C:\\Users\\..\\index.pdf";
    
    // save pdf document
    doc.Save(file);

    // close pdf document
    doc.Close();
}

Upvotes: 0

Ali Jago Seif
Ali Jago Seif

Reputation: 1

Here is the different approach. We are going to convert HTML/XML to PDF directly with 3d party tool (it has multiple preference and settings of conversion and doesn't require any external libraries).

1) Download free HTML to PDF SDK from her (it is easy PDF SDK)

2) Use the following code or run Action Center to customize the conversion

using BCL.easyPDF.Printer;

namespace TestPrinter
{
   class Program
   {
      static void Main(string[] args)
      {
         if(args.Length != 2)
            return;

         string inputFileName = args[0];
         string outputFileName = args[1];
         Printer printer = new Printer();
         try
         {
            IEPrintJob printjob = printer.IEPrintJob;
            printjob.PrintOut(inputFileName, outputFileName);
         }
         catch(PrinterException ex)
         {
            System.Console.WriteLine(ex.Message);
         }
         finally
         {
            printer.Dispose();
         }
      }
   }
}

Image: HTML to PDF C# API - Action Center

Upvotes: 0

Sanjay Dwivedi
Sanjay Dwivedi

Reputation: 749

I have write easiest way to write html to pdf code using NRerco Pdf library which available free, Install nuget package

PM > Install-Package NReco.PdfGenerator

   Create HtmltoPdf()
   {
   if (System.IO.File.Exists("HTMLFile.html"))
    {
        System.IO.File.Delete("HTMLFile.html");
    }

    System.IO.File.WriteAllText("HTMLFile.html", html);
    var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
    if (System.IO.File.Exists("export.pdf"))
    {
        System.IO.File.Delete("export.pdf");
    }

    htmlToPdf.GeneratePdfFromFile("HTMLFile.html", null, "export.pdf");
 }

Upvotes: 3

Shoban
Shoban

Reputation: 23016

Searched a lot for my personal stack app project SO2PDF and finally settled with wkhtmltopdf which so far is the best free tool to convert HTML to PDF. Yes I used it with c# ;-)

Upvotes: 1

Olical
Olical

Reputation: 41422

Well you could use a HTML to PDF converter via shell, I am sorry I can not rememeber the name of the one I have used in the past, if you have a Google around, you should be able to find a good one.

Upvotes: 1

Related Questions