Today, after the launch of a customer site (using a new generation of Guangzhou virtual space) to read the uploaded PDF file when reading errors, by directly in the browser to enter the file address to find the file address is redirected (hehe!). ), the result is that the PDF file source has been directly converted from local to cross-domain acquisition. Solve the problem!
1, Pdf.js method of obtaining files
You can modify the Defaulturl app optioninchThe Web/app_options.jsfileOr can append the?file= QuerystringTo the viewer URL, e.g. http://mozilla.github.com/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf. In the latter case, the PDF Path/url must is encoded using encodeURIComponent ().The Viewer can be started without any PDF loaded by setting the Defaulturl app option to an emptystringor by using the?file= QuerystringWithout any location specified. Use Pdfviewerapplication.open (file) to load the PDFfilelater. You can use raw binary data to open a PDF document:use uint8array instead of URLinchThe Pdfviewerapplication.open call. If you have base64 encoded data, please decode it first – not all browsers has Atob or data URI scheme support. (The Base64 conversion operation uses MoreMemory, so we recommend delivering raw PDF data as typed arrayinchFirst place.)
The above is taken from the GitHub project is probably three ways
- Set Defaulturl
- With the file parameter followed by the path such as: http://mozilla.github.com/pdf.js/web/viewer.html?file= Compressed.tracemonkey-pldi-09.pdf
- Use Pdfviewerapplication.open to open Uint8array to get the file stream form
The first two methods can only get the same source file, the third method is the cross-domain I need to get the file method.
2. Get the file through the background (PHP) and convert it to a file stream back to the front end
<?php
Just use file_get_contents to get the re-output.
$filefile_get_contents($url); Echo $file;
?>
3. Pdf.js to get the file stream through Ajax synchronization request
varPdfdata = "";varGETURL = "";varBASEURL = "http://www.zdxhxfzxwx.com.img26752.200cdn.com:9898"; GetUrl= BaseUrl + getquerystring ("FilePath"); $.ajax ({type:"POST", Async:false,//ContentType: "Application/x-www-form-urlencoded", MimeType:' Text/plain; Charset=x-user-defined ', URL:"/plus/getfiletobinary.php", Success:function(data) {Pdfdata=data; }, data: {"url": GetUrl,}}); varRawlength =pdfdata.length; Console.log (rawlength);//Convert to a uint8array type that pdf.js can directly parse, see pdf.js-4068varArray =NewUint8array (NewArrayBuffer (rawlength)); for(i = 0; i < rawlength; i++) {Array[i]= Pdfdata.charcodeat (i) & 0xFF; } Default_url=Array;functiongetquerystring (name) {varReg =NewRegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "I"); varr = Window.location.search.substr (1). Match (REG); if(r! =NULL)returnUnescape (r[2]); return NULL; }
This method has a problem: 1, the PHP server to support the file read (can be modified by modifying the PHP configuration file if you have this permission to modify)
2,php read and output file execution time is relatively long
Pdf.js How do I read PDF files across domains?