Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display PDF(stored as a BLOB) as an image in MVC

I have to display a PDF(stored as BLOB in SQL Server table) in a partial view. I am doing the same for a ".jpeg" image stored as BLOB in the table.The image is getting displayed,however,the PDF isnt.I have the following code in my controller:

[Authorize]
public ActionResult Show3LeadImage(int id)
{
    Byte[] imageData = null;
    using (var scope = new PatientPersistenceScope())
    {
         searchRequest leadRequest = new searchRequest();
         leadRequest.CaseNumber = id;
         var mrx12LeadBitmap = _mrxSearchService.Get12Lead(leadRequest);
         imageData = mrx12LeadBitmap.Data12Leads[0].mrx3Lead;
         scope.Done();
    }
   return File(imageData,"image/png");//I have tried "FileContentResult,but it didnt work
 }

The code in the partial view(.ascx) looks like this:

<object data='<%= Url.Action("Show3LeadImage", "CareRecord", new { id = Model.CareRecord.CaseNumber}) %>' type="application/pdf" style="width: 721px; height: 800px;" ></object>

I do not have access to the physical file....the BLOB is all I have.

Is it even possible to display a PDF as image in partial view? I came across lots of articles that explained how to make a PDF available for download in the partial view.But I do not want my file to be downloadable. Is this functionality achievable or am I trying to do something impossible?

like image 285
user1550951 Avatar asked Jan 20 '26 21:01

user1550951


1 Answers

Converting a PDF to image is not as simple as serving it as a PNG. You will need to convert it using a library, like ImageMagick or ABCPdf (sample: Convert PDF to image with high resolution)

If you want to embed the PDF inside the HTML the are several ways to do it, i.e. Recommended way to embed PDF in HTML?

like image 67
Eduardo Molteni Avatar answered Jan 23 '26 19:01

Eduardo Molteni