我尝试使用打印 PDFprint.js https://printjs.crabbly.com/库,它对于单个 PDF 打印工作得很好。现在,当我尝试打印多个 PDF 时,它会抛出一个错误错误:错误多项选择。。另外,我尝试过使用纯 JS,但它会多次提示多个文档。
下面是我们正在使用的代码。
printJS({
printable: ['dummy.pdf', 'dummy1.pdf'],
type:'pdf'
});
请找到附件。
Any help much appreciate!!
最后,经过 1 周的时间,我能够使用 Angular 9 中的 print.js 打印多个 PDF。
以下是打印多个 pdf 的步骤:
-
安装 npm 模块:PDF-lib
npm i pdf-lib
-
在 Angular 文件中导入 npm 模块,并使用以下代码将多个 pdf 合并为单个 pdf(例如 app.components.ts)
import { PDFDocument } from 'pdf-lib'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'print-multiple-pdfs';
/* Convert the merged pdf array buffer to blob url for print or open in browser.*/
downloadFile(data) {
const blob = new Blob([data], { type: 'application/pdf' });
const url= window.URL.createObjectURL(blob);
//window.open(url);
printJS({
printable: url,
type: 'pdf',
})
}
async printPDFS() {
/* Array of pdf urls */
let pdfsToMerge = ['https://cors-anywhere.herokuapp.com/https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf', 'https://cors-anywhere.herokuapp.com/http://africau.edu/images/default/sample.pdf', 'https://cors-anywhere.herokuapp.com/https://www.hq.nasa.gov/alsj/a17/A17_FlightPlan.pdf']
const mergedPdf = await PDFDocument.create();
for (const pdfCopyDoc of pdfsToMerge) {
const pdfBytes = await fetch(pdfCopyDoc).then(res => res.arrayBuffer())
//const pdfBytes = fs.readFileSync(pdfCopyDoc);
const pdf = await PDFDocument.load(pdfBytes);
const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
copiedPages.forEach((page) => {
mergedPdf.addPage(page);
});
}
const mergedPdfFile = await mergedPdf.save();
this.downloadFile(mergedPdfFile);
}
}
-
调用打印函数(app.component.html)
<button (click)="printPDFS()">print pdfs</button>
参考:pdf-lib https://www.npmjs.com/package/pdf-lib
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)