Code Guru
Code Guru

Reputation: 15578

Issue while printing pdf files from Blob angular

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?

Upvotes: 2

Views: 5578

Answers (2)

sharmaaa
sharmaaa

Reputation: 13

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.

Upvotes: 0

Code Guru
Code Guru

Reputation: 15578

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);

Upvotes: 2

Related Questions