在 ASP.NET 中将 HTML 转换为 PDF 时保持 CSS 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/415535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maintain CSS styling when converting HTML to PDF in ASP.NET
提问by Matt Mitchell
I am using ITextSharp to convert an HTML page to PDF.
我正在使用 ITextSharp 将 HTML 页面转换为 PDF。
However, ITextSharp prints the CSS in the STYLE declaration straight out, ignores stylesheets even when added programmatically and only listens to some inline styles (e.g. font-size
and color
but not background-color
).
然而,ITextSharp 直接在 STYLE 声明中打印 CSS,即使以编程方式添加时也会忽略样式表,并且只侦听某些内联样式(例如font-size
and color
but not background-color
)。
Is there something I am missing with ITextSharp, or is there a better (and free) way of doing this conversion?
ITextSharp 有什么我遗漏的地方,还是有更好(且免费)的方式来进行这种转换?
Thanks in advance,
提前致谢,
采纳答案by Mic
回答by Mauricio Scheffer
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 的正确工具。看看这些解决方案:
- Create screenshot of the page with Watin-like tool
- http://blog.taiki.be/index.php/2008/07/generating-screenshots-of-webpages-using-net/
- http://www.codegod.de/WebAppCodeGod/Screenshot-of-Webpage-with-ASP-NET-AID398.aspx
- 使用类似 Watin 的工具创建页面截图
- http://blog.taiki.be/index.php/2008/07/generating-screenshots-of-webpages-using-net/
- http://www.codegod.de/WebAppCodeGod/Screenshot-of-Webpage-with-ASP-NET-AID398.aspx
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 则是另一回事。
回答by Constantine
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
回答by OnceUponATimeInTheWest
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 的概念。这只是我所知道的。:-)
回答by Cyril N.
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 列表(并非详尽无遗):
- PDFShift
- HTML2PdfRocket
- Web2PDFConvert
- PDFonline
- ... (and many more :) )
- PDFShift
- HTML2PdfRocket
- Web2PDF转换
- PDF在线
- ... (还有很多 :) )