我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
如果你需要完美的html pdf渲染,你需要使用商业库。
ExpertPdf Html To Pdf Converter非常容易使用,它支持最新的html5/css3。您可以将整个url转换为pdf:
using ExpertPdf.HtmlToPdf;
byte[] pdfBytes = new PdfConverter().GetPdfBytesFromUrl(url);
或者HTML字符串:
using ExpertPdf.HtmlToPdf;
byte[] pdfBytes = new PdfConverter().GetPdfBytesFromHtmlString(html, baseUrl);
您还可以选择直接将生成的pdf文档保存到磁盘上的文件流。
其他回答
到目前为止,似乎最好的免费。net解决方案是TuesPechkin库,它是wkhtmltopdf本机库的包装。
我现在已经使用单线程版本将几千个HTML字符串转换为PDF文件,它似乎工作得很好。它应该也可以在多线程环境中工作(例如IIS),但我还没有对此进行测试。
另外,因为我想使用最新版本的wkhtmltopdf(在编写时为0.12.5),我从官方网站下载了DLL,复制到我的项目根目录,设置copy to output为true,并像这样初始化库:
var dllDir = AppDomain.CurrentDomain.BaseDirectory;
Converter = new StandardConverter(new PdfToolset(new StaticDeployment(dllDir)));
上面的代码看起来完全是“wkhtmltox.dll”,所以不要重命名文件。我使用的是64位版本的DLL。
确保你阅读了多线程环境的说明,因为你只需要在每个应用生命周期中初始化它一次,所以你需要把它放在一个单例或其他东西中。
你可以使用谷歌Chrome打印到pdf功能从它的无头模式。我发现这是最简单但最健壮的方法。
var url = "https://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net";
var chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
var output = Path.Combine(Environment.CurrentDirectory, "printout.pdf");
using (var p = new Process())
{
p.StartInfo.FileName = chromePath;
p.StartInfo.Arguments = $"--headless --disable-gpu --print-to-pdf={output} {url}";
p.Start();
p.WaitForExit();
}
编辑:新建议 使用PdfSharp的PDF HTML渲染器
(在尝试wkhtmltopdf并建议避免它之后)
HtmlRenderer。PdfSharp是一个100%完全c#托管代码,易于使用,线程安全,最重要的是免费(新BSD许可证)的解决方案。
使用
下载HtmlRenderer。PdfSharp nuget包。 使用实例方法。 public static Byte[] PdfSharpConvert(String html) { 字节[]res = null; 使用(内存流ms =新的内存流()) { var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator。GeneratePdf (html、PdfSharp.PageSize.A4); pdf.Save(女士); res = ms.ToArray(); } 返回res; }
一个非常好的替代是iTextSharp的免费版本
在版本4.1.6之前,iTextSharp是在LGPL许可下授权的,而4.16之前的版本(或者也可能有分叉)是作为包提供的,可以自由使用。当然有人可以使用5+付费版本。
我尝试在我的项目中集成wkhtmltopdf解决方案,遇到了一堆障碍。
我个人会避免在托管企业应用程序上使用基于wkhtmltopdf的解决方案,原因如下。
First of all wkhtmltopdf is C++ implemented not C#, and you will experience various problems embedding it within your C# code, especially while switching between 32bit and 64bit builds of your project. Had to try several workarounds including conditional project building etc. etc. just to avoid "invalid format exceptions" on different machines. If you manage your own virtual machine its ok. But if your project is running within a constrained environment like (Azure (Actually is impossible withing azure as mentioned by the TuesPenchin author) , Elastic Beanstalk etc) it's a nightmare to configure that environment only for wkhtmltopdf to work. wkhtmltopdf is creating files within your server so you have to manage user permissions and grant "write" access to where wkhtmltopdf is running. Wkhtmltopdf is running as a standalone application, so its not managed by your IIS application pool. So you have to either host it as a service on another machine or you will experience processing spikes and memory consumption within your production server. It uses temp files to generate the pdf, and in cases Like AWS EC2 which has really slow disk i/o it is a big performance problem. The most hated "Unable to load DLL 'wkhtmltox.dll'" error reported by many users.
——PRE编辑部分——
对于任何想要在更简单的应用程序/环境中从html生成pdf的人,我把我的旧帖子作为建议。
TuesPechkin
https://www.nuget.org/packages/TuesPechkin/
或专为MVC Web应用程序 (但我认为你可以在任何。net应用程序中使用它)
旋转
https://www.nuget.org/packages/Rotativa/
他们都利用了 Wkhtmtopdf二进制转换HTML到pdf。它使用webkit引擎来呈现页面,因此它也可以解析css样式表。
它们提供了易于使用的与c#的无缝集成。
Rotativa还可以从任何Razor View直接生成pdf。
此外,对于现实世界的web应用程序,他们还管理线程安全等…
PDFmyURL最近也发布了一个。net组件,用于网页/ HTML到PDF的转换。它有一个非常友好的用户界面,例如:
PDFmyURL pdf = new PDFmyURL("yourlicensekey");
pdf.ConvertURL("http://www.example.com", Application.StartupPath + @"\example.pdf");
文档:PDFmyURL .NET组件文档
免责声明:我为拥有PDFmyURL的公司工作
你也可以检查Spire,它允许你用这段简单的代码创建HTML到PDF
string htmlCode = "<p>This is a p tag</p>";
//use single thread to generate the pdf from above html code
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
// Save the file to PDF and preview it.
pdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");