Prevent PDF auto download by idm using pdf.js

22,576

Solution 1

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.

Solution 2

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.

Solution 3

try with this

<embed src="'.$baseurl.'/assets/pdf/web/viewer.html?file='.urlencode($pdf_url).'" type="text/html" >

Solution 4

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

Share:
22,576

Related videos on Youtube

user3003810
Author by

user3003810

Updated on July 17, 2022

Comments

  • user3003810
    user3003810 almost 2 years

    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

    • Halayem Anis
      Halayem Anis about 9 years
      (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 ?
    • user3003810
      user3003810 about 9 years
      (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
    user3003810 about 9 years
    I tried <object width="400" height="400" data="targetfile"></object> depending on many solutions on Internet, but the viewer does not appear the pdf file.
  • Mohiyo Deen
    Mohiyo Deen about 5 years
    What about if my PDF Files are larger than 500MB to 1GB