0

I have a html document with css defined within the head tags. I want this html string to converted into a pdf.

I have used ABC pdf and SelectPDF dlls and generated the pdf.

When I used ABC pdf it does not applied any CSS styles when converting to pdf. SelectPDF has applied the CSS styles to the pdf but its bit messy.

Does any one know how to properly convert HTML to pdf?

7
  • You could try itextsharp Commented Jun 6, 2018 at 4:07
  • @JamieD77: Thanks, I tried itextsharp following a tutorial, the uses HTMLWorker, but when I used it, throws me an nullpointer exception. Someone in that tutorial suggested to use XMLWorker class, but I could not find how to use that class, do you know about it ?. Commented Jun 6, 2018 at 5:08
  • Why not using online API to do the job? They are mostly free if you don't have a high usage and performs well. PDFShift.io, HTML2PDFRocket and PDFCrowd to name a few. Commented Jun 6, 2018 at 8:33
  • @CyrilN.: Thanks for your comment. All 3 are good links. But HTML2PDFRocket is not rendering the css properly, the pdf does not contain the css rules I have mentioned in the html. PDFCrowd is rendered the HTML and css properly but its not take full height the of the page. At the moment I am messing around with ABCpdf, lets see how it goes. I will post the code if I was able to render the HTML with css properly. Meanwhile if you know any other tutorials let me know please. Commented Jun 7, 2018 at 22:44
  • @ChamalPerera Have you tried PDFShift? How did it render your documents? Commented Jun 8, 2018 at 9:04

1 Answer 1

2

I have found a solution to convert a HTML string with inline style to PDF. I have done using ABCpdf version 11. This solution is given by the technical team at ABCpdf. I have tried many numerous libraries and online solutions (where I can pass my HTML string to service and get the pdf) but none has given me a good output including the above commented solutions. So here is the solution for the HTML to pdf conversion.

<html>
<meta charset="utf-8" />
<head><head>
<body style="height: 100%;background-color: #D7CCC8;font-size: 12px;position: relative;height: 100%;margin: 0;">
<div style='position: relative;min-height: 100%;padding: 1em 1em 2em;margin-bottom: -11em;'>

put the content that you want to be in the pdf(with inline styling the html elements). This is an example of the html string that needs to be converted into a pdf.


</div>
</body>
</html>

Following is the C# code to convert the above HTML string to a pdf.

            //generate pdf
            using (Doc pdfDocument = new Doc())
            {
                // Set HTML options
                pdfDocument.HtmlOptions.Engine      = EngineType.Gecko;
                pdfDocument.HtmlOptions.Media       = MediaType.Screen;
                // Convert first HTML page, result: html string
                int         pageID                  = pdfDocument.AddImageHtml(result);

                // Convert other HTML pages
                while (true)
                {
                    if (!pdfDocument.Chainable(pageID))
                    {
                        break;
                    }

                    pdfDocument.Page                = pdfDocument.AddPage();
                    pageID                          = pdfDocument.AddImageToChain(pageID);
                }

                //save
                for (int i = 0; i < pdfDocument.PageCount; i++)
                {
                    pdfDocument.PageNumber          = i;
                    pdfDocument.Flatten();
                }

                //save the pdf, pdfFullPath: path to save the pdf
                pdfDocument.Save(pdfFullPath);
            }

The above code will convert the html string to pdf. NOTE: in my html I did not have any images and all the styles were mentioned inline, like in the example.

Hope the above solution will help someone as it did for me. Anyone is welcomed to suggest any improvements for this code (e.g: insert images, complex html to pdf conversion etc.).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.