简体   繁体   中英

Prevent PDF auto download by idm using pdf.js

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>

在此处输入图像描述

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 (eg *.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 阻止处理文件类型

  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.

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.

试试这个

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

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

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>

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.'');

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.

只需通过替换<iframe />来使用<embed /> > 来防止自动下载

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM