Java uses Pdf.js to view PDF documents online

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.