如何在ASP.Net MVC中使用PdfSharp显示PDF? [英] How do I display a PDF using PdfSharp in ASP.Net MVC?

查看:325
本文介绍了如何在ASP.Net MVC中使用PdfSharp显示PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在制作一个ASP.Net MVC应用程序,该应用程序必须能够生成PDF并将其显示在屏幕上,或者将其保存在易于用户访问的位置.我们正在使用PdfSharp生成文档.完成后,我们如何让用户保存文档或在阅读器中打开它?我特别困惑,因为PDF是在服务器端生成的,但我们希望它显示在客户端.

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.

以下是MVC控制器,用于创建到目前为止我们编写的报告:

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

运行此命令时,我在documentRenderer.PdfDocument.Save(filename);处得到一个UnauthorizedAccessException,说:Access to the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\report.pdf' is denied.我也不确定执行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.

这是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
    }
}

推荐答案

我不熟悉PDF Sharp,但是对于MVC,大多数情况下是通过内置功能来完成的.您需要获取以字节数组表示的pdf文档.然后,您只需使用MVC的File方法将其返回到浏览器并让其处理下载.他们的班上有什么方法可以做到这一点吗?

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

}

这篇关于如何在ASP.Net MVC中使用PdfSharp显示PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆