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?
32.9k 16 16 gold badges 124 124 silver badges 150 150 bronze badges asked Jun 6, 2018 at 3:18 837 3 3 gold badges 13 13 silver badges 28 28 bronze badges 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:08Why 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:04I 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.
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.
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.).