We are using PDF.js as PDF viewer and the following code is used as the endpoint for downloading PDF. The issue is sometimes, ~80% of the times, when a large PDF, say > 5 MB, is opened the PDF.js is showing error "Stream must have data". But when we open smaller PDFs in KB then it always working fine. On debugging it was found that whenever the issue is occurring the response header contains "content-length: 0" but when it is working fine the header "Transfer-Encoding: chunked" is present.
@GetMapping(value = { "/view/{id}" }, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadFile(@PathVariable("id") DiskFileId docId, HttpServletResponse response) throws IOException {
try {
StorageDocumentDTO document = service.getDocument(docId);
if(document== null)
return;
response.setContentType(document.getMimeType());
IOUtils.copy(new FileInputStream(document.getDocumentPath().toFile()), response.getOutputStream());
} catch (Exception e) {
throw new RuntimeException("Document not found", e);
}
}
Any help, on how to debug this further, is appreciated