Този сайт ще има ограничена функционалност, докато се извършва тече неговата поддръжка. Ако дадена статия не може реши проблема ви и искате да зададете въпрос, нашата общност е готова да ви помогне на @firefox в Twitter и /r/firefox в Reddit.

Търсене в помощните статии

Избягвайте измамите при поддръжката. Никога няма да ви помолим да се обадите или изпратите SMS на телефонен номер или да споделите лична информация. Моля, докладвайте подозрителна активност на "Докладване за злоупотреба".

Научете повече

pdf file displayed as html; add on is installed

  • 7 отговора
  • 2 имат този проблем
  • 1 изглед
  • Последен отговор от truckeesolutions

more options

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.

Избрано решение

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.

Прочетете този отговор в контекста 👍 0

Всички отговори (7)

more options

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.

more options

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

more options

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;
  

Променено на от jscher2000 - Support Volunteer

more options

Избрано решение

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.

more options

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.

more options

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

more options

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.