Lazy load :How to display multiple pdf documents as one with pdf.js?

China☆狼群 提交于 2020-06-17 14:49:32

问题


My purpose is to display pdf use pdf.js in lazy mode,I have two choice:

  1. Use disableRange=false It worked fine when use a url in nginx,but when I use a java sevlet url: /dowload/fileid/123,it doesn't load via 206 partial content (range requests) but 200 and then viewed in the viewer.

public void download(String identNo,HttpServletRequest request,HttpServletResponse response) {
		File file = getFileFromServer(identNo);
        BufferedInputStream bis = null;
        OutputStream os = null;
        BufferedOutputStream bos = null;
        InputStream is = null;
        try {
            is =new FileInputStream(file);
            bis = new BufferedInputStream(is);
            os = response.getOutputStream();
            bos = new BufferedOutputStream(os);
            int startByte,endByte,totalByte;
            if (request != null && request.getHeader("range") != null) {
                String[] range = request.getHeader("range").replaceAll("[^0-9\\-]", "").split("-");
                totalByte = is.available();
                startByte = Integer.parseInt(range[0]);
                if (range.length > 1) {
                    endByte = Integer.parseInt(range[1]);
                } else {
                    endByte = totalByte - 1;
                }
                     response.setStatus(206);
            } else {
                totalByte = is.available();
                startByte = 0;
                  endByte = totalByte - 1;
                response.setHeader("Accept-Ranges","bytes");
                response.setStatus(200);
            }
            int length = endByte - startByte + 1;
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Range", "bytes " + startByte + "-" + endByte + "/" + totalByte);
            response.setContentType("Content-Type: application/octet-stream");
            response.setContentLength(length);
            bis.skip(startByte);
            int len = 0;
            byte[] buff = new byte[1024 * 64];
            while ((len = bis.read(buff, 0, buff.length)) != -1) {
                if (length <= len) {
                    bos.write(buff, 0, length);
                    break;
                } else {
                    length -= len;
                    bos.write(buff, 0, len);
                }
            }
        } catch (IOException e) {
        } finally {
            FileUtil.closeQuiet(bos);
            FileUtil.closeQuiet(os);
            FileUtil.closeQuiet(bis);
            FileUtil.closeQuiet(is);
        }

	}
  1. spit big pdf file in server side,then display multiple pdf documents as one with pdf.js

I found this : Is there a way to combine PDFs in pdf.js? but it needed to load all file at once,what I need is: when scroll to bottom of file,load next file then merge to current pdf


回答1:


  1. PDF.js,set the parameter: "disableAutoFetch": true, "disableStream": true
  2. server support accept ranges,range Then pdf.js whill only fetch data in pages are visible now from server

https://github.com/mozilla/pdf.js/issues/11933



来源:https://stackoverflow.com/questions/61401021/lazy-load-how-to-display-multiple-pdf-documents-as-one-with-pdf-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!