PDF file preview and download

Background: The project preview and download the pdf file of the

    environment: jdk1.8, SpringBoot2.0, Maven
    pdf.js Download: http://mozilla.github.io/pdf.js/getting_started/#download (download 2.1. 266 version can)

The copy-in download source projects
    Review viewer.js:

The defaultUrl: {
    value: 'compressed.tracemonkey-PLDI-09.pdf', --- is the path where the default pdf
    kind: OptionKind.VIEWER
  }
  modify :
  defaultUrl: {
    value: '',
    kind: OptionKind.VIEWER
  }

    opens a new window preview:

     <INPUT type = "Button" value = "preview" ID = "viewBtn">
    <Script type = "text / JavaScript">
        $ ( "#viewBtn")
                .click (
                        function () {
                            var curWwwPath = window.document.location.href;
                            var = pathName window.document.location.pathname;
                            var pos = curWwwPath.indexOf(pathName);
                            var localhostPath = curWwwPath.substring(0, pos);

                         // request call back here to get the interface and open the pdf file in a new window

             window.open("http://localhost:8081/api/file/preview?fileName=2019_PDF.pdf");


                        });
    // background controller code read pdf file to the specified directory according distal incoming fileName, on display
    @RequestMapping (value = "/ preview", Method = RequestMethod.GET)
    public void prePDF (String fileName, the HttpServletRequest Request , the HttpServletResponse Response) {
        logger.info ( "file name:" + fileName);
        file file = new new file ( "E: / PDF /" + fileName);
        IF (File.Exists ()) {
            byte [] = null Data ;
            the try {
                the FileInputStream new new INPUT = the FileInputStream (File);
                Data = new new byte [input.available ()];
                input.read (Data);
                . response.getOutputStream () Write (Data);
                input.close ();
            } catch (Exception e) {
                logger.info("pdf文件处理异常...");
            }
        }
    }

    PDF文件下载:

<input type="button" value="下载" id="download">
$("#download").click(function() {
            var form = $("<form>");
            form.attr("style", "display:none");
            form.attr("target", "");
            form.attr("method", "post");//提交方式为post
            form.attr("action", "/downloadFile");//定义action
            $("body").append(form);
            form.submit();
        });
//后台代码
@RequestMapping(value="/downloadFile")
    public void downloadFile(HttpServletResponse response) {
        String downloadFilePath = "E: / pdf /"; // path to the downloaded file in the server,
        String fileName = "0602.pdf"; // name of the file to be downloaded
    
        File file = new File (downloadFilePath + fileName);
        IF (File.Exists ()) {
            the response.setContentType ( "file application / downloads-force"); // set the enforced downloading without opening            
            Response.AddHeader ( "the Content-Disposition", "Attachment; fileName =" + fileName);
            byte ; [] = new new byte Buffer [1024]
            the FileInputStream FIS = null;
            BufferedInputStream BIS = null;
            the try {
                FIS new new = the FileInputStream (File);
                BIS = new new BufferedInputStream (FIS);
                OutputStream outputStream = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                  outputStream.write(buffer, 0, i);
                  i = bis.read(buffer);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(bis != null) {
                    try {
                        bis.close();
                    }catch(IOException e) {
                        e.printStackTrace();
                    }
                }
                if(fis != null) {
                    try {
                        fis.close ();
                    } the catch (IOException E) {
                        e.printStackTrace ();
                    }
                }
            }
        }
    }

 
    at the top will have a corresponding preview page toolbar, print, download, flip, zoom, etc., according to the actual needs of the individual, can be View page source code, comment out the corresponding function in viewer.html in.

    can be read PDF content files pdfbox:

introducing jar package:
<-! read PDF dependent ->
        <dependency>
            <groupId> org.apache. PDFBox </ the groupId>
            <the artifactId> PDFBox </ the artifactId>
            <Version> 2.0.4 </ Version>
        </ dependency>


background reading pdf file code:

public String viewPDF (proCode String, String fileName, String originPage,HttpServletRequest request) {
        request.setAttribute ( "dse_sessionId", request.getParameter ( "dse_sessionId") the TRIM ().);
        logger.info ( "Disclosure Report Preview File name:" + fileName);
        File File = new new File (constant.getExposeLocalDir () + fileName);
        IF (! File.Exists ()) {// file does not exist, from the FTP download files to local
            }
        }
        // reading the content file pdf - code for
        the try {
            PDDocument = PDDocument.load document (file);
            document.getClass ();
            IF (document.isEncrypted ()!) {
                PDFTextStripperByArea new new PDFTextStripperByArea Stripper = ();
                stripper.setSortByPosition (to true);
                the PDFTextStripper textStripper the PDFTextStripper new new = ();
                String exposeContent = textStripper.getText(document);
                String[] content = exposeContent.split("\\n");
                StringBuffer stringBuffer = new StringBuffer();
                for(String line:content) {
                    stringBuffer.append(line);
                }
            }
            
        } catch (Exception e) {
            logger.info("读取pdf文件异常...");
        }
    
        return "";
    }

 

Disclaimer: The film article reprinted from: https: //blog.csdn.net/Xing_Pengfei/article/details/97649888

On this basis, the author just a few changes, I use the Maven project, and this may be slightly different bit blog
----------------
Disclaimer: This article is CSDN blogger "midnight apprentice xpf 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/Xing_Pengfei/article/details/97649888

Guess you like

Origin www.cnblogs.com/langcangsheng/p/11527898.html