為了改善您的使用體驗,本網站正在進行維護,部分功能暫時無法使用。若本站的文件無法解決您的問題,想要向社群發問的話,請到 Twitter 上的 @FirefoxSupport 或 Reddit 上的 /r/firefox 發問,我們的社群成員將很快會回覆您的疑問。

搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

了解更多

pdf file displayed as html; add on is installed

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.