使用iTextSharp将HTML转换为PDF时的绝对位置

时间:2021-10-09 21:09:03

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:

我正在使用iTextSharp将HTML转换为PDF,它似乎不适用于绝对定位的元素。例如,我有这个HTML文件:

<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.

文本未正确定位在生成的PDF文件中。您知道在将HTML转换为PDF时是否可以使用绝对定位的元素?任何允许这种情况的免费解决方案(iTextSharp或其他)都将非常感激。

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

这是我用来执行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.

经过进一步调查,似乎iTextSharp的HTML到PDF转换功能仅限于一些非常简单的HTML文档。有一个很好的Java项目叫Flying Flying,它处理复杂的HTML文档。所以我尝试将它与IKVM一起使用,效果非常好。唯一的问题是它感觉某种肮脏的解决方案。添加31MB的HTML到PDF转换的汇编代码似乎非常多。有没有更好的“免费”替代方案来处理这种情况。

3 个解决方案

#1


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.

我终于决定使用xhtmlrenderer。它完全符合我的需求,它具有许多功能,并且能够正确呈现我的任何HTML文件。

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

目前它只有一个JAVA版本,我不得不将jar转换为带有IKVM的.NET程序集。

#2


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?

假设您使用的是Windows,那么如何通过COM或命令行自动化免费的PDFCreator,以通过Internet Explorer的渲染引擎将HTML呈现为PDF?

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

安装PDFCreator时,C:\ Program Files \ PDFCreator \ COM \文件夹中有许多自动化示例

#3


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:

在二进制发行版中的JAR上使用ikvmc向我展示了很多来自无法创建的类的警告。在包含iKVM核心库来处理Flying Saucer针对Java对象的事实之后,我遇到了以下异常:

`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?

这似乎是Flying Saucer使用的主要对象。你能提供一些如何使用它的来源吗?

#1


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.

我终于决定使用xhtmlrenderer。它完全符合我的需求,它具有许多功能,并且能够正确呈现我的任何HTML文件。

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

目前它只有一个JAVA版本,我不得不将jar转换为带有IKVM的.NET程序集。

#2


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?

假设您使用的是Windows,那么如何通过COM或命令行自动化免费的PDFCreator,以通过Internet Explorer的渲染引擎将HTML呈现为PDF?

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

安装PDFCreator时,C:\ Program Files \ PDFCreator \ COM \文件夹中有许多自动化示例

#3


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:

在二进制发行版中的JAR上使用ikvmc向我展示了很多来自无法创建的类的警告。在包含iKVM核心库来处理Flying Saucer针对Java对象的事实之后,我遇到了以下异常:

`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?

这似乎是Flying Saucer使用的主要对象。你能提供一些如何使用它的来源吗?