问题
My purpose is to display pdf use pdf.js in lazy mode,I have two choice:
- 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);
}
}
- 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:
- PDF.js,set the parameter: "disableAutoFetch": true, "disableStream": true
- 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