Hi,
I’m using Slim 4.
I want to show a PDF file in the browser instead of downloading it. If I use a JPG file, the image is shown in the browser. But if I use a PDF file, the browser automatically downloads the file. Downloaded files can be open perfectly on any computer.
Both test files are stored in the server, not generated by a library.
This is the code I’m using:
$attachmentFilename = "test.pdf"; //"test.jpg"
$pathFilename = $directory . DIRECTORY_SEPARATOR . $attachmentFilename;
$attachmentFile = file_get_contents($pathFilename);
$response->getBody()->write($attachmentFile);
$response = $response->withHeader('Content-Type', mime_content_type($pathFilename));
$response = $response->withHeader('Content-Disposition', 'inline; filename="'.$attachmentFilename.'"');
$response = $response->withHeader('Content-Transfer-Encoding', 'binary');
$response = $response->withHeader('Accept-Ranges', 'bytes');
$response = $response->withHeader('Content-Length', filesize($pathFilename));
return $response;
I´ve also tried the following code with the same result:
$attachmentFilename = "test.pdf"; //"test.jpg"
$pathFilename = $directory . DIRECTORY_SEPARATOR . $attachmentFilename;
$fh = fopen($pathFilename, 'r');
$file_stream = new Stream($fh);
$response->getBody()->write($attachmentFile);
return $response->withHeader('Content-Type', mime_content_type($pathFilename))
->withHeader('Content-Length', filesize($pathFilename))
->withHeader('Content-Disposition', 'inline; filename="'.$attachmentFilename.'"')
->withHeader('Accept-Ranges', 'bytes')
->withHeader('Expires', '0');
What am I doing wrong?
Thanks in advance.