Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get byte-array data from servlet to pdf.js

How can I get this:

File file = new File(doneDir + "\\" + batchName + "\\" + fileName);
byte[] by = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(by);
fis.close();

response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=TheDocument." + "pdf");
response.getOutputStream().write(by);
response.getOutputStream().flush();
response.getOutputStream().close();

From my servlet either doGet or doPost to the pdf.js function:

var data = (byte array returned from servlet)
PDFJS.getDocument(data).then(function(pdf) {});
like image 948
Chandler E. Avatar asked Feb 23 '26 04:02

Chandler E.


1 Answers

Based on the example, I would say that instead of

var data = (byte array returned from servlet)
PDFJS.getDocument(data).then(function(pdf) {});

I think you should use:

PDFJS.getDocument(servlet_url).then(function(pdf) {
   // you can now use *pdf* here
});

A servlet that returns a PDF file should be no different to the client than a PDF file on the server, and the example uses PDFJS.getDocument('helloworld.pdf').then(... so this function obviously takes a URL.

like image 91
developerwjk Avatar answered Feb 24 '26 17:02

developerwjk