从HTML表单创建PDF导致C#

时间:2022-10-02 21:11:09

I have a project where I need to create an HTML form (no problem) and then create a PDF file from the results using C#.

我有一个项目,我需要创建一个HTML表单(没问题),然后使用C#从结果创建一个PDF文件。

I have done this before in PHP using FPDF but this one needs to be C#. Ideally I want to put the code into a user control and then stick it in an Umbraco website.

我之前在使用FPDF的PHP中做过这个,但这个需要是C#。理想情况下,我想将代码放入用户控件,然后将其粘贴到Umbraco网站中。

Can anyone recommend a good way to do this? PDF doesn't need to be fancy, it'll just display text, we aim to create a generic purchase order based on what the customer wants from the form, which can then be emailed to them to print off on headed paper.

任何人都可以推荐一个很好的方法吗? PDF不需要花哨,它只是显示文本,我们的目标是根据客户想要的形式创建一个通用的采购订单,然后通过电子邮件发送给他们在头纸上打印。

Thanks

3 个解决方案

#1


1  

There are a couple of recent problems with iTextSharp. The most annoying is that in the latest version they've deprecated the HTML parser. So now everything has to work through the XMLWorkerHelper singleton and parses through ParseXHtml. I find this a real pain, since HTML pages which aren't well formed appear fine on browser, parse OK in the old method and now crash out with an exception. So it necessitates an extra step to make sure your HTML is well formed (as XHTML) first. If you are generating your HTML from an ASPX page, then using Server.Execute() to get the stream, then this might be useful to you for iTextSharp:

iTextSharp最近出现了一些问题。最令人讨厌的是,在最新版本中,他们已经弃用了HTML解析器。所以现在一切都必须通过XMLWorkerHelper单例并通过ParseXHtml进行解析。我发现这真的很痛苦,因为格式不正确的HTML页面在浏览器上显示正常,在旧方法中解析好,现在崩溃,异常。因此,需要额外的步骤来确保您的HTML格式正确(如XHTML)。如果要从ASPX页面生成HTML,然后使用Server.Execute()来获取流,那么对于iTextSharp,这可能对您有用:

http://jwcooney.com/2012/12/30/generate-a-pdf-from-an-asp-net-web-page-using-the-itextsharp-xmlworker-namespace/

Be mindful that iTextSharp has a distinct lack of any decent documentation of the modern changes (being mindful that the Java iText documents don't translate perfectly to C#), it makes the learning curve far too long and steep for any practical use in short spaces of time. I've basically given up on that platform, though may just create a baseline system to get something working lean whilst I then learn another framework.

请注意,iTextSharp明显缺乏对现代变更的任何体面文档(注意Java iText文档不能完美地转换为C#),它使得学习曲线太长而且在短空间中的任何实际应用中都很陡峭时间我基本上已经放弃了这个平台,虽然可能只是创建一个基线系统来让一些工作变得精简,然后我学习另一个框架。

As a result, I'm looking at PDFizer and PDFSharp libraries. If I have some success, I'll report back.

因此,我正在研究PDFizer和PDFSharp库。如果我取得了一些成功,我会报告回来。

#2


0  

here is a library for converting HTML to PDF

这是一个用于将HTML转换为PDF的库

http://pdfcrowd.com/web-html-to-pdf-net/

#3


0  

I like the PDFsharp library. Not sure how it would work for your needs, though.

我喜欢PDFsharp库。但不确定它如何适合您的需求。

#1


1  

There are a couple of recent problems with iTextSharp. The most annoying is that in the latest version they've deprecated the HTML parser. So now everything has to work through the XMLWorkerHelper singleton and parses through ParseXHtml. I find this a real pain, since HTML pages which aren't well formed appear fine on browser, parse OK in the old method and now crash out with an exception. So it necessitates an extra step to make sure your HTML is well formed (as XHTML) first. If you are generating your HTML from an ASPX page, then using Server.Execute() to get the stream, then this might be useful to you for iTextSharp:

iTextSharp最近出现了一些问题。最令人讨厌的是,在最新版本中,他们已经弃用了HTML解析器。所以现在一切都必须通过XMLWorkerHelper单例并通过ParseXHtml进行解析。我发现这真的很痛苦,因为格式不正确的HTML页面在浏览器上显示正常,在旧方法中解析好,现在崩溃,异常。因此,需要额外的步骤来确保您的HTML格式正确(如XHTML)。如果要从ASPX页面生成HTML,然后使用Server.Execute()来获取流,那么对于iTextSharp,这可能对您有用:

http://jwcooney.com/2012/12/30/generate-a-pdf-from-an-asp-net-web-page-using-the-itextsharp-xmlworker-namespace/

Be mindful that iTextSharp has a distinct lack of any decent documentation of the modern changes (being mindful that the Java iText documents don't translate perfectly to C#), it makes the learning curve far too long and steep for any practical use in short spaces of time. I've basically given up on that platform, though may just create a baseline system to get something working lean whilst I then learn another framework.

请注意,iTextSharp明显缺乏对现代变更的任何体面文档(注意Java iText文档不能完美地转换为C#),它使得学习曲线太长而且在短空间中的任何实际应用中都很陡峭时间我基本上已经放弃了这个平台,虽然可能只是创建一个基线系统来让一些工作变得精简,然后我学习另一个框架。

As a result, I'm looking at PDFizer and PDFSharp libraries. If I have some success, I'll report back.

因此,我正在研究PDFizer和PDFSharp库。如果我取得了一些成功,我会报告回来。

#2


0  

here is a library for converting HTML to PDF

这是一个用于将HTML转换为PDF的库

http://pdfcrowd.com/web-html-to-pdf-net/

#3


0  

I like the PDFsharp library. Not sure how it would work for your needs, though.

我喜欢PDFsharp库。但不确定它如何适合您的需求。