简体   繁体   中英

HTML to PDF - CSS in iTextSharp

I am trying to build a pdf file from html code using iTextSharp.

Here is a sample of the HTML:

<div style="height:400px; font-family:Courier; font-size:9px">
    herrow
</div>
<span style="padding-left:100px;">
    world
</span>

Running as an html file it works fine putting 400px between hello and world and 100px before world.

Here is the code I am using to convert the html to pdf:

    List<IElement> htmlList = HTMLWorker.ParseToList(new StringReader(htmlText), new StyleSheet());

    foreach (IElement element in htmlList)
    {
        doc.Add(element);
    }

It does apply the font-family and font-size in the resulting pdf.
However, it ignores the div height and the span width in the pdf that is produced.

Is it possible to use heights / widths / padding etc., when converting html to pdf with iTextSharp?

If not is there a work around, or another free .dll that I can use to do this?

Just pull out height attribute outside the style tag and use it like this:-

<div height='400' style="font-family:Courier;font-size:9px">

herrow

</div>

Instead of using iTextSharp, you may want to take a look at PhantomJS. It's a different approach but it is really helpfull! No more problems with css.

Take a look at this: phantomJS and this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM