1

I would like to create a PDF file in a controller (based on a Crystal Report), serve it back to the user in an HTML object. My current iteration of the following controller simply returns a File Stream, which just loads the PDF document. In order to display the PDF to the user in an iframe or HTML object, is it required to first save the PDF to the server and then return the path to my view? What is the best way to accomplish this task? Thanks!

[HttpPost]
public ActionResult Index(string test)
{
    ReportClass rptH = new ReportClass();

    rptH.FileName = Server.MapPath(@"~/Reports/Report.rpt");
    rptH.SetParameterValue("@ID", 33);
    rptH.Load();
    Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

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

1 Answer 1

1

Did you try using FileStreamResult:

Controller

    [HttpGet]
    public ActionResult DisplayPdfInIframe(string test)
    {
        ReportClass rptH = new ReportClass();

        rptH.FileName = Server.MapPath(@"~/Reports/Report.rpt");
        rptH.SetParameterValue("@ID", 33);
        rptH.Load();
        Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

        return new FileStreamResult(stream, "application/pdf");
    } 

View

 <iframe src="@Url.Action("DisplayPdfInIframe", "YourController")"></iframe>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your input. The issue is that after I receive the FIleStreamResult on the view side, I don't think that I will have a path to provide to the HTML object data (since its in memory?). My current solution involves saving the PDF locally and passing a model containing the PDF file path to the view. However, I'm quite the novice and would appreciate any further feedback. Thanks!
See my edit. In your view you want to call the Helper Url.Action to load up the method in your controller that will serve back the response stream which gets rendered in your iframe.
Yes, that will definitely work. Thanks for helping me with this sorcery.
Also, you're going to want to decorate it with [HttpGet] I think as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.