pdf file displayed as html; add on is installed
Environment: Firefox 57.03, add-on Open in PDF Viewer 0.1.1; path to Reader verified as correct; restarted and cache cleared.
While developing the ability to create a PDF document to an existing application attempts to view a test document fails. For example, if the filename is "summary.pdf" and I ask to save it, the file is saved with the .pdf extension. File appears properly in Reader.
If I ask to view it, the extension .html is added and I see the raw code in the browser.
Chosen solution
I'd just stumbled on the proper use of Response() & headers. My code now reads:
$content = $snappy->getOutputFromHtml($html); $response = new Response($content, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename=' . urlencode($filename) . '.pdf', ]);
return $response;
Your code show `application/json` - did you borrow from a non-pdf response?
How does one get the response to be viewed in the browser? Mine just goes straight to save.
Read this answer in context 👍 0All Replies (7)
What headers is your application sending? For example:
- Content-Disposition: attachment; filename=summary.pdf
- Content-Type: application/pdf
I think both are essential to get the proper result in Firefox. Some other browsers will override text/plain or text/html based on from a file extension or content sniffing, but Firefox generally doesn't do that.
Here's a snippet of the controller function creating the PDF:
header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename=' . urlencode($filename) . '.pdf'); $content = $snappy->getOutputFromHtml($html);
return new Response($content);
This approach is taken directly from here
How do the PHP header() commands work with the new Response()? Or to ask that differently, are you sure they aren't overridden by later headers generated by the Response object?
What headers are received by Firefox? One way to observe that is to open the Browser Console before requesting the download, click the trash can icon to clear existing messages, then request the download. If you do not see URLs, you may need to use the Net button to show requests/responses. Use the triangle at the left of the URL to view the request and response headers.
Assuming you use Symfony, an alternate way to set the headers (inspired by the Symfony documentation) would be along these lines:
$content = $snappy->getOutputFromHtml($html); $response = new Response($content); $response->headers->set('Content-Type', 'application/pdf'); $response->headers->set('Content-Disposition', 'attachment; filename=' . urlencode($filename) . '.pdf'); return $response;
Modified
Chosen Solution
I'd just stumbled on the proper use of Response() & headers. My code now reads:
$content = $snappy->getOutputFromHtml($html); $response = new Response($content, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename=' . urlencode($filename) . '.pdf', ]);
return $response;
Your code show `application/json` - did you borrow from a non-pdf response?
How does one get the response to be viewed in the browser? Mine just goes straight to save.
truckeesolutions said
Your code show `application/json` - did you borrow from a non-pdf response?
Yes, copy/paste error. I'll fix that now.
How does one get the response to be viewed in the browser? Mine just goes straight to save.
I think Firefox should default to the Open/Save/Cancel dialog, but if it doesn't, you can check your PDF setting on the Options page. See: View PDF files using Firefox’s built-in viewer.
Also, you may want to test without the extension in case there is a conflict with the Application preferences on the Options page.
Well, DOH! The View PDF link led me to the setting that PDFs were "always save". It's now "always ask".
Many thanks for your assistance. Reaffirms by preference to work with Firefox.