在 .NET 中将 HTML 转换为 PDF [英] Convert HTML to PDF in .NET

查看:43
本文介绍了在 .NET 中将 HTML 转换为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将 HTML 内容传递给函数来生成 PDF.我为此使用了 iTextSharp,但是当它遇到表格并且布局变得凌乱时,它的表现不佳.

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.

有更好的方法吗?

推荐答案

新建议使用 PdfSharp 的 PDF 的 HTML 渲染器

(在尝试 wkhtmltopdf 并建议避免它之后)

HtmlRenderer.PdfSharp 是 100% 完全 C# 托管的代码易于使用,线程安全,最重要的是 免费 (新 BSD 许可证) 解决方案.

HtmlRenderer.PdfSharp is a 100% fully C# managed code, easy to use, thread safe and most importantly FREE (New BSD License) solution.

用法

  1. 下载 HtmlRenderer.PdfSharp nuget 包.
  2. 使用示例方法.

  1. Download HtmlRenderer.PdfSharp nuget package.
  2. Use Example Method.

public static Byte[] PdfSharpConvert(String html)
{
    Byte[] res = null;
    using (MemoryStream ms = new MemoryStream())
    {
        var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
        pdf.Save(ms);
        res = ms.ToArray();
    }
    return res;
}

非常好的替代方案免费版本iTextSharp

直到 4.1.6 版 iTextSharp 是在 LGPL 许可下获得许可的,并且直到 4.16 版(或者可能还有分叉)都可以作为包提供并且可以免费使用.当然有人可以继续使用 5+ 付费版.

Until version 4.1.6 iTextSharp was licensed under the LGPL licence and versions until 4.16 (or there may be also forks) are available as packages and can be freely used. Of course someone can use the continued 5+ paid version.

我尝试将 wkhtmltopdf 解决方案集成到我的项目中,但遇到了很多障碍.

I tried to integrate wkhtmltopdf solutions on my project and had a bunch of hurdles.

出于以下原因,我个人会避免在托管企业应用程序上使用基于 wkhtmltopdf 的解决方案.

I personally would avoid using wkhtmltopdf - based solutions on Hosted Enterprise applications for the following reasons.

  1. 首先,wkhtmltopdf 是 C++ 实现的,而不是 C#,你会将其嵌入到 C# 代码中时遇到各种问题,特别是在 32 位和 64 位版本之间切换时项目.不得不尝试几种解决方法,包括条件项目建设等只是为了避免无效格式异常"在不同的机器上.
  2. 如果您管理自己的虚拟机就可以了.但是如果你的项目在受限环境中运行,例如 (Azure(实际上不可能像 azure 提到的那样TuesPenchin 作者) ,Elastic Beanstalk 等)配置该环境仅让 wkhtmltopdf 工作是一场噩梦.
  3. wkhtmltopdf 正在您的服务器中创建文件,因此您必须管理用户权限并授予写入"访问权限wkhtmltopdf 正在运行.
  4. Wkhtmltopdf 作为一个独立的应用程序运行,所以它不是由您的 IIS 应用程序池管理.所以你必须要么主持它作为另一台机器上的服务,否则您将在生产中遇到处理尖峰和内存消耗服务器.
  5. 它使用临时文件来生成 pdf,在情况下像 AWSEC2 磁盘 i/o 真的很慢,这是一个很大的性能问题.
  6. 最讨厌的Unable to load DLL 'wkhtmltox.dll'"错误报告许多用户.
  1. 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.
  2. 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.
  3. wkhtmltopdf is creating files within your server so you have to manage user permissions and grant "write" access to where wkhtmltopdf is running.
  4. 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.
  5. 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.
  6. The most hated "Unable to load DLL 'wkhtmltox.dll'" error reported by many users.

--- 预编辑部分---

对于想要在更简单的应用程序/环境中从 html 生成 pdf 的任何人,我将我的旧帖子作为建议.

TuesPechkin

https://www.nuget.org/packages/TuesPechkin/

或特别适用于 MVC Web 应用程序(但我认为你可以在任何 .net 应用程序中使用它)

or Especially For MVC Web Applications (But I think you may use it in any .net application)

Rotativa

https://www.nuget.org/packages/Rotativa/

他们都利用wkhtmtopdf 用于将 html 转换为 pdf 的二进制文件.它使用 webkit 引擎来呈现页面,因此它还可以解析 css 样式表.

They both utilize the wkhtmtopdf binary for converting html to pdf. Which uses the webkit engine for rendering the pages so it can also parse css style sheets.

它们提供易于使用的与 C# 的无缝集成.

They provide easy to use seamless integration with C#.

Rotativa 还可以从任何 Razor 视图直接生成 PDF.

Rotativa can also generate directly PDFs from any Razor View.

此外,对于现实世界的 Web 应用程序,它们还管理线程安全等...

Additionally for real world web applications they also manage thread safety etc...

这篇关于在 .NET 中将 HTML 转换为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆