C# 具有完整 CSS 支持的开源 HTML 到 PDF 渲染器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/430590/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 03:05:01  来源:igfitidea点击:

Open Source HTML to PDF Renderer with Full CSS Support

c#htmlcsspdfpdf-generation

提问by Adam Haile

I asked about getting iTextSharp to render a PDF from HTML and a CSS sheet before herebut it seems like that may not be possible... So I guess I will have to try something else.

我在这里之前询问过让 iTextSharp 从 HTML 和 CSS 表渲染 PDF,但似乎这可能不可能......所以我想我将不得不尝试其他方法。

Is there an open source .NET/C# library out there that can take HTML andCSS as input and render it correctly?

是否有开源 .NET/C# 库可以将 HTMLCSS 作为输入并正确呈现?

I must reiterate... the library MUST be free and preferably something with a fairly liberal license. I'm working with basically no budget here.

我必须重申......图书馆必须是免费的,最好是具有相当自由许可的东西。我在这里工作基本上没有预算。

采纳答案by Norman Ramsey

I've always used it on the command line and not as a library, but HTMLDOCgives me excellent results, and it handles at least someCSS (I couldn't easily see how much).

我一直在命令行上使用它而不是作为库使用它,但是HTMLDOC给了我很好的结果,并且它至少处理了一些CSS(我不容易看到有多少)。

Here's a sample command line

这是一个示例命令行

htmldoc --webpage -t pdf --size letter --fontsize 10pt index.html > index.pdf

回答by Charlie Martin

It's not open source, but you can at least get a free personal use license to Prince, which really does a lovely job.

它不是开源的,但您至少可以获得Prince的免费个人使用许可,这确实做得很好。

回答by azerttyu

回答by AffineMesh

Try ABCpdf from webSupergoo. It's a commercial solution, not open source, but the standard edition can be obtained free of charge and will do what you are asking.

试试来自 webSupergoo 的 ABCpdf。这是一个商业解决方案,不是开源的,但标准版可以免费获得,并且可以满足您的要求。

ABCpdf fully supports HTML and CSS, live forms and live links. It also uses Microsoft XML Core Services (MSXML) while rendering, so the results should match exactly what you see in Internet Explorer.

ABCpdf 完全支持 HTML 和 CSS、实时表单和实时链接。它还在呈现时使用 Microsoft XML 核心服务 (MSXML),因此结果应该与您在 Internet Explorer 中看到的完全匹配。

The on-line demo can be used to test HTML to PDF rendering without needing to install any software. See: http://www.abcpdfeditor.com/

在线演示可用于测试 HTML 到 PDF 的渲染,无需安装任何软件。见:http: //www.abcpdfeditor.com/

The following C# code example shows how to render a single page HTML document.

以下 C# 代码示例展示了如何呈现单页 HTML 文档。

Doc theDoc = new Doc();
theDoc.AddImageUrl("http://www.example.com/");
theDoc.Save("htmlimport.pdf");
theDoc.Clear();

To render multiple pages you'll need the AddImageToChain function, documented here: http://www.websupergoo.com/helppdf7net/source/5-abcpdf6/doc/1-methods/addimagetochain.htm

要呈现多个页面,您需要 AddImageToChain 函数,此处记录:http: //www.websupergoo.com/helppdf7net/source/5-abcpdf6/doc/1-methods/addimagetochain.htm

回答by isaac

This command line tool is the business! https://wkhtmltopdf.org/

这个命令行工具就是生意! https://wkhtmltopdf.org/

It uses webkit rendering engine(used in safari and KDE), I tested it on some complex sites and it was by far better than any other tool.

它使用 webkit 渲染引擎(在 safari 和 KDE 中使用),我在一些复杂的站点上对其进行了测试,它比任何其他工具都要好得多。

回答by James

We have been using html2pdf for generating highly styled PDF documents with custom fonts, for a few years now. It's a little hard to get set up, but once it is, it works great... and it's free.

几年来,我们一直在使用 html2pdf 生成带有自定义字体的高度样式化的 PDF 文档。设置起来有点困难,但是一旦设置,它就很好用……而且它是免费的。

http://www.tufat.com/s_html2ps_html2pdf.htm

http://www.tufat.com/s_html2ps_html2pdf.htm

回答by Pablo Ruiz García

You could try my wkhtmltopdf wrapper: https://github.com/pruiz/WkHtmlToXSharp;)

你可以试试我的 wkhtmltopdf 包装器:https: //github.com/pruiz/WkHtmlToXSharp;)