25

I'm using PDF.Js to embed PDF file for preview, and I removed the script of download and open files from the viewer.js , but when I test the page and PDF file try to show, the Internet Download Manager download it and abort the preview .. after search I found that using object instead of iframe may solve the problem, but it didn't work the pdf viewer appeared white, what can I do to prevent auto download ? or using another way (Plugin) to show PDF file content.

<iframe 
  class="pdf" 
  webkitallowfullscreen="" 
  mozallowfullscreen="" 
  allowfullscreen="" 
  frameborder="no" 
  width="'.$width.'" 
  height="'.$height.'" 
  src="'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'" 
  data-src="'.$pdf_url.'">
  '.$pdf_url.'
</iframe>

enter image description here

user3003810
  • 941
  • 3
  • 18
  • 45
  • (1) - Click on "more information" and tell us what it is shown in "Unexpected server response" section (2) - have you tried with other browsers ? it is the same issue ? – Halayem Anis May 06 '15 at 18:55
  • (1) PDF.js v1.0.1040 (build: 997096f) Message: Unexpected server response (204) while retrieving PDF. (2) Yes I tried on Firefox and the same issue there (the Internet download manager has an option (auto download (some extensions files) and PDF included) but I want to prevent it. – user3003810 May 06 '15 at 19:48

8 Answers8

26

This is not something related to developing issue , this is something related to user specific environement.

The Issue :

Using IDM ,any URL that ends with a media extension (e.g *.JPG , *.PNG , *.MP4 , *.WMV , *.PDF ..etc ) will be downloaded automatically , However on the other hand if the user doesnt have IDM installed , the file will be viewed immediately in browser window.

Possible Solutions :

  1. Remove PDF extension Handler from IDM , to prevent auto download, and i think the image explains it very well.

IDM prevent file types from being handled

  1. Modify response header for your PDF link to force your browser to view pdf inside its view , please consider that each browser may handle the response differently , more details about this method can be found here.

Final Note:

As a developer you shouldnt handle each user specific environment , we suppose that when user installs a specific app to handle generic files , then it is his/her role to handle that application , and not the developer role , cause if you follow this algorithm you jump inside infinite loop handling different users specific setup.

Community
  • 1
  • 1
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72
7

Just remove .pdf Extension from the main file. IDM cannot detect what kind of file it is. but the browser will handle its native way.

Arif
  • 927
  • 9
  • 9
1

try with this

<embed src="'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'" type="text/html" >
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
1

It is possible to prevent download automatically from idm (PdfJs). IDM find extention pdf file so we need to convert pdf file to base-64 and then read base64 of pdf into node DOM src:

1. Convert your file pdf to base64

document.getElementById('myfiles').addEventListener('change', function(event){

  var input = document.getElementById("myfiles"); 

  var fReader = new FileReader();
  fReader.readAsDataURL(input.files[0]);
  fReader.onloadend = function(event){ 
    document.getElementById("base64").innerHTML = event.target.result;
   console.log(event.target.result); 
     
  }
 });
<input type="file" name="files" id="myfiles" value=""><br>
<textarea id="base64" cols="50"></textarea>

2. get all string base64 put to txt file

filename.txt

3. read base64 from file txt and past into iframe or with viewer.js (PDF JS)

-iframe should

<iframe scr="data:application/pdf,base64.....">prevent download from idm</iframe>

-with PDF viewer

 var xhr = new XMLHttpRequest();
     console.log(file);
     xhr.open('GET', folderFiles+filename+".txt", true);
     xhr.responseType = 'text';
     xhr.onload = function(e) {
     
        if (this.status == 200) {
     
           PDFViewerApplication.open(this.response);     
        
        }
      };

So, IDM won't force download pdf file automatically because it cannot find specific file. link : https://github.com/sokhasen/ViewerPDF.git

Sen Sokha
  • 1,784
  • 16
  • 16
1

I suggest you use php to remove the file extension then use viewer.html?file=file_path&name to view the pdf without interception from idm:

$fileinfo = pathinfo($book['file']);
$path_pdf=$fileInfo['dirname'];
$name_pdf=$fileInfo['filename'];
header('location: viewer.html?file='.$path_pdf.'/'.$name_pdf.'');
Seth B
  • 1,014
  • 7
  • 21
0

Judging by your comments, I'd say your uri may be incorrect.
You can try replacing the uri with any pdf file online to see if the rest of the code is okay.

If you just want to embed a PDF object, you could try using PDFobject.js.
https://github.com/pipwerks/PDFObject
http://pdfobject.com/instructions.php

HTML

<div id="my_pdf_object"
  class="pdf" 
  webkitallowfullscreen="" 
  mozallowfullscreen="" 
  allowfullscreen="" 
  frameborder="no" 
  width="'.$width.'" 
  height="'.$height.'" 
>
  It appears you don't have Adobe Reader or PDF support in this web browser. 
  <a href="'.$baseurl.'/assets/pdf/web/viewer.html file='.urlencode($pdf_url).'">
  Click here to download the PDF</a>
</div>

JavaScript

<script type="text/javascript">
  //loads pdf files for resume
  window.onload = function (){
    var success = new PDFObject({ 
      url: "'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'" 
    }).embed("my_pdf_object");
  };
</script>
0

How to prevent download managers like IDM downloading pdf files from the server

  1. Do not provide direct file path to the pdf-viewer like path/to/pdf-file.pdf. Create a route at your server that serves this file.

    http :// example.com / d-file.php ? file-name=some-file

    This way, you'll have more control over how and when you want to server this file. Plus, download managers that look for files in the html source ending with specified extensions (like .pdf) won't be able to find this link.

  2. Use session to block more than one request to the file route.

    These dms look for ajax request being fired by the browser. The pdf-viewer will make this ajax request first to load the pdf file from the server. Right after that, the download manager will mimic this request and download the file. We can leverage session to block this.

    On the page that has the pdf-viewer, we'll set a session key with random generated string and attach it as query param to the file route. As soon as the pdf file request comes in, we'll verify the token and unset it from session and serve the file. When the next request comes in, we won't have the session key anymore, so we block the request. That session key can only be reset when user reloads the page that has the pdf-viewer.

Sumit Wadhwa
  • 2,825
  • 1
  • 20
  • 34
-1

Simply use <embed /> by replacing <iframe /> to prevent auto download