基于 HTML 代码生成 PDF(iTextSharp、PDFSharp?)
PDFSharp 库是否可以像 iTextSharp 一样生成 PDF 文件*考虑 HTML 格式*? (粗体(strong)、间距(br)等)
之前我用过iTextSharp,大致是这样处理的(代码如下):
string encodingMetaTag = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
string htmlCode = "text <div> <b> bold </ b> or <u> underlined </ u> <div/>";
var sr = new StringReader (encodingMetaTag + htmlCode);
var pdfDoc = new Document (PageSize.A4, 10f, 10f, 10f, 0f);
var = new HTMLWorker htmlparser (pdfDoc);
PdfWriter.GetInstance (pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open ();
htmlparser.Parse (sr);
pdfDoc.Close ();
合并到合适的HTML表单到PDF文档中处理类对象 HTMLWorker.. 那么 PDFSharp 又如何呢? PDFSharp有类似的解决方案吗?
Does the library PDFSharp can - like iTextSharp - generate PDF files *take into account HTML formatting *? (bold (strong), spacing (br), etc.)
Previously I used iTextSharp and roughly handled in such a way (code below):
string encodingMetaTag = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
string htmlCode = "text <div> <b> bold </ b> or <u> underlined </ u> <div/>";
var sr = new StringReader (encodingMetaTag + htmlCode);
var pdfDoc = new Document (PageSize.A4, 10f, 10f, 10f, 0f);
var = new HTMLWorker htmlparser (pdfDoc);
PdfWriter.GetInstance (pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open ();
htmlparser.Parse (sr);
pdfDoc.Close ();
incorporated into the appropriate HTML form to a PDF document dealt with the class object HTMLWorker.. so what with PDFSharp? Has PDFSharp similar solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用 PdfSharp 的 PDF HTML 渲染器 可以从 HTML 生成 PDF
在插入 PDF 之前
。要渲染为图像,请参考迭戈答案中的代码。
要呈现为文本,请参考以下代码:
HTML 代码
HTML Renderer for PDF using PdfSharp can generate a PDF from an HTML
before inserting to the PDF.
To render as an image, please refer to the code from Diego answer.
To render as text, please refer code below:
HTML code
不幸的是,HtmlRenderer 不是一个适合在基于 .NET 5.0 的项目中使用的库:
另外,我发现依赖包 HtmlRender.PdfSharp 有以下警告消息:
顺便说一句,我设法使用另一个库将 HTML 渲染为 PDF :
库 IronPDF
License.LicenseKey
不是必需的,您可以将其删除,但您的 pdf 将在每页末尾生成带有 IronPDF 水印的 PDF。但 IronPDF 提供获取试用许可证密钥。Unfortunately, HtmlRenderer is not an appropriate library to be used in a project based on .NET 5.0:
Also, I found that the dependency package HtmlRender.PdfSharp has the following warning message:
By the way, I managed to render HTML as PDF using another library IronPDF:
The line with
License.LicenseKey
is not necessary and you can remove it, but your pdf will be generated with the IronPDF watermark in the end of each page. But IronPDF provides getting trial license key.经过长时间的斗争,我成功使用 Polybioz.HtmlRenderer.PdfSharp.Core
=> HtmlRenderer.PdfSharp.Core 是 HtmlRenderer.PdfSharp for .NET Core 的部分移植,
它在 Net5.0 中工作:)
我的解决方案直接受到 VDWWD 和其他人的启发,
唯一剩下的想法是拉丁语中未呈现粗体(“facilluptat”应加粗),请参阅我的 打印页面屏幕
after a long struggle, i succeed using Polybioz.HtmlRenderer.PdfSharp.Core
=> HtmlRenderer.PdfSharp.Core is a partial port of HtmlRenderer.PdfSharp for .NET Core
it work in Net5.0 :)
my solution, directly inspired from VDWWD and others
the only think which is remain is that the bold is not rendered (the "facilluptat" should be bolded) in the latinstuff, see my print screen of the page
我会推荐您NReco.PdfGenerator,因为它有免费和付费许可证,并且很容易从 nuget 安装。
主页:https://www.nrecosite.com/pdf_generator_net.aspx
文档:https://www.nrecosite.com/doc/NReco.PdfGenerator/
如果您想从 html 文件创建 PDF,请尝试:
I'll recommend you NReco.PdfGenerator because have free and paid license and its easy to install from nuget.
Main page: https://www.nrecosite.com/pdf_generator_net.aspx
Documentation: https://www.nrecosite.com/doc/NReco.PdfGenerator/
If you want create PDF from html file try:
我知道这个问题已经很老了,但这里有一个干净的方法...
您可以使用 HtmlRenderer 与 PDFSharp 结合来完成此操作:
有些人报告最终图像看起来有点模糊,显然到期的自动抗锯齿。以下是有关如何解决此问题的帖子消息:http: //forum.pdfsharp.com/viewtopic.php?f=2&t=1811&start=0
I know this question is old, but here's a clean way to do it...
You can use HtmlRenderer combined with PDFSharp to accomplish this:
Some people report that the final image looks a bit blurry, apparently due to automatic anti-aliasing. Here's a post message on how to fix that: http://forum.pdfsharp.com/viewtopic.php?f=2&t=1811&start=0
不,PDFsharp 当前不包含解析 HTML 文件的代码。
No, PDFsharp does not currently include code to parse HTML files.
老问题,但以上都不适合我。然后我尝试了 HtmlRenderer 的
generatepdf
方法与 pdfsharp。希望有帮助:您必须安装名为
HtmlRenderer.pdfsharp
的 nuget。Old question but none of above worked for me. Then i tried
generatepdf
method of HtmlRenderer in combination of pdfsharp. Hope it helps:You must install a nuget named
HtmlRenderer.pdfsharp
.如果您只想将某个 HTML 字符串写入 PDF,而不希望将其余字符串写入 PDF,则可以使用 TheArtOfDev HtmlRenderer。此代码片段使用 V 1.5.1
If you only want a certain HTML string written to the PDF but not the rest, you can use the
HtmlContainer
from TheArtOfDev HtmlRenderer. This snippet uses V 1.5.1在我去年开发的一个项目中,我使用 wkhtmltopdf (http://wkhtmltopdf.org/) 从 html 生成 pdf然后我读取该文件并将其返回给用户。
它对我来说效果很好,对你来说可能是一个主意......
In a project that I developed last year I used wkhtmltopdf (http://wkhtmltopdf.org/) to generate pdf from html then I read the file and get back it to the user.
It works fine for me and it could be an idea for you...
你们听说过这个吗?我可能回复得很晚,但我认为这有帮助。它非常简单并且效果很好。
编辑:我带着使用“PDFSharp”将 HTML 代码转换为 PDF 的问题来到这里,发现“PDFSharp”无法做到这一点,然后我发现了 NReco,它对我有用,所以我觉得它可能会帮助像我这样的人。
Have you guys heard of this. I might be answering very late but I thought it helps. It is very simple and works well.
Edit: I came here with the question of converting HTML code to PDF using 'PDFSharp' and found out that 'PDFSharp' cannot do it then I found out about NReco and it worked for me so I felt it might help someone just like me.
我知道这是一个非常老的问题,但我意识到没有人说实际上有一种准确的方法可以将 HTML 渲染为 PDF。根据我的测试,我发现您需要以下代码才能成功完成此操作。
还有另一种方法可以做到,但您可能会遇到尺寸问题。
I know there is a really old question but I realize that there is no one saying actually an accurate method to render an HTML into a PDF. Based on my test I found out that you need the following code to successfully do it.
There is another way to do but you might have problems with the size.