How do I display a PDF using PdfSharp in ASP.Net MVC?

20,333

Solution 1

I'm not familar with PDF sharp but for MVC is mostly done via built in functionality. You need to get your pdf document represented as an array of bytes. Then you'd simply use MVC's File method to return it to the browser and let it handle the download. Are there any methods on their class to do that?

public class PdfDocumentController : Controller
{
    public ActionResult GenerateReport(ReportPdfInput input)
    {
        //Get document as byte[]
        byte[] documentData;

        return File(documentData, "application/pdf");
    }

}

Solution 2

Using Yarx's suggestion and PDFsharp Team's tutorial, this is the code we ended up with:

Controller:

[HttpGet]
public ActionResult GenerateReport(ReportPdfInput input)
{
    using (MemoryStream stream = new MemoryStream())
    {
        var manager = new ReportPdfManagerFactory().GetReportPdfManager();
        var document = manager.GenerateReport(input);
        document.Save(stream, false);
        return File(stream.ToArray(), "application/pdf");
    }
}

ReportPdfManager:

public PdfDocument GenerateReport(ReportPdfInput input)
{
    var document = CreateDocument(input);
    var renderer = new PdfDocumentRenderer(true,
        PdfSharp.Pdf.PdfFontEmbedding.Always);
    renderer.Document = document;
    renderer.RenderDocument();

    return renderer.PdfDocument;
}

private Document CreateDocument(ReportPdfInput input)
{
    //Creates a Document and puts content into it
}
Share:
20,333
Kevin
Author by

Kevin

I write code, fiction, and music.

Updated on July 10, 2022

Comments

  • Kevin
    Kevin almost 2 years

    We're making an ASP.Net MVC app that needs to be able to generate a PDF and display it to the screen or save it somewhere easy for the user to access. We're using PdfSharp to generate the document. Once it's finished, how do we let the user save the document or open it up in a reader? I'm especially confused because the PDF is generated server-side but we want it to show up client-side.


    Here is the MVC controller to create the report that we have written so far:

    public class ReportController : ApiController
    {
        private static readonly string filename = "report.pdf";
    
        [HttpGet]
        public void GenerateReport()
        {
            ReportPdfInput input = new ReportPdfInput()
            {
                //Empty for now
            };
    
            var manager = new ReportPdfManagerFactory().GetReportPdfManager();
            var documentRenderer = manager.GenerateReport(input);
            documentRenderer.PdfDocument.Save(filename); //Returns a PdfDocumentRenderer
            Process.Start(filename);
        }
    }
    

    When this runs, I get an UnauthorizedAccessException at documentRenderer.PdfDocument.Save(filename); that says, Access to the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\report.pdf' is denied. I'm also not sure what will happen when the line Process.Start(filename); is executed.

    This is the code in manager.GenerateReport(input):

    public class ReportPdfManager : IReportPdfManager
    {
        public PdfDocumentRenderer GenerateReport(ReportPdfInput input)
        {
            var document = CreateDocument(input);
            var renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
            renderer.Document = document;
            renderer.RenderDocument();
    
            return renderer;
        }
    
        private Document CreateDocument(ReportPdfInput input)
        {
            //Put content into the document
        }
    }