0

I am generating PDF dynamically on the server in response to params received from ajax request. I need to handle the response in ajax and pass the PDF to PDF.js as a data object.

I do not want to write these PDF to the server - I rather have a clean process that returns the PDF stream instead

I have done something similar successfully using filereader - loading local PDF into PDF.js in the browser as ArrayBuffer.

The responseType is 'application/pdf' and the raw PDF is being returned but I cannot seem to convert it to an ArrayBuffer for PDF.js

var responseText = src
var len = responseText.length
console.log(len);


var buf = new ArrayBuffer(len);
console.log(buf);


var bufView = new Uint8Array(buf);
console.log(bufView);


for (var i=0; i < len; i++) {
    bufView[i] = responseText.charCodeAt(i);
}
console.log(bufView);

var docInitParams = { data: bufView};

PDFJS.getDocument(docInitParams).then(function(pdf) {......

Here is the output of the above code

screen shot

alQemist
  • 362
  • 4
  • 13
  • you can just do `var docInitParams = {data: responseText};` (see https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L379) You solution shall work as well, so I think your responseText contains corrupted data. – async5 Dec 28 '15 at 15:19
  • Try also output sha1 of your PDF data before you feed it to the PDF.js `window.crypto.subtle.digest("SHA-1", bufView).then(function (d) { console.log(new Uint8Array(d)); })` and also before you send it as a stream (on server you can perform e.g. `openssl sha1 test.pdf`) – async5 Dec 28 '15 at 19:09
  • Whoops - does not like that.....TypeError: undefined is not an object (evaluating 'window.crypto.subtle.digest') – alQemist Dec 28 '15 at 19:30
  • I notice it does load the PDF and resize the canvas to fit the page size but does not render it and gives - Warning: Unsupported feature "unknown" – alQemist Dec 28 '15 at 19:33
  • If I load the PDF using the url to the php script then it renders the PDF just fine but if I try to pass in the ajax responseText from the same php script via the data:src method it throws the warning above. – alQemist Dec 28 '15 at 19:38
  • responseText can be used with binary data only when xhr.overrideMimeType('text/plain; charset=x-user-defined') is called. "undefined is not an object" (evaluating 'window.crypto.subtle.digest') happens only on old browsers afaik – async5 Dec 28 '15 at 19:57
  • Thanks for all the help async5 - you did solve my original problem - after further testing it seems that the ajax request is mangling the PDF somehow. The PDF renders the same way, blank, when opening it in a new window - window.open("data:application/pdf," + escape(d.responseText)); – alQemist Dec 29 '15 at 06:58
  • it's not proper way to encode binary data into url without its corruption. You have to `"data:application/pdf;base64," + btoa(d.responseText)` or `URL.createObjectURL(new Blob([d.responseText], {type: 'application/pdf'}))` (Notice that older versions of IE does not have btoa or URL, and also IE truncates long URLs) – async5 Dec 29 '15 at 14:20

1 Answers1

0

I had a similar problem, and succeeded to pass the data directly (via a data:// URL), but I had to modify the source code; you can see my changes here

Community
  • 1
  • 1
Eino Gourdin
  • 4,169
  • 3
  • 39
  • 67