在 angular 中打印多个文件

Printing multiple files in angular

我有多个 blob,我正在从中创建 blob url 并使用 print-js 库来显示浏览器的打印预览屏幕。

我有

async printDocuments(): Promise<any> {
    const _files: { fileName: string, blob: any }[] = await this._getFiles();
    _files.forEach((_fileInfo, index) => {
        const blobUrl = URL.createObjectURL(_fileInfo.blob);
        printJS(blobUrl);
    });
}

但这只显示第一个文件的打印预览对话框。

如何通过合并或打开多个打印来打印所有文档windows。

我试过用这个

printJS({
    printable: _files[0].blob,
    type: "pdf",
    onPrintDialogClose: () => {
      console.log("nex");
    }
});

但现在显示

core.js:4196 ERROR Error: Uncaught (in promise): TypeError: params.printable.charAt is not a function
TypeError: params.printable.charAt is not a function

onPrintDialogClose 为我工作。

这是我做的

async printDocuments(): Promise<any> {
     const _files: { fileName: string, blob: any }[] = await this._getFiles();
     if (_files && _files.length > 0) {
            this._printDocument(_files, 0);
     }
}

private _printDocument(files: { fileName: string, blob: any }[], index: number): void {
    const blobUrl = URL.createObjectURL(files[index].blob);
    printJS({
        printable: blobUrl,
        type: "pdf",
        onPrintDialogClose: () => {
            index = index + 1;
            if (index < files.length) {
                this._printDocument(files, index);
            }
        }
    });
}

这是documentation

感谢@crabbly

编辑 - 最新的chrome不适用于回调

对于最新的 chrome,onPrintDialogClose 不起作用。为此,从 printJs 调用中删除 onPrintDialogClose 并在 printJs 调用后添加以下代码。

// fix : printjs issue #495
const handler = () => {
    // Make sure the event only happens once.
    window.removeEventListener("mouseover", handler);

    // function that you want to execute for onPrintDialogClose
    onCloseCallback();

    // Remove iframe from the DOM, by default 'printJS'
    const iframe = document.getElementById("printJS");

    if (iframe) {
        iframe.remove();
    }
    };
setTimeout(() => { window.addEventListener("mouseover", handler); }, 1000);