C# How To Convert Html To a PDF File Using Free Dll in Windows Service - International Days

International Days

Internationally important Days

Breaking

Home Top Ad

Post Top Ad

Thursday, 5 April 2018

C# How To Convert Html To a PDF File Using Free Dll in Windows Service

C# How To Convert Html To a PDF File Using Free Dll in Windows Service

How can we create a PFD file from a HTML with a free tool in Windows service.
Initially, I had tried to create the PDF file from HTML using iTextSharp tool. But it was success in MVC web applications , but in case of storing it in folder, the dll was not quite good. It was creating the PDF file but not taking styles . Then I have decided to use the 'SelectPDF' tool.

The selectPdf is a paid tool but there is a community version for this tool not everyone knows it.
The free version has some limitation in it. That is it only create PDF files that is having 5 pages only.
In My requirement, I have only less than 5 pages. So I have decided to go ahead with the 'SelectPDF' Community free version.

Implementation for this tool is quiet simple , Simply follow the below steps .

Step 1

Create Windows service Please see my previous post Create windows service using tops-help

Step 2

Install the community version of SelectPDF

SelectPDF Community version


Step 3

Use the below code for creating the Sample PDF from HTML File
            string htmLReport = string.Empty;
                string fileName = "ShortForm.pdf";
                htmLReport = "This is a Sample PDF";
                string htmlString = htmLReport;
                string baseUrl = "";
                string pdf_page_size = "A4";
                PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
                    pdf_page_size, true);
                string pdf_orientation = "Portrait";
                PdfPageOrientation pdfOrientation =
                    (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                    pdf_orientation, true);
                int webPageWidth = 1024;
                int webPageHeight = 0;
                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();
                // set converter options
                converter.Options.PdfPageSize = pageSize;
                converter.Options.PdfPageOrientation = pdfOrientation;
                converter.Options.WebPageWidth = webPageWidth;
                converter.Options.WebPageHeight = webPageHeight;
                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);
                // save pdf document
                doc.Save(@"C:\PDFDestinationFolder\Sample.Pdf");
                // close pdf document
                doc.Close();
        
Step 4

Run the code
HTML PDF Conversion Using Free tool c#


Step 5

Check the folder to find the PDF
HTML to PDF Creation in c#


C# How To Convert Html To a PDF File Using Free Dll in Windows Service

No comments:

Post a Comment

Post Bottom Ad

Pages