1

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?

3
  • 1
    HTMLWorker is deprecated and not supported anymore, instead look into XMLWorker. demo.itextsupport.com/xmlworker/itextdoc/flatsite.html Commented Jul 15, 2013 at 19:28
  • same problem here... Is that use of XMLWorker will solve this?? how to use it?? Commented Jul 16, 2013 at 13:10
  • 1
    Try PrinceXML: a replacement and/or a testing for your html2pdf renderization. Commented Nov 1, 2014 at 0:31

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

-1

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.

1 Comment

that would create an image? in that case the pdf wouldn't have selectable text, right?

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.