J Evans
J Evans

Reputation: 1122

.NET server based PDF generation

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!

Upvotes: 12

Views: 18445

Answers (13)

Mark Redman
Mark Redman

Reputation: 24515

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.

Upvotes: 1

BNG016
BNG016

Reputation: 310

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;
    }

Upvotes: 1

Emre Yilmaz
Emre Yilmaz

Reputation: 11

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

Upvotes: 1

rlb.usa
rlb.usa

Reputation: 15043

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

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

Upvotes: 2

Vitaliy Fedorchenko
Vitaliy Fedorchenko

Reputation: 9235

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)

Upvotes: 4

quimbo
quimbo

Reputation: 503

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.)

Upvotes: 4

wageoghe
wageoghe

Reputation: 27608

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.

Upvotes: 2

Robert Gowland
Robert Gowland

Reputation: 7947

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.

Upvotes: 0

marc_s
marc_s

Reputation: 754240

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

Upvotes: 0

Fran Knebels
Fran Knebels

Reputation:

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

Upvotes: 0

Akshay
Akshay

Reputation: 11923

Have a look at http://itextsharp.sourceforge.net/. Its open source.
Tutorial: http://itextdocs.lowagie.com/tutorial/

Upvotes: 8

D'Arcy Rittich
D'Arcy Rittich

Reputation: 171351

I have had good success using SharpPDF.

Upvotes: 6

Stephen Wrighton
Stephen Wrighton

Reputation: 37819

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.

Upvotes: 0

Related Questions