0

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 servlet url: /dowload/fileid/123,it doesn't load via 206 partial content (range requests) but 200 and then viewed in the viewer.
class Scratch {
  public static void main(String[] args) {
    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. Split 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

ssuperczynski
  • 3,190
  • 3
  • 44
  • 61
wideweide
  • 41
  • 8

1 Answers1

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

wideweide
  • 41
  • 8