基于 .NET 服务器的 PDF 生成

发布于 2024-07-11 05:16:34 字数 160 浏览 7 评论 0原文

我想动态生成内容然后渲染为 PDF 文件。 此处理将在远程托管服务器上进行,因此使用虚拟打印机等已不再可行。 有没有人推荐一个可行的 .NET 库(首选 C#)?

我知道我可以生成一堆 PS 代码并自己打包,但在这个阶段我更喜欢一些不那么棘手的东西。

谢谢!

I'd like to dynamically generate content and then render to a PDF file. This processing would take place on a remote hosting server so using virtual printers etc is out. Does any have a recommendation for a .NET library (pref C#) that would work?

I know that I could generate a bunch of PS code and package it myself but I'd prefer something a little less tricksy at this stage.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

悸初 2024-07-18 05:16:34

我使用 SharpPDF 取得了巨大成功。

I have had good success using SharpPDF.

弄潮 2024-07-18 05:16:34

我已成功使用 Siberix

http://www.siberix.com/

公司许可证:350 美元(A单一许可证涵盖无限数量的公司开发人员席位、无限数量的公司 Web 服务器以及无限数量的发行版(作为应用程序的一部分)。)

I have had success using Siberix

http://www.siberix.com/

Corporate License: $350 USD (A single license covers unlimited number of company's developer seats, unlimited number of company's web servers and unlimited number of distributions as a part of your application.)

木格 2024-07-18 05:16:34

免费的PDF Generator .NET(WkHtmlToPdf 包装器)可以通过一行代码从 HTML 模板生成漂亮的 PDF :(

var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);

你只需要一个DLL,没有外部依赖)

Free PDF Generator .NET (WkHtmlToPdf wrapper) can generate pretty PDF from HTML template with one line of code:

var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);

(all you need is one DLL, no external dependencies)

甩你一脸翔 2024-07-18 05:16:34

我们使用 Amyuni PDF Converter 并已成功使用它多年。 我们的使用是通过 COM 接口进行的,但它确实支持 .NET 接口。

We use the Amyuni PDF Converter and have used it successfully for several years. Our usage is via the COM interface, but it does support a .NET interface.

空城之時有危險 2024-07-18 05:16:34

我在 Winnovative 的 HTML 到 PDF 方面获得了很好的体验。

以及开源 HTML Doc 的问题(表单元素 + CSS 的问题)。

I've had good experiences with Winnovative's HTML to PDF.

And bad ones with Open Source HTML Doc (Problems with form elements + CSS).

北渚 2024-07-18 05:16:34

我一直在寻找高性能的 docx 到 pdf 工具一段时间了。 我们的系统具有电子政务方面的特点,并且正在向用户社区生成大量报告。 此时,性能至关重要。

我使用的早期工具没有同时进行转换,而是每个 exe 都需要等待另一个 exe 完成。 我尝试过 Aspose.words,对结果非常满意。

首先,在我们的项目中集成和部署非常容易且无缝。 非常光滑。

其次,由于多个作业并行运行,转换速度更快。

第三,不仅速度快,而且没有格式错误。 考虑到我们提供的是多语言系统,并且一些报告包括英语和阿拉伯语字段(注意从右到左对齐!),这一点非常重要。

最后,文件大小非常小,这一点非常重要,因为我们的系统创建了数以万计的文档。

我们的第一个实现是 Microsoft Office Interop 库。 我们使用以下代码将 docx 转换为 pdf 文档。 该库将 docx 文档完美地转换为 pdf 文件,我们决定将其上传到报告生成服务器。 但过了一会儿,我们注意到转换操作正在等待每个可执行文件。 这会导致同时转换文档出现很大的延迟,这就是为什么我们开始寻找将 docx 文件转换为 pdf 文件的新工具。
查看图片

下面的代码展示了如何使用 Aspose.Words 将 docx 文档转换为 pdf 文件.NET 工具。
参见图 2

I have been looking for a high performing docx to pdf tool for a while now. Our system has an e-government aspect and is generating a very high number of reports to the user community. At this point, performance is paramount.

Earlier tools I have used did not do simultaneous conversion, instead each exe needed to wait for completion of the other. I have tried Aspose.words and I am very happy with the results.

First of all, it was very easy and seamless to integrate and deploy in our project. Very smooth.

Secondly, the speed of conversion is way better due to the fact that multiple jobs run in parallel.

Thirdly, not only fast, but even with no formatting errors. Considering that we are providing a multi-lingual system and some reports include both English and Arabic fields (mind right-to-left alignment!), this was very important.

And finally, the file size was quite small, which again is very important as tens of thousands of documents are created through our system.

Our first implementation was Microsoft Office Interop library. We convert docx to pdf documents by using below code. This library converts the docx documents to pdf files perfectly and we decided to upload this to report generation server. But after a while, we noticed that conversion operations are waiting for each executable. This causes a big delay on converting the documents at the same time and that's why we start to search a new tool for converting docx files to pdf files.
See Image

Below code shows the how to convert docx documents to pdf files by using Aspose.Words for .NET tool.
See Image 2

聽兲甴掵 2024-07-18 05:16:34

RDLC& 报告查看器控件可以根据客户端的判断或服务器命令生成 PDF,然后将其用作 PDF mime 类型。

RDLC & the Report Viewer controls can generate PDF either at the Client's discretion or at server command which can then be served as a PDF mime-type.

一直在等你来 2024-07-18 05:16:34

我使用 O2solutions 的 PDF4NET 取得了很大成功。 它们支持各种场景和 pdf 的数字签名。

I've used PDF4NET from O2solutions with much success. They support all sorts of scenarios and digital signing of the pdf.

绿光 2024-07-18 05:16:34

如果您的数据主要采用 XML 格式,您还可以查看 XSL-FO 解决方案 - 我们正在使用 Alt- Soft 的 Xml2Pdf 取得了巨大成功。 “服务器”版本有点用词不当 - 它实际上只是您需要包含在 Winforms、WPF 或 ASP.NET 应用程序中的单个 DLL - 仅此而已!

工作起来就像一个魅力(如果您熟悉 XSLT 和 XSL-FO,或者愿意学习它)。

马克

If your data is mostly in XML, you could also look at a XSL-FO solution - we're using Alt-Soft's Xml2Pdf with great success. The "server" version is a bit of a misnomer - it's really just a single DLL you need to include in your Winforms, WPF or ASP.NET app - that's all!

Works like a charm (if you're familiar with XSLT and XSL-FO, or willing to learn it).

Marc

独自←快乐 2024-07-18 05:16:34

我们使用了来自 的一组第三方 DLL PDFSharp 又使用 MigraDoc 中的 DLL。 我不知道我们朝这个方向发展的所有原因(该决定是由高级开发人员做出的),但我可以告诉你:

  • 它似乎处于活跃状态
    发展。
  • 它拥有大部分
    我们需要的功能。
  • 源代码
    可用。 虽然用了一些
    我的模式和惯例
    以前没见过,一旦我接触到
    他们,很容易制作
    变化。 我添加了对使用的支持
    直接System.Drawing.Image
    而不是作为保存文件。
  • 这是
    也没有很好的记录
    内部或外部。

We used a set of third party DLLs from PDFSharp who in turn use DLLs from MigraDoc. I'm not privy to all the reasons that we went that direction (the decision was made by a senior developer), but I can tell you that:

  • It seems to be in active
    development.
  • It had most of the
    features we needed.
  • The source code
    is available. Although it used some
    patterns and conventions that I
    hadn't seen before, once I got on to
    them, it was fairly easy to make the
    changes. I added support for using
    the System.Drawing.Image directly
    rather than as saving files.
  • It is
    not documented well either
    internally or externally.
妄司 2024-07-18 05:16:34

我在 .NET 6 中使用了 iTextSharp ,如下所示,但它存在加载脚本的问题和用于加载样式表的 cdn,它仅适用于内联样式。 可以使用 File.WriteAllBytes() 保存这些字节

using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Net.Http;
using PageSize = iTextSharp.text.PageSize;

    public static byte[] GenratePdfBytes(string htmlContent)
    {
        byte[] pdfBytes;
        var pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        var html = new StringReader(htmlContent);
        var htmlparser = new HTMLWorker(pdfDoc);
        using (var memoryStream = new MemoryStream())
        {
            var writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();
            htmlparser.Parse(html);
            pdfDoc.Close();
            pdfBytes = memoryStream.ToArray();
        }
        return pdfBytes;
    }

I used iTextSharp in .NET 6 as shown below, but it had an issue of loading scripts and cdn(s) for loading stylesheet, it only works with inline styles. these bytes can be saved using File.WriteAllBytes()

using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Net.Http;
using PageSize = iTextSharp.text.PageSize;

    public static byte[] GenratePdfBytes(string htmlContent)
    {
        byte[] pdfBytes;
        var pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        var html = new StringReader(htmlContent);
        var htmlparser = new HTMLWorker(pdfDoc);
        using (var memoryStream = new MemoryStream())
        {
            var writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
            pdfDoc.Open();
            htmlparser.Parse(html);
            pdfDoc.Close();
            pdfBytes = memoryStream.ToArray();
        }
        return pdfBytes;
    }
左耳近心 2024-07-18 05:16:34

根据我的经验,有几种方法可以做到这一点,这取决于您尝试生成的应用程序和复杂性,以及生成的 PDF 是否需要是商业打印就绪文件或只是用于共享/的 PDF 报告归档等,以及根据预算对卷输出进行排序。 大多数高端 PDF 库的价格都很高。

我使用了各种基于不复杂性的技术,有一些库可以生成 PDF(从头开始构建 PDF 元素),在这种情况下,您可以使用 iText 或其他可以在 PDF 顶部添加内容的东西。

如果您需要进行细微调整,即使用现有的 PDF 作为模板并添加一些内容(文本/图像),则有一些库可以仅在顶部标记文本和图像。 (例如:http://www.pdfsharp.net/

如果您生成发票或报告,您可以使用 HTML 模板,合并数据(替换 {tokens} 等),然后使用不同类型的机制将 html 转换为 pdf(例如:https://www.nrecosite.com/pdf_generator_net.aspx

如果您需要完全控制样式、客户端生成的模板(idml)等,可以使用 API,您可以与 InDesign Server 集成,使用它来生成可打印的 PDF 文件。 我已经构建了一个像这样的 API,但这是 PDF 生成的另一个级别。

There are a few ways to do this, in my experience, and it depends on the application and complexity of what you are trying to generate and whether the resulting PDF needs to be a commercial print-ready file or just a PDF report for sharing/archiving etc, and what sort out volume output, based on budget. Most higher end PDF libraries come with a large price tag.

I have used various techniques based no the complexity, there are libraries to generate PDF (build PDF elements from the ground up) in this case you could use something like iText or others that can add content on top of a PDF.

If you need to do minor adjustments i.e. use an existing PDF as a template and add some content (text/images) there are libraries that can just stamp text and images on top. (eg: http://www.pdfsharp.net/)

If you generating invoices or reports, you could use an HTML template, merge data (replacing {tokens} etc) and then convert the html to pdf using a different type of mechanism (eg: https://www.nrecosite.com/pdf_generator_net.aspx)

There are API's if you need full control over styling, client generated templates (idml) etc, you can integrate with InDesign Server and use that to generate print ready PDF files. I have build an API like this but this is another level of PDF generation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文