<script src='https://unpkg.com/pdf-lib/dist/pdf-lib.js'></script>
<script src='https://unpkg.com/pdf-lib/dist/pdf-lib.min.js'></script>
<script>
async function mergeAllPDFs(urls) {
const pdfDoc = await PDFLib.PDFDocument.create();
const numDocs = urls.length;
for (var i = 0; i < numDocs; i++) {
const donorPdfBytes = await fetch(urls[i]).then(res => res.arrayBuffer());
const donorPdfDoc = await PDFLib.PDFDocument.load(donorPdfBytes);
const docLength = donorPdfDoc.getPageCount();
for (var k = 0; k < docLength; k++) {
const [donorPage] = await pdfDoc.copyPages(donorPdfDoc, [k]);
pdfDoc.addPage(donorPage);
}
}
const pdfBytes = await pdfDoc.save();
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
// 创建一个下载链接
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = 'merged.pdf'; // 指定下载文件名
document.body.appendChild(downloadLink); // 将链接添加到页面中
downloadLink.click(); // 模拟点击以下载文件
document.body.removeChild(downloadLink); // 下载后移除链接
URL.revokeObjectURL(url); // 释放创建的URL对象
}
</script>
调用:
mergeAllPDFs([“/file/save/2024-10-31-fdad0790-bef7-4d0c-bc0f-988c056fe489.pdf”,”/file/save/2024-10-31-22c2e761-a2e2-4459-b5a6-a67f78c1ce25.pdf”])
mergeAllPDFs([“bc089.pdf”,”5.pdf”])两个文件合并下载