I'm using Azure blob storage to house images and DOMPdf to create PDF files. The issue I'm facing is that DOMPdf will not "Download files" to display them. My current controller, contacts Azure blob storage, grabs the file and spits it out as a download.
Instead, I have tried to use the response()->file()
method in Laravel to display the image instead:
// Assume $file holds the file content from Azure Blob Storage
\Illuminate\Support\Facades\File::append(Storage::disk('local')->path("chunks/{$fileToken}"), $file);
$mimeType = mime_content_type(Storage::disk('local')->path("chunks/{$fileToken}"));
# Check if we can render this as an image
if (in_array($mimeType, ['image/jpeg','image/gif','image/png','image/bmp','image/svg+xml'])) {
$headers = [
'Content-Type' => $mimeType,
'Content-Disposition' => sprintf('inline; filename="%s"', $realPath->filename)
];
$response = response()->file(Storage::disk('local')->path("chunks/{$fileToken}"), $headers);
# Delete from local disk
\Illuminate\Support\Facades\File::delete(Storage::disk('local')->path("chunks/{$fileToken}"));
return $response;
}
# Delete from local disk
\Illuminate\Support\Facades\File::delete(Storage::disk('local')->path("chunks/{$fileToken}"));
// Do normal download functionality
Now, the mime_content_type
successfully identifies the PNG I am trying, it creates the file in the chunks
directory and if I remove the delete, I can open it as the PNG perfectly fine. The issue I'm getting is the browser is not rendering the image. Infact, it is rendering the headers?
How can I render this image out to the browser so I can then access the image in DOMPdf?