# pdfjs **Repository Path**: yargs/pdfjs ## Basic Information - **Project Name**: pdfjs - **Description**: 实现pdfjs一系列功能 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-06-02 - **Last Updated**: 2023-06-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # js 该项目通过iframe 的形式引入pdf.js, 原则上不管是不是vue,都可以使用 实现的功能: 1. 解决pdf.js跨域问题 2. 能够对pdf里的文本复制 3. 选中内容后可以弹出操作提示框 4. 实现搜索并定位 5. 通过contentWindow方式跳转指定页面 ```js iframe.contentWindow.PDFViewerApplication.pagesCount iframe.contentWindow.PDFViewerApplication.page // 注: 以下形式也可以跳转到指定页 // https://commoncms.ynyplt.cn/file-server/13648711629/9b613676-d867-47e3-873d-47d6d59015d5.pdf#page=1 ``` # java controller ```java // 接口部分 @RequestMapping("/pdfdownload") @ResponseBody public void fileDownload(@RequestParam("filename") String filename, HttpServletResponse response) throws UnsupportedEncodingException { // 通过网络获取文件流 DownloadUtils.getFileStream(filename, response); } ``` DownloadUtils ```java package com.example.mybatis.utils; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class DownloadUtils { // 下载工具部分 // 将文件流传送到response public static void getFileStream(String filename, HttpServletResponse response) { try { // 根据文件url下载文件 openFile(filename, response); } catch (Exception e) { System.out.println("下载失败"); } } // 通过url下载文件的方法 private static void openFile(String filePath, HttpServletResponse response) { // 服务器状态 int HttpResult; try { // URL URL url = new URL(filePath); // URL连接对象 URLConnection urlconn = url.openConnection(); urlconn.connect(); HttpURLConnection httpconn = (HttpURLConnection) urlconn; HttpResult = httpconn.getResponseCode(); if (HttpResult != HttpURLConnection.HTTP_OK) { System.out.println("网络连接失败"); throw new ConnectException("网络连接失败"); } else { InputStream in = urlconn.getInputStream(); try { String fileName = "test.pdf"; response.setContentType("application/pdf;charset=utf-8"); response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); int b = 0; byte[] buffer = new byte[512]; while (b != -1) { b = in.read(buffer); if (b != -1) { response.getOutputStream().write(buffer, 0, b); } } } catch (Exception e) { e.printStackTrace(); System.out.println("文件取得失败"); } finally { try { if (in != null) { in.close(); } response.getOutputStream().flush(); } catch (IOException e) { e.printStackTrace(); System.out.println("文件取得失败"); } } } } catch (Exception e) { e.printStackTrace(); System.out.println("文件取得失败"); } } } ```