搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

详细了解

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.