C# 在 .NET 中将 HTML 转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/564650/
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
Convert HTML to PDF in .NET
提问by
I want to generate a PDF by passing HTML contents to a function. I have made use of iTextSharp for this but it does not perform well when it encounters tables and the layout just gets messy.
我想通过将 HTML 内容传递给函数来生成 PDF。我为此使用了 iTextSharp,但是当它遇到表格并且布局变得混乱时它表现不佳。
Is there a better way?
有没有更好的办法?
回答by Rune Grimstad
It depends on any other requirements you have.
这取决于您的任何其他要求。
A really simple but not easily deployable solution is to use a WebBrowser control to load the Html and then using the Print method printing to a locally installed PDF printer. There are several free PDF printers available and the WebBrowser control is a part of the .Net framework.
一个非常简单但不容易部署的解决方案是使用 WebBrowser 控件加载 Html,然后使用 Print 方法打印到本地安装的 PDF 打印机。有几种免费的 PDF 打印机可用,WebBrowser 控件是 .Net 框架的一部分。
EDIT: If you Html is XHtml you can use PDFizerto do the job.
编辑:如果您的 Html 是 XHtml,您可以使用PDFizer来完成这项工作。
回答by Stewart
Winnovativeoffer a .Net PDF library that supports HTML input. They offer an unlimited free trial. Depending on how you wish to deploy your project, this might be sufficient.
Winnovative提供了一个支持 HTML 输入的 .Net PDF 库。他们提供无限制的免费试用。根据您希望如何部署项目,这可能就足够了。
回答by Stewart
If you don't really need a true .Net PDF library, there are numerous free HTML to PDF tools, many of which can run from a command-line.
如果您真的不需要真正的 .Net PDF 库,那么有许多免费的 HTML 到 PDF 工具,其中许多可以从命令行运行。
One solution would be to pick one of those and then write a thin wrapper around that in C#. E.g., as done in this tutorial.
一种解决方案是选择其中一个,然后在 C# 中围绕它编写一个薄包装器。例如,正如本教程中所做的那样。
回答by enriquein
I was also looking for this a while back. I ran into HTMLDOC http://www.easysw.com/htmldoc/which is a free open source command line app that takes an HTML file as an argument and spits out a PDF from it. It's worked for me pretty well for my side project, but it all depends on what you actually need.
不久前我也在寻找这个。我遇到了 HTMLDOC http://www.easysw.com/htmldoc/,这是一个免费的开源命令行应用程序,它将 HTML 文件作为参数并从中输出 PDF。它对我的业余项目非常有效,但这一切都取决于您的实际需要。
The company that makes it sells the compiled binaries, but you are free to download and compile from source and use it for free. I managed to compile a pretty recent revision (for version 1.9) and I intend on releasing a binary installer for it in a few days, so if you're interested I can provide a link to it as soon as I post it.
制作它的公司出售编译好的二进制文件,但您可以免费下载和编译源代码并免费使用它。我设法编译了一个相当新的修订版(版本 1.9),我打算在几天内发布一个二进制安装程序,所以如果你有兴趣,我可以在发布后立即提供它的链接。
Edit (2/25/2014): Seems like the docs and site moved to http://www.msweet.org/projects.php?Z1
编辑 (2/25/2014):似乎文档和站点已移至http://www.msweet.org/projects.php?Z1
回答by Constantine
Try this PDF Duo .Netconverting component for converting HTML to PDF from ASP.NETapplication without using additional dlls.
试试这个PDF Duo .Net转换组件,用于将HTML 从 ASP.NET应用程序转换为 PDF,而无需使用额外的 dll。
You can pass the HTML string or file, or stream to generate the PDF. Use the code below (Example C#):
您可以传递 HTML 字符串或文件,或流来生成 PDF。使用以下代码(示例 C#):
string file_html = @"K:\hdoc.html";
string file_pdf = @"K:\new.pdf";
try
{
DuoDimension.HtmlToPdf conv = new DuoDimension.HtmlToPdf();
conv.OpenHTML(file_html);
conv.SavePDF(file_pdf);
textBox4.Text = "C# Example: Converting succeeded";
}
Info + C#/VB examples you can find at: http://www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx
您可以在以下位置找到信息 + C#/VB 示例:http: //www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx
回答by jjchiw
回答by Marko
Update:I would now recommend PupeteerSharpover wkhtmltopdf.
更新:我现在推荐PupeteerSharp 而不是 wkhtmltopdf。
Try wkhtmtopdf. It is the best tool I have found so far.
试试wkhtmtopdf。这是迄今为止我发现的最好的工具。
For .NET, you may use this small libraryto easily invoke wkhtmtopdf command line utility.
对于 .NET,您可以使用这个小型库轻松调用 wkhtmtopdf 命令行实用程序。
回答by AffineMesh
To convert HTML to PDF in C# use ABCpdf.
要将 HTML 转换为 C# 中的 PDF,请使用 ABCpdf。
ABCpdf can make use of the Gecko or Trident rendering engines, so your HTML table will look the same as it appears in FireFox and Internet Explorer.
ABCpdf 可以使用 Gecko 或 Trident 渲染引擎,因此您的 HTML 表格将与在 FireFox 和 Internet Explorer 中显示的相同。
There's an on-line demo of ABCpdf at www.abcpdfeditor.com. You could use this to check out how your tables will render first, without needing to download and install software.
在 www.abcpdfeditor.com 上有 ABCpdf 的在线演示。您可以使用它来检查您的表格将如何首先呈现,而无需下载和安装软件。
For rendering entire web pages you'll need the AddImageUrl or AddImageHtml functions. But if all you want to do is simply add HTML styled text then you could try the AddHtml function, as below:
要渲染整个网页,您需要 AddImageUrl 或 AddImageHtml 函数。但是,如果您只想添加 HTML 样式的文本,那么您可以尝试使用 AddHtml 函数,如下所示:
Doc theDoc = new Doc();
theDoc.FontSize = 72;
theDoc.AddHtml("<b>Some HTML styled text</b>");
theDoc.Save(Server.MapPath("docaddhtml.pdf"));
theDoc.Clear();
ABCpdf is a commercial software title, however the standard edition can often be obtained for free under special offer.
ABCpdf 是一个商业软件标题,但标准版通常可以通过特价免费获得。
回答by Mike
PDF Visionis good. However, you have to have Full Trust to use it. I already emailed and asked why my HTML wasn't being converted on the server but it worked fine on localhost.
PDF 视觉很好。但是,您必须拥有完全信任才能使用它。我已经通过电子邮件发送并询问为什么我的 HTML 没有在服务器上转换,但它在 localhost 上运行良好。
回答by charpdevel0p3r
I used ExpertPDF Html To Pdf Converter. Does a decent job. Unfortunatelly, it's not free.
我使用 ExpertPDF Html To Pdf Converter。做一份体面的工作。不幸的是,它不是免费的。