Render PDF in Itextsharp from HTML with CSS

Render PDF in iTextSharp from HTML with CSS

It's not possible right now but nothing stops you from starting open-source project that will do it. I might actually start one, because I need it too!

Basically you will need parser that will convert html and css markup into iTextSharp classes. So <table> becames iTextSharp.SimpleTable and so on.

It would be easy to come up with prototype that would be able to work with limited html and css subset.

Update: Until the time this will be possible, this is how I temporarily resolved it for myself. Only two steps:

  • Tell your users to download open-source app called PDFCreator
  • Make all your html reports printer friendly by providing stylesheets for print.

    If some of your multi-page reports need to have headers on every page, set them up in THEAD html tag.

Now users will be able to print-friendly and if they choose PDFCreator printer driver, they will even be able to get report in PDF format (there are other pdf printer drivers but this one is free and open-source).

Also I know HTML is not as flexible as PDF but it might be good enough. I was doing some tests with real users and they actually like it because not only they can now print anything to PDF (even beyond my app), also their workflow is faster because they don't have to download and wait until their pdf reader opens up. they just print (or export to pdf) what they see on website directly from their webbrowser... kind of makes sense.

Create a PDF using iTextSharp from HTML page with external CSS in a Web Service

Thanks to all. I already made some tricks to keep design of html when created pdf in server side with iTextSharp. I add manually some clases to the css resolver just like this:

StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver();
cssResolver.AddCssFile(Context.Server.MapPath("/Content/FOUNDATION/css/PDF.css"), true);
cssResolver.AddCss(".fontSizeTb table, td {font-size: 12px; }", true);
cssResolver.AddCss(".ChangeH3 {font-size: 16px; padding-top: 20px; margin-top: 20px; vertical-align: bottom;}", true);

Render images in iTextSharp HTML5 to PDF

It turns out that iTextSharp does not honor the doctype and instead forces XHTML. According to XHTML, the image tag needs to be closed. If you use it seems to generate without the exception. However, I was not able to get the base64 encoded content to render. There is no error in this case but the image does not show up.

Styles are not getting Implemented in PDF using ITextSharp

This is a known problem with old versions of iText for .NET.

Over two years ago, we abandoned the name iTextSharp in favor of the name iText for .NET.

I've taken your HTML:

<html>
<head>
<style type='text/css'>

body{font-family: Segoe, 'Segoe UI', 'DejaVu Sans', 'Trebuchet MS', Verdana, 'sans-serif'; background: #f2f2f2; margin: 0; padding: 0; font-size: 20px; color: #565656;}
.outerpdfboxCont{width: 100%; background: #fff; margin: 0 auto; padding: 15px 30px; max-width: 80%; display: table;box-shadow: 1px 1px 2px #ddd;}

.blueBorderLine{background: #0A82B4; height: 10px; width: 100%; margin: 10px auto;}

.workingWrapperboxPdf{margin: 10px 0; padding: 10px 0px; display: table;width: 100%; }
.workingWrapperboxPdf h2{font-size: 36px; font-weight: 500; margin: 40px 0 0; line-height: 40px; color: #000;}
.workingWrapperboxPdf h3{font-size: 18px; font-weight: 300;line-height: 20px;}

.darkB{color: #000!important; font-weight: 500!important;}
.DataboxH{margin: 20px 0; display: table;width: 100%;}
.border-heading{border-bottom: 1px solid #ddd; font-size: 30px!important; line-height: 35px!important; color: #000!important;width: 100%; padding-bottom: 10px;}

</style>
</head>
<body>
<div class='outerpdfboxCont'>
<div class='blueBorderLine'></div>

<div class='workingWrapperboxPdf'>

<h3>Dalvir Singh</h3>
<h3 class='darkB'>India</h3>

<div class='DataboxH'>
<h2>Summery Of the PDF</h2>
<h3 class='darkB'>Summery of pdf will go here</h3>
<h3>PDF created on 2018-6-12</h3>
</div>
</div>
</div>
</body>
</html>

An I've opened it in a browser:

Sample Image

Then I've taken iText 7 and the pdfHTML add-on.

I executed the following line of code

HtmlConverter.ConvertToPdf(src, dest);

In this line src refers to the HTML file, and dest refers to the PDF that will be created based on the HTML.

This is what the resulting PDF looks like:

Sample Image

As you can see, the styles are respected. Regarding the font family: you can get one of the fonts with a higher preference by creating a ConverterProperties object that gives the converter access to a font repository. For more info about that feature, please read chapter 6 of the PDF to HTML tutorial.

Summarized: your problem will go away once you upgrade to iText 7 + the pdfHTML add-on. You are using the old XMLWorkerHelper that, as explained in the introduction of the HTML to PDF tutorial, is no longer supported.



Related Topics



Leave a reply



Submit