X
Tap here to go to the mobile version of the site.
Top Contributor Feature will be unavailable for a short amount of time today until further notice.

Support Forum

pdf file displayed as html; add on is installed

Posted

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.

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 0

Additional System Details

Installed Plug-ins

  • Shockwave Flash 28.0 r0

Application

  • User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0

More Information

jscher2000
  • Top 10 Contributor
7777 solutions 63332 answers

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.

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.

Question owner

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

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 [https://github.com/KnpLabs/snappy. here]
jscher2000
  • Top 10 Contributor
7777 solutions 63332 answers

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;
  
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 [https://symfony.com/doc/current/introduction/http_fundamentals.html#symfony-response-object the Symfony documentation]) would be along these lines: <pre> $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; &nbsp; </pre>

Modified by jscher2000

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.

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.
jscher2000
  • Top 10 Contributor
7777 solutions 63332 answers

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 in Firefox.

''truckeesolutions [[#answer-1058228|said]]'' <blockquote> Your code show `application/json` - did you borrow from a non-pdf response?</blockquote> Yes, copy/paste error. I'll fix that now. <blockquote>How does one get the response to be viewed in the browser? Mine just goes straight to save. </blockquote> 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 in Firefox]].
jscher2000
  • Top 10 Contributor
7777 solutions 63332 answers

Also, you may want to test without the extension in case there is a conflict with the Application preferences on the Options page.

Also, you may want to test without the extension in case there is a conflict with the Application preferences on the Options page.

Question owner

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.

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.