1.去官网下载pdf.js。
其中的view.html不要改,直接放在jsp里。
view.js是渲染pdf,下面的方法需要注意的是路径要对。之前文件流已获得,但是渲染不出来就是因为这里路径不对。
1 2 3 4 5 6
| function configure(PDFJS) { PDFJS.imageResourcesPath = path +’resources/ctrl/pdfjs/web/images/’; PDFJS.workerSrc = path +’resources/ctrl/pdfjs/build/pdf.worker.js’; PDFJS.cMapUrl = path +’resources/ctrl/pdfjs/web/web/cmaps/’; PDFJS.cMapPacked = true; }
|
2.修改viewer.js
var DEFAULT_URL = ‘compressed.tracemonkey-pldi-09.pdf‘ 里面是PDF的路径删除该变量定义;
3.ajax调用
写在view.js开头
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| var path = $(“base”).attr(“href”); var PDFData = “”; $.ajax({ type: “GET”, async: false, mimeType: ‘text/plain; charset=x-user-defined’, url: path+”pdf”, success: function(data) { PDFData = data; } }); var rawLength = PDFData.length;
var array = new Uint8Array(new ArrayBuffer(rawLength)); for (var i = 0;i < rawLength; i++) { array[i] = PDFData.charCodeAt(i) & 0xff; } DEFAULT_URL = array;
|
方法一:
1.本地文件流后台处理,只用了web项目,没有任何框架
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public void pdfStreamHandeler(HttpServletRequest request, HttpServletResponse response) { filePath = “d: File file = new File(filePath); byte[] data = null; try { FileInputStream input = new FileInputStream(file); data = new byte[input.available()]; input.read(data); response.getOutputStream().write(data); input.close(); } catch (Exception e) { logger.error(“pdf文件处理异常:” + e.getMessage()); } }
|
方法二:跨域(从云服务器下载)设计知识点(https://www.alibabacloud.com/help/zh/doc-detail/32016.htm?spm=a3c0i.o32014zh.b99.108.e7882928dW9FL)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @RequestMapping(value = “/pdf”, method = RequestMethod.GET) @ResponseBody public byte[] loadPdf1(Model model, HttpServletResponse response, HttpServletRequest request) { final String keySuffixWithSlash = “a.pdf”; OSSClient Server = new OSSClient(endpoint, accessKeyId, accessKeySecret); Date expiration = new Date(new Date().getTime() + 3600 * 1000); GeneratePresignedUrlRequest request1 = new GeneratePresignedUrlRequest(“aaa”, keySuffixWithSlash, HttpMethod.GET); request1.setExpiration(expiration); URL signedUrl = Server.generatePresignedUrl(request1); String url = signedUrl.toString(); Server.shutdown(); System.out.println(“signed url for getObject: “ + signedUrl.toString()); HttpURLConnection httpConn = null; URL urlObj; try {
HttpURLConnHelper HttpURLConnHelper=new HttpURLConnHelper(); InputStream input=HttpURLConnHelper.loadFileFromURL(url); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(“application/pdf;charset=UTF-8”); ServletOutputStream out =null; out = response.getOutputStream(); int read = 0; byte buffBytes[] =new byte[1024]; while((read = input.read(buffBytes)) != -1) { out.write(buffBytes, 0, read); } out.flush(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } return null; }
|
其中HttpURLConnHelper是一个方法类,使用了别人的工具类