views:

1210

answers:

4

I am using iTextSharp to convert HTML to PDF and it doesn't seem to work with absolutely positioned elements. For example I have this HTML file:

<html>
<body>
    <p style="position: absolute; left: 10px; top: 100px; width: 50px;">Hello World</p>
</body>
</html>

The text is not correctly positioned in the resulting PDF file. Do you know if it is possible to have absolutely positioned elements when converting HTML to PDF? Any free solution (iTextSharp or other) that allows this would be greatly appreciated.

Here's the code I am using to perform the conversion with iTextSharp:

class Program
{
    static void Main(string[] args)
    {
        Document document = new Document(PageSize.A4);
        using (Stream output = new FileStream("out.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
        using (Stream htmlStream = new FileStream("input.htm", FileMode.Open, FileAccess.Read, FileShare.Read))
        using (XmlTextReader reader = new XmlTextReader(htmlStream))
        {
            PdfWriter.GetInstance(document, output);
            HtmlParser.Parse(document, reader);
        }
        Process.Start(@"C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe", "out.pdf");
    }
}


EDIT:

After further investigation it seems that iTextSharp's HTML to PDF conversion capability is limited to some very simple HTML documents. There's a nice Java project called Flying Saucer which handles complex HTML documents. So I tried using it with IKVM and it worked very well. The only problem is that it feels somehow a dirty solution. Adding 31MB of assembly code for HTML to PDF conversion seems quite much. Are there any better and "free" alternatives to handle this scenario.

A: 

Assuming you are on Windows, how about automating the free PDFCreator through COM or the command line to render HTML to PDF via Internet Explorer's rendering engine?

There are lots of automation examples in the C:\Program Files\PDFCreator\COM\ folder when PDFCreator is installed

Matthew Lock
+2  A: 

I finally decided to use xhtmlrenderer. It perfectly fits my needs, it has many features and it was able to correctly render any of my HTML files.

As currently it has only a JAVA version I had to convert the jars into a .NET assembly with IKVM.

Darin Dimitrov
This seems like a solution to a problem I'm facing at the moment. How difficult is the JAR to DLL conversion using IKVM, and have you come across any problems?
TonE
The JAR to DLL conversion is a simple matter of using ikvmc.exe at the command line. I didn't face any problems and it is now more than 2 months that this component works in production without a single failure.
Darin Dimitrov
Thanks, I will give this a try then!
TonE
A: 

Using ikvmc on the JARs in the binary distribution showed me a lot of warnings from classes that couldn't be made. After including the iKVM core libraries to deal with the fact that Flying Saucer targets Java objects, I ran into the following exception:

`Cannot load AWT toolkit: ikvm.awt.NetToolkit, IKVM.AWT.WinForms, Version=0.40.0.1, Culture=neutral, PublicKeyToken=13235d27fcbfff58`

and that was just from executing:

`ITextRenderer toRender = new ITextRenderer();`

Which seems to be the main object used by Flying Saucer. Can you provide some source for how you used it?

Have you referenced IKVM.AWT.WinForms.dll so that it is copied to the bin\Release folder?
Darin Dimitrov
A: 

I tried your approach of using IKVM but when I am trying to create an instance of ITextRenderer it says `NoClassDefFoundError com.lowagie.text.DocumentException'

I referenced the IKVM dlls aswell as itext and core-renderer dlls that come with flying saucer

How did you avoid this?

Drahcir