1. Download the corresponding Pdf.js file:
Recommended Address:
https://github.com/mozilla/pdf.js/
http://mozilla.github.io/pdf.js/
2. When the download is complete, open the corresponding viewer.js file.
As you can see, the COMPRESSED.TRACEMONKEY-PLDI-09.PD F file is opened by default, and if we need to open the address we specify later, we will empty the default address.
3. In this way, we can use the pass file parameter to dynamically specify an open PDF file, such as:
1 http://localhost:8080/mypdf/web/viewer.html?file=123.pdf
Tips: must be loaded to server access
4. Now we use the controller to dynamically locate the PDF of the disk and load the display:
1@RequestMapping (value = "Showviewpdf")2 Public voidShowviewpdf ()throwsIOException {4File f =NewFile ("d:/aaa/123.pdf");5 getResponse (). reset ();6GetResponse (). setContentType ("Multipart/form-data");7GetResponse (). SetHeader ("Content-disposition", "Attachment;filename=123.pdf");8OutputStream out =getResponse (). Getoutputstream ();9 Try {Ten Fileutil.writetostream (F, out); A}Catch(IOException e) { - e.printstacktrace (); -}finally { the Out.flush (); - out.close (); - } -}
The address now visited becomes the following address:
1 Http://localhost:8080/mypdf/web/viewer.html?file=http://localhost:8080/mypdf/pdfPageController/showViewPDF
5. Now we display the corresponding ID file by ID number:
1@RequestMapping (value = "Showviewpdf")2 Public voidShowviewpdf (String ID)throwsIOException {3Tuploadfileblfy Tuploadfile =Pdfpageservice.getpdfbyid (ID); This is the path to the database corresponding to the storage file according to the ID number.4File f =NewFile (Tuploadfile.getfilepath ()); Tuploadfile.getfilepath (); This is the path to get the corresponding ID file5 getResponse (). reset ();6GetResponse (). setContentType ("Multipart/form-data");7GetResponse (). SetHeader ("Content-disposition", "attachment;filename=" +tuploadfile.getfilename ()); Get the corresponding file name8OutputStream out =getResponse (). Getoutputstream ();9 Try {Ten Fileutil.writetostream (F, out); A}Catch(IOException e) { - e.printstacktrace (); -}finally { the Out.flush (); - out.close (); - } -}
As a result, the requested address now changes to this:
1 http://localhost:8080/mypdf/web/viewer.html?file=http://localhost:8080/mypdf/pdfPageController/ showviewpdf?id%3d402881d35dcb410f015dcb455cfc0001
The parameters following the ID are described in detail:
%3d: This is an escape character for the equals sign (=);
402881D35DCB410F015DCB455CFC0001: This is the file's UUID number, unique identifier;
Well, that's probably the way it is, hoping to help everyone.
Java uses Pdf.js to view PDF documents online