Generate PDF files from asp.net mvc

If you are using MVC 4, check this out: http://www.nyveldt.com/blog/post/Introducing-RazorPDF


I've had good success creating bar code labels in PDF format using Report.net as well as iTextSharp. For iTextSharp in particular, the API seemed easy enough and it worked well in our ASP.NET MVC application. Not a lot of documentation for iTextSharp so you have to look up the java documentation for iText (which it's a port of).

Bonus: They're both free!


Using Report.NET the code to return their hello world example in an MVC action is:

    public void MyPDFAction()
    {
        Root.Reports.Report report = new Root.Reports.Report(new PdfFormatter());
        FontDef fd = new FontDef(report, "Helvetica");
        FontProp fp = new FontPropMM(fd, 25);
        Page page = new Page(report);
        page.AddCB_MM(80, new RepString(fp, "Hello World!")); 
        RT.ViewPDF(report, "HelloWorld.pdf");
    }

The return type of the action is void as the Report.NET code directly updates the response, which gets returned by default for void actions. This opens up a pdf viewer directly from the browser.

To get the response returned as a page rather than a download replace the RT.ViewPDF line with

RT.ResponsePDF(report, System.Web.HttpContext.Current.Response);

However this method is marked as deprecated in favour of the one that deals with System.Web.UI.Page. Unfortunately I don't know how to deal with a Page object in the context of an MVC app.

I've been unable to get Report.NET to initiate the download of a PDF file.

Download Report.NET here.

Edit I've recently discovered PDFSharp which seems to be more recently maintained than Report.NET. It's also available under the MIT license. You can download from here. There's also an extensive wiki with many examples.

Basic code to return a file as a download in MVC:

    [HttpGet]
    public ActionResult MyPdfAction()
    {
        using (MemoryStream stream = new MemoryStream())
        {
            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
            gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
            document.Save(stream, false);
            return File(stream.ToArray(), "application/pdf", "HelloWorld.pdf");
        }
    }  

Another Edit If you're interesting in producing largely text based documents with tables, it's well worth checking out MigraDoc as it provides a useful abstraction layer on top of the PDFSharp primitives. MigraDoc is included in the PDFSharp download. You'll need to add the PDFSharp, PDFSharp.Charting, MigraDoc.DocumentObjectModel and MigraDoc.Rendering projects to your project to get it all working.

Code to generate a PDF for download here:

    [HttpGet]
    public ActionResult MyPdfAction()
    {
        using (MemoryStream stream = new MemoryStream())
        {
            Document document = CreateDocument();
            document.UseCmykColor = true;
            const bool unicode = false;
            const PdfFontEmbedding embedding = PdfFontEmbedding.Always;
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(unicode, embedding);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();
            pdfRenderer.PdfDocument.Save(stream, false);
            return File(stream.ToArray(), "application/pdf", "HelloWorld.pdf");
        }
    }


    /// <summary>
    /// Creates an absolutely minimalistic document.
    /// </summary>
    static Document CreateDocument()
    {
        Document document = new Document();
        Section section = document.AddSection();
        Paragraph paragraph = section.AddParagraph();
        paragraph.Format.Font.Color = Color.FromCmyk(100, 30, 20, 50);
        paragraph.AddFormattedText("Hello, World!", TextFormat.Bold);
        return document;
    }

Summary Having looked at a number of PDF solutions for C# MVC, I'm using PDFSharp / MigraDoc. I've discounted iTextSharp and projects based on it because of the expensive licensing costs if you're using it commercially (€2500 in my case). Report.NET was developed with ASP.NET classic in mind and hasn't received an update since 2006.