This commit is contained in:
mol
2024-06-06 17:42:35 +08:00
parent 80dd69b366
commit 6f8082ff50
5 changed files with 131 additions and 1 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
/node_modules node_modules

Binary file not shown.

55
src/js/print/index.html Normal file
View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="handlePrint()">打印</button>
<script src="./node_modules/print-js/dist/print.js"></script>
<script src="./node_modules/pdf-lib/dist/pdf-lib.js"></script>
<script>
async function handlePrint() {
const pdfUrls = [
"./assets/test.pdf",
"./assets/test.pdf",
"./assets/test.pdf",
];
const url = await mergePDFs(pdfUrls);
// 调用打印功能,无需打开文件再手动打印
printJS(url);
}
// 批量打印与分页打印
async function mergePDFs(pdfUrls) {
// 创建一个新的空白PDF文档
const mergedPdfDoc = await PDFLib.PDFDocument.create();
for (const pdfUrl of pdfUrls) {
// 获取PDF文件的二进制数据
const pdfBytes = await fetch(pdfUrl).then((response) =>
response.arrayBuffer()
);
// 将获取到的PDF文件添加到新的文档中
const pdfDoc = await PDFLib.PDFDocument.load(pdfBytes);
// 如果单个PDF为多页则要一页一页往新建的PDF中添加
const copiedPages = await mergedPdfDoc.copyPages(
pdfDoc,
pdfDoc.getPageIndices()
);
copiedPages.forEach((page) => mergedPdfDoc.addPage(page));
}
// 将合并后的PDF保存为Blob对象
const mergedPdfBytes = await mergedPdfDoc.save();
const mergedPdfBlob = new Blob([mergedPdfBytes], {
type: "application/pdf",
});
return URL.createObjectURL(mergedPdfBlob);
}
</script>
</body>
</html>

59
src/js/print/package-lock.json generated Normal file
View File

@ -0,0 +1,59 @@
{
"name": "printjs",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "printjs",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"pdf-lib": "^1.17.1",
"print-js": "^1.6.0"
}
},
"node_modules/@pdf-lib/standard-fonts": {
"version": "1.0.0",
"resolved": "https://registry.npmmirror.com/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
"integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==",
"dependencies": {
"pako": "^1.0.6"
}
},
"node_modules/@pdf-lib/upng": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/@pdf-lib/upng/-/upng-1.0.1.tgz",
"integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==",
"dependencies": {
"pako": "^1.0.10"
}
},
"node_modules/pako": {
"version": "1.0.11",
"resolved": "https://registry.npmmirror.com/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw=="
},
"node_modules/pdf-lib": {
"version": "1.17.1",
"resolved": "https://registry.npmmirror.com/pdf-lib/-/pdf-lib-1.17.1.tgz",
"integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==",
"dependencies": {
"@pdf-lib/standard-fonts": "^1.0.0",
"@pdf-lib/upng": "^1.0.1",
"pako": "^1.0.11",
"tslib": "^1.11.1"
}
},
"node_modules/print-js": {
"version": "1.6.0",
"resolved": "https://registry.npmmirror.com/print-js/-/print-js-1.6.0.tgz",
"integrity": "sha512-BfnOIzSKbqGRtO4o0rnj/K3681BSd2QUrsIZy/+WdCIugjIswjmx3lDEZpXB2ruGf9d4b3YNINri81+J0FsBWg=="
},
"node_modules/tslib": {
"version": "1.14.1",
"resolved": "https://registry.npmmirror.com/tslib/-/tslib-1.14.1.tgz",
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg=="
}
}
}

16
src/js/print/package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "printjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"pdf-lib": "^1.17.1",
"print-js": "^1.6.0"
}
}