3

Here is the code that loads the pdf file and download it:

$http.get('/retrievePDFFile', {responseType: 'arraybuffer'})
   .success(function (data) {
       var file = new Blob([data], {type: 'application/pdf'});
       var fileURL = URL.createObjectURL(file);
       window.open(fileURL);
});

For some reason, I want to check if the pdf is not blank before it gets downloaded. I don't want to download an empty pdf.

Or someone can tell me, how to convert PDF Stream to json or text, would be great.

Luud van Keulen
  • 1,204
  • 12
  • 38
Mukesh Singh
  • 303
  • 2
  • 5
  • 17
  • Probably something like this: https://stackoverflow.com/questions/12066118/reading-pdf-file-using-javascript – Solo Nov 29 '18 at 12:27
  • 2
    Wouldn't it be easier to check, server-side, whether the pdf is empty? client-side it may be way harder than expected. – briosheje Nov 29 '18 at 12:36
  • ℹ️ This module is meant to be run using Node.js only. It does not work from a web browser @Solo – Mukesh Singh Nov 29 '18 at 12:41
  • Yes, I know, it was meant to be a hint that there are plenty of JS packages that can read PDF. Or you could use that one and do it server-side. – Solo Nov 29 '18 at 12:43
  • `file` has a property `size` attached to it, can you please check in the console what it's returning? – Shaheryar.Akram Nov 29 '18 at 12:43
  • @Shaheryar.Akram beware that the size may change due to file headers or so, so switching from a server to another or chaning the pdf generation process may lead to different sizes, though this might be a solution. Do you have any control over the generation of the file? – briosheje Nov 29 '18 at 12:45
  • @briosheje I agree, is only a temporary workaround and yes we don't have any control over it. – Shaheryar.Akram Nov 29 '18 at 12:47

0 Answers0