Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an HTML that contains HTML controls. On converting it into pdf the html controls are not rendering. Also it fails to apply the styles in external css. Please suggest a method for converting HTML to PDf that satisfies the requirements

What I have tried:

I have tried using iTextSharp and PDFSharp. iTextSharp doest render the html controls and also pdfsharp but it doest have a method to convert html to PDF. I tried with html to image and then image to PDF but a good html to image renderer is not found
Posted
Updated 15-Sep-16 11:27am
Comments
Sergey Alexandrovich Kryukov 25-May-16 22:36pm    
Not only such method does not exist, but it's pretty obvious that it should never exist in such PDF libraries, it would not make sense. No matter what library you are using, this is you who renders something and use or not use some files, not the library; and you failed write what you have tried. I can only assume that you did not try to render the controls and use CSS. Sorry, but at this point it would be pretty hard to help you. I only want to ask you: why rendering controls in PDF? In PDF, you won't be able to use them (excluding rare cases of AcroForms or XFA). The concept of PDF is radically different from the concept of other types of electronic content; PDF is closer to the concept of the document on paper.
—SA

1 solution

C#
public byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;

            MemoryStream ms = new MemoryStream();
            TextReader txtReader = new StringReader(pHTML);

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

            // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
            PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

            // 4: we open document and start the worker on the document
            doc.Open();
            htmlWorker.StartDocument();

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return bPDF;
        }


Try this just pass ur html above . Hope it helps.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900