Chrome PDF Viewer embedded download button not working

Clicking the Chrome PDF Viewer download button directly downloading the server-side file.

I'm using ColdFusion server to generate the pdf file and view it in a separate tab with inline. PDF was correctly viewing in a tab but when clicking the download icon I can able to download that PDF file, it was downloading my .cfm file.

screenshot

Upvotes: 2

Views: 1985

Answers (2)

badams
badams

Reputation: 108

To support downloading, the Chrome PDF viewer needs a reference to the PDF content in memory. To provide this reference:

  1. Download the PDF content into a JavaScript blob.
  2. Call createObjectURL() to create an object URL from the blob.
  3. Use the object URL as the data for an object tag.

Here is an example:

<object id="content" type="application/pdf"></object>

<script>            
function displayPdf() {
  // Download the PDF Content.
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
      var url = window.URL || window.webkitURL;

      // Create an object URL from the blob.
      var objectUrl = url.createObjectURL(this.response);

      // Use the object URL as the data for the object tag.
      $("#content").attr('data', objectUrl);
    }
  };

  xhr.open('GET', url_for_pdf_content);
  xhr.responseType = 'blob';  // The response will be a blob.
  xhr.send();
}
</script>

Upvotes: 0

Tom Stroinski
Tom Stroinski

Reputation: 66

Try swapping 'inline' with 'attachment', in your Content-Disposition header.

Basically, inline will try, always varying by borwser, to open the file inside the browser. In any case what you're trying to do you should definitely use attachment. This will directly go to the user and force a download.

Upvotes: 1

Related Questions