在ASP.NET中将HTML转换为PDF时保持CSS样式

时间:2022-01-24 01:15:40

I am using ITextSharp to convert a HTML page to PDF.

我正在使用ITextSharp将HTML页面转换为PDF。

However, ITextSharp prints the CSS in the STYLE declaration straight out, ignores stylesheets even when added programatically and only listens to some inline styles (e.g. font-size and color but not background-color).

但是,ITextSharp直接在STYLE声明中打印CSS,即使在以编程方式添加时也忽略样式表,并且只侦听某些内联样式(例如字体大小和颜色但不是背景颜色)。

Is there something I am missing with ITextSharp, or is there a better (and free) way of doing this conversion?

ITextSharp是否缺少某些东西,或者有更好(免费)的方式进行此转换?

Thanks in advance,

提前致谢,

5 个解决方案

#1


5  

Have a look at WKHTMLTOPDF. It is open source, based on webkit and free.

看看WKHTMLTOPDF。它是开源的,基于webkit和免费。

We wrote a small tutorial here.

我们在这里写了一个小教程。

#2


7  

HTML / CSS support in iText / iTextSharp is very basic. It's just not the right tool to convert html to pdf. Take a look at these solutions instead:

iText / iTextSharp中的HTML / CSS支持非常基础。这不是将html转换为pdf的正确工具。请看一下这些解决方案:

These render html to an image. Then you can insert them in your PDF with iTextSharp.

这些将html渲染为图像。然后,您可以使用iTextSharp将它们插入PDF中。

Otherwise you could try converting HTML -> XSL-FO -> PDF, but including CSS there is a whole other thing.

否则你可以尝试转换HTML - > XSL-FO - > PDF,但包括CSS还有另外一件事。

#3


1  

Try PDF Duo .NET converter. You can apply for support if you need a special feature.

试试PDF Duo .NET转换器。如果您需要特殊功能,可以申请支持。

http://www.duodimension.com/html_pdf_asp.net/downloads/html_pdf_net.zip

http://www.duodimension.com/html_pdf_asp.net/downloads/html_pdf_net.zip

#4


0  

ABCpdf provides two HTML rendering engines. One is based around the MSHTML version installed on the system. The other is based around the FireFox Gecko rendering engine.

ABCpdf提供了两个HTML呈现引擎。一个是基于系统上安装的MSHTML版本。另一个是基于FireFox Gecko渲染引擎。

So there's plenty of room for manouver if you want a particular set of features. It's very real world.

因此,如果您想要一组特定功能,那么就有足够的空间供您使用。这是非常真实的世界。

In terms of quality I would just say that we do get a lot of people settling on ABCpdf after trying a lot of different alternatives.

在质量方面,我只想说,在尝试了很多不同的选择后,我们确实让很多人在ABCpdf上定居。

I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)

我在ABCpdf .NET软件组件上工作,所以我的回复可能包含基于ABCpdf的概念。这就是我所知道的。 :-)

#5


0  

Why not use online API? There are plenty of them available and they do the work well, which let you worry about your core work, not how to render a PDF correctly :)

为什么不使用在线API?有很多可用的,他们做得很好,这让你担心你的核心工作,而不是如何正确渲染PDF :)

You mention something "free". It depends on your usage, but most services offer free conversions to start with, ranging from 50 to 250 (and even more). Maybe it would be enough for you?

你提到“免费”的东西。这取决于您的使用情况,但大多数服务提供免费转换,范围从50到250(甚至更多)。也许这对你来说已经足够了?

All you'd have to do then is a basic POST request to the service with your HTML data (or link), and you'll get a PDF in response.

您需要做的就是使用HTML数据(或链接)向服务发出基本的POST请求,然后您将获得PDF作为回应。

Here are a list of API to convert HTML to PDF (not exhaustive):

以下是将HTML转换为PDF的API列表(并非详尽无遗):

#1


5  

Have a look at WKHTMLTOPDF. It is open source, based on webkit and free.

看看WKHTMLTOPDF。它是开源的,基于webkit和免费。

We wrote a small tutorial here.

我们在这里写了一个小教程。

#2


7  

HTML / CSS support in iText / iTextSharp is very basic. It's just not the right tool to convert html to pdf. Take a look at these solutions instead:

iText / iTextSharp中的HTML / CSS支持非常基础。这不是将html转换为pdf的正确工具。请看一下这些解决方案:

These render html to an image. Then you can insert them in your PDF with iTextSharp.

这些将html渲染为图像。然后,您可以使用iTextSharp将它们插入PDF中。

Otherwise you could try converting HTML -> XSL-FO -> PDF, but including CSS there is a whole other thing.

否则你可以尝试转换HTML - > XSL-FO - > PDF,但包括CSS还有另外一件事。

#3


1  

Try PDF Duo .NET converter. You can apply for support if you need a special feature.

试试PDF Duo .NET转换器。如果您需要特殊功能,可以申请支持。

http://www.duodimension.com/html_pdf_asp.net/downloads/html_pdf_net.zip

http://www.duodimension.com/html_pdf_asp.net/downloads/html_pdf_net.zip

#4


0  

ABCpdf provides two HTML rendering engines. One is based around the MSHTML version installed on the system. The other is based around the FireFox Gecko rendering engine.

ABCpdf提供了两个HTML呈现引擎。一个是基于系统上安装的MSHTML版本。另一个是基于FireFox Gecko渲染引擎。

So there's plenty of room for manouver if you want a particular set of features. It's very real world.

因此,如果您想要一组特定功能,那么就有足够的空间供您使用。这是非常真实的世界。

In terms of quality I would just say that we do get a lot of people settling on ABCpdf after trying a lot of different alternatives.

在质量方面,我只想说,在尝试了很多不同的选择后,我们确实让很多人在ABCpdf上定居。

I work on the ABCpdf .NET software component so my replies may feature concepts based around ABCpdf. It's just what I know. :-)

我在ABCpdf .NET软件组件上工作,所以我的回复可能包含基于ABCpdf的概念。这就是我所知道的。 :-)

#5


0  

Why not use online API? There are plenty of them available and they do the work well, which let you worry about your core work, not how to render a PDF correctly :)

为什么不使用在线API?有很多可用的,他们做得很好,这让你担心你的核心工作,而不是如何正确渲染PDF :)

You mention something "free". It depends on your usage, but most services offer free conversions to start with, ranging from 50 to 250 (and even more). Maybe it would be enough for you?

你提到“免费”的东西。这取决于您的使用情况,但大多数服务提供免费转换,范围从50到250(甚至更多)。也许这对你来说已经足够了?

All you'd have to do then is a basic POST request to the service with your HTML data (or link), and you'll get a PDF in response.

您需要做的就是使用HTML数据(或链接)向服务发出基本的POST请求,然后您将获得PDF作为回应。

Here are a list of API to convert HTML to PDF (not exhaustive):

以下是将HTML转换为PDF的API列表(并非详尽无遗):