2

I have an Api that returns the data in the format of

{ fileName: string, blob: Blob }[]

I want to print all these files, so I am using

_files.forEach((_fileInfo) => {
      const blobUrl = URL.createObjectURL(_fileInfo.blob);
      const oWindow = window.open(blobUrl, "print");
      oWindow.print();
      oWindow.close();
});

this opens the multiple print windows, but in preview it shows blank documents.

but when i download all these files as a zip it downloads the correct PDF files.

// add files to zip
files.forEach((_fileInfo) => {
    zip.file(_fileInfo.fileName, _fileInfo.blob);
});

// download and save
return zip.generateAsync({ type: "blob" }).then((content) => {
    if (content) {
        return saveAs(content, name);
    }
});
  1. What could be the issue,

  2. is there any way to print all documents in a sequence without opening multiple windows?

4
  • does it work properly for a single file, without a loop? Commented Jul 31, 2020 at 7:58
  • no, it shows blank document in preview Commented Jul 31, 2020 at 11:50
  • Could you specify what is _files collection, e.g. how do you get the value of _fileInfo ? Commented Aug 2, 2020 at 19:42
  • @AlenaKastsiukavets, printJs did the work, I mean now it shows the proper PDF preview, now only problem is to merge the pdf, is there any way to print multiple PDF files in single click. Commented Aug 3, 2020 at 6:29

2 Answers 2

2

Pdf file takes time to load thats why it was showing blank document, So i used print-js to solve this issue.

how to import

import * as printJS from "print-js";

how to use

const blobUrl = URL.createObjectURL(_fileInfo.blob);
printJS(blobUrl);
Sign up to request clarification or add additional context in comments.

Comments

0

In case, you don't want to use printJS

I was facing the same issue but in my case I only have to deal with one file at a time. I solved it by putting the print function in a timeout block, so in your case this would be

        setTimeout(function(){
            oWindow.print();
        }, 1000)

if this works then great, if not then you might also need to set the source of window equal to the 'blobUrl' that you created. This should be done after you come out of the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.