【问题标题】:Printing multiple files in angular以角度打印多个文件
【发布时间】:2020-11-25 08:24:58
【问题描述】:

我有多个 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);
    });
}

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

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

我试过用这个

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,只有在第一个打印作业完成后才触发下一个打印作业。浏览器可能会忽略其他作业,因为它们是在处理第一个作业之前触发的。 printjs.crabbly.com/#configuration
  • @crabbly,更新了问题,你能帮忙

标签: javascript angular printjs


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-10-02
    • 2018-06-09
    • 2021-04-08
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 2020-11-21
    相关资源
    最近更新 更多