1

Expanding on this question, I'm struggling to work out how I can specify which PDF I'm reading.

I'm creating my PDF with WkHtmlToPdf which generates the PDF file. I'm then saving the PDF bytes to my database and I want to read it out again to display it on the View:

public void GetPDF(int id)
{
    // get the byte array for the PDF out of the database
    var Pdf = db.Invoices.FirstOrDefault(x => x.Id == id).Document;

    //FileStream Stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    //return File(Stream, "application/pdf");

    // this code reads from a file but I need to read the byte array
    // back out so that it displays as a PDF
}

On my View, I'm doing this as per the answer in the linked question:

<object data='@Url.Action("GetPDF")'></object>

How do I pass in the Invoice ID parameter into the GetPDF method?

Is there a better way to do this?

Community
  • 1
  • 1
Ortund
  • 8,095
  • 18
  • 71
  • 139

2 Answers2

2
@Url.Action("GetPDF", new { id = 1 })
Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
2

Here is how to read the byte array back out so that it displays as a PDF

public ActionResult GetPDF(int id) {
    // get the byte array for the PDF out of the database
    var Pdf = db.Invoices.FirstOrDefault(x => x.Id == id).Document;
    // return Pdf content
    return File(Pdf, "application/pdf");    
}

Controller.File has an overloaded method that takes a byte[] of the file content to be returned. So return the result of passing the byte array to that method.

And @RosdiKasim already showed you how to call the action with the id from the view

@Url.Action("GetPDF", new { id = 1 })
Nkosi
  • 235,767
  • 35
  • 427
  • 472