Best C# API to Create PDF

Best C# API to create PDF

Update:

I'm not sure when or if the license changed for the iText# library, but it is licensed under AGPL which means it must be licensed if included with a closed-source product. The question does not (currently) require free or open-source libraries. One should always investigate the license type of any library used in a project.


I have used iText# with success in .NET C# 3.5; it is a port of the open source Java library for PDF generation and it's free.

There is a NuGet package available for iTextSharp version 5 and the official developer documentation, as well as C# examples, can be found at itextpdf.com

Creating pdf files at runtime in c#

iTextSharp
http://itextsharp.sourceforge.net/

Complex but comprehensive.

itext7 former iTextSharp

Open source API to Create and add dynamic data to PDF

Would it be possible to use Ghostscript? Just output the data as postscript and let the program output the pdf...

I found also this: Creating pdf files at runtime in c#. Maybe it helps.

What options do we have to generate a PDF from an ASP.NET Core Web Api

using IronPdf;
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Render an HTML document or snippet as a string
Renderer.RenderHtmlAsPdf("<h1>Hello World</h1>").SaveAs("html-string.pdf");
// Advanced:
// Set a "base url" or file path so that images, javascript and CSS can be loaded
var PDF = Renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>",@"C:\site\assets\");
PDF.SaveAs("html-with-assets.pdf");

IronPDF for .NET Core

Best .Net pdf library to generate data tables

ItextSharp 4 released under the GNU Lesser General Public License (LGPL)
by using version 4 or earlier you can use without having to buy a license.

For version 4 heres the link, download the code and in bin folder you will get ITextsharp old version dll
itextsharp 4

Here the updated Itextsharp dll

Or you can also use client side PDF generation using Jspdf

Best way to create PDF from XML XSLT in C#

In the past I've used a commercial library called Ibex PDF Creator to generate PDF documents from XML data using the XSL-FO standard that has worked really well.

Here's an example of how I would use it:

XML data:

<DocumentRoot>
<!-- Some content -->
</DocumentRoot>

XSL-FO layout:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/DocumentRoot">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format">
<ibex:properties
title="Some document"
subject=""
author=""
keywords=""
creator="" />
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm">
<fo:region-body margin-bottom="1cm" margin-top="3cm"/>
<fo:region-before extent="20mm"/>
<fo:region-after extent="8mm"/>
<fo:region-start extent="1mm"/>
<fo:region-end extent="1mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
</<fo:root>
</xsl:template>
</xsl:stylesheet>

Generating the PDF document in .NET:

var data = new MemoryStream(dataBytes);
var layout = new MemoryStream(layoutBytes);
var pdf = new MemoryStream();

// Using the Ibex PDF Creator .NET API
var doc = new FODocument();
doc.generate(data, layout, pdf);

I hope this helps.

How to return a PDF from a Web API application

Some Server side code to return PDF (Web Api).

[HttpGet]
[Route("documents/{docid}")]
public HttpResponseMessage Display(string docid) {
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest);
var documents = reader.GetDocument(docid);
if (documents != null && documents.Length == 1) {
var document = documents[0];
docid = document.docid;
byte[] buffer = new byte[0];
//generate pdf document
MemoryStream memoryStream = new MemoryStream();
MyPDFGenerator.New().PrintToStream(document, memoryStream);
//get buffer
buffer = memoryStream.ToArray();
//content length for use in header
var contentLength = buffer.Length;
//200
//successful
var statuscode = HttpStatusCode.OK;
response = Request.CreateResponse(statuscode);
response.Content = new StreamContent(new MemoryStream(buffer));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentLength = contentLength;
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=" + document.Name + ".pdf", out contentDisposition)) {
response.Content.Headers.ContentDisposition = contentDisposition;
}
} else {
var statuscode = HttpStatusCode.NotFound;
var message = String.Format("Unable to find resource. Resource \"{0}\" may not exist.", docid);
var responseData = responseDataFactory.CreateWithOnlyMetadata(statuscode, message);
response = Request.CreateResponse((HttpStatusCode)responseData.meta.code, responseData);
}
return response;
}

On my a View you could do something like this

<a href="api/documents/1234" target = "_blank" class = "btn btn-success" >View document</a>

which will call the web api and open the PDF document in a new tab in the browser.

Here is how i basically do the same thing but from a MVC controller

// NOTE: Original return type: FileContentResult, Changed to ActionResult to allow for error results
[Route("{docid}/Label")]
public ActionResult Label(Guid docid) {
var timestamp = DateTime.Now;
var shipment = objectFactory.Create<Document>();
if (docid!= Guid.Empty) {
var documents = reader.GetDocuments(docid);
if (documents.Length > 0)
document = documents[0];

MemoryStream memoryStream = new MemoryStream();
var printer = MyPDFGenerator.New();
printer.PrintToStream(document, memoryStream);

Response.AppendHeader("Content-Disposition", "inline; filename=" + timestamp.ToString("yyyyMMddHHmmss") + ".pdf");
return File(memoryStream.ToArray(), "application/pdf");
} else {
return this.RedirectToAction(c => c.Details(id));
}
}
return this.RedirectToAction(c => c.Index(null, null));
}

Hope this helps

Can PDFSharp create Pdf file from a Html string in Net Core?

I recently ran into the exact same issue myself. There is currently extremely limited ways in dealing with PDFs in general in .NET Core. To go through them...

  • PDF Sharp - Does not support .NET Core/Standard.
  • SelectPDF - Does have a "free" community version hidden in their website footer. Should be useable in most cases. https://selectpdf.com/community-edition/
  • IronPDF - "Enterprise" pricing. Starts at $1.5k
  • WKHTMLTOPDF - This is actually just an executable that someone has written a C# wrapper over the top to run the exe. Not a great solution.
  • iTextSharp - Has "hidden" pricing but apparently this is the only one that specifically will run on Linux under .NET Core (If that's important to you).

IMO the only free one that will do what you need is SelectPDF. And that's saying something because I don't rate the library or the API. But it's free and it works.

More info : https://dotnetcoretutorials.com/2019/07/02/creating-a-pdf-in-net-core/



Related Topics



Leave a reply



Submit