feat: add pdf-merge-safe tool (closes #233)

This commit is contained in:
“[firstname 2025-10-03 12:29:42 +05:30
commit a42bb5824b
12 changed files with 293 additions and 0 deletions

54
pdf-merge-safe/package-lock.json generated Normal file
View file

@ -0,0 +1,54 @@
{
"name": "pdf-merge-safe",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"pdf-lib": "^1.17.1"
}
},
"node_modules/@pdf-lib/standard-fonts": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
"integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==",
"license": "MIT",
"dependencies": {
"pako": "^1.0.6"
}
},
"node_modules/@pdf-lib/upng": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz",
"integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==",
"license": "MIT",
"dependencies": {
"pako": "^1.0.10"
}
},
"node_modules/pako": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==",
"license": "(MIT AND Zlib)"
},
"node_modules/pdf-lib": {
"version": "1.17.1",
"resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz",
"integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==",
"license": "MIT",
"dependencies": {
"@pdf-lib/standard-fonts": "^1.0.0",
"@pdf-lib/upng": "^1.0.1",
"pako": "^1.0.11",
"tslib": "^1.11.1"
}
},
"node_modules/tslib": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
"license": "0BSD"
}
}
}

View file

@ -0,0 +1,5 @@
{
"dependencies": {
"pdf-lib": "^1.17.1"
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,58 @@
{
"name": "pdf-merge-safe",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "pdf-merge-safe",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"pdf-lib": "^1.17.1"
}
},
"node_modules/@pdf-lib/standard-fonts": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
"integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==",
"license": "MIT",
"dependencies": {
"pako": "^1.0.6"
}
},
"node_modules/@pdf-lib/upng": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz",
"integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==",
"license": "MIT",
"dependencies": {
"pako": "^1.0.10"
}
},
"node_modules/pako": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz",
"integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==",
"license": "(MIT AND Zlib)"
},
"node_modules/pdf-lib": {
"version": "1.17.1",
"resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz",
"integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==",
"license": "MIT",
"dependencies": {
"@pdf-lib/standard-fonts": "^1.0.0",
"@pdf-lib/upng": "^1.0.1",
"pako": "^1.0.11",
"tslib": "^1.11.1"
}
},
"node_modules/tslib": {
"version": "1.14.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz",
"integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==",
"license": "0BSD"
}
}
}

View file

@ -0,0 +1,16 @@
{
"name": "pdf-merge-safe",
"version": "1.0.0",
"description": "Merge PDFs with safe handling of encrypted/signed inputs (pdf-lib).",
"type": "commonjs",
"scripts": {
"scan": "node src/scan.js",
"merge": "node src/index.js",
"merge:yes": "node src/index.js --yes",
"make:normal": "node scripts/make-normal.js",
"make:encrypted": "node scripts/make-encrypted.js"
},
"dependencies": {
"pdf-lib": "^1.17.1"
}
}

View file

@ -0,0 +1,20 @@
const fs = require("fs");
const path = require("path");
const { PDFDocument, StandardFonts, rgb } = require("pdf-lib");
async function make(text, outfile) {
const pdf = await PDFDocument.create();
const page = pdf.addPage([420, 300]);
const font = await pdf.embedFont(StandardFonts.Helvetica);
page.drawText(text, { x: 40, y: 150, size: 18, font, color: rgb(0,0,0) });
const bytes = await pdf.save();
fs.writeFileSync(outfile, bytes);
console.log("✅ Created", outfile);
}
(async () => {
const dir = "examples";
fs.mkdirSync(dir, { recursive: true });
await make("Hello from normal1", path.join(dir, "normal1.pdf"));
await make("Hello from normal2", path.join(dir, "normal2.pdf"));
})();

View file

@ -0,0 +1,94 @@
const fs = require("fs");
const path = require("path");
const { PDFDocument } = require("pdf-lib");
const { askYesNo } = require("./prompt");
// helpers
const isPdf = (p) => p.toLowerCase().endsWith(".pdf");
const exists = (p) => fs.existsSync(p);
const read = (p) => fs.readFileSync(p);
function listPdfs(target) {
if (!exists(target)) return [];
const stat = fs.lstatSync(target);
if (stat.isDirectory()) {
return fs.readdirSync(target)
.filter((f) => isPdf(f))
.map((f) => path.join(target, f));
}
return isPdf(target) ? [target] : [];
}
async function safeLoad(buffer, filename, autoYes) {
try {
return await PDFDocument.load(buffer); // normal, non-encrypted
} catch (err) {
const msg = String(err.message).toLowerCase();
if (!msg.includes("encrypted")) throw err;
console.warn(`\n🔐 Detected encrypted/signed file: ${filename}`);
console.warn(`⚠️ If you continue, encryption/signature will be REMOVED in merged output.`);
if (!autoYes) {
const ok = await askYesNo("Continue by stripping protection?");
if (!ok) return null; // skip
}
return await PDFDocument.load(buffer, { ignoreEncryption: true });
}
}
async function merge(files, outPath, autoYes) {
const merged = await PDFDocument.create();
let total = 0;
for (const file of files) {
const buf = read(file);
const name = path.basename(file);
// heuristic signature marker
const hasSig = buf.includes("/Sig");
const doc = await safeLoad(buf, name, autoYes);
if (!doc) { console.log(`⏭️ Skipped: ${name}`); continue; }
if (hasSig) {
console.warn(`⚠️ ${name} appears signed. Merging will invalidate the signature.`);
}
const pages = await merged.copyPages(doc, doc.getPageIndices());
pages.forEach((p) => merged.addPage(p));
total += pages.length;
console.log(` Added ${pages.length} page(s) from ${name}`);
}
if (total === 0) { console.log("❌ No pages added. Nothing to save."); return; }
fs.mkdirSync(path.dirname(outPath), { recursive: true });
const bytes = await merged.save();
fs.writeFileSync(outPath, bytes);
console.log(`\n✅ Saved: ${outPath} (total pages: ${total})`);
}
// -------- CLI --------
// Usage:
// node src/index.js [--yes|-y] <output.pdf> <fileOrFolder1> [fileOrFolder2 ...]
(async () => {
const args = process.argv.slice(2);
const autoYes = args.includes("--yes") || args.includes("-y");
const a = args.filter((x) => x !== "--yes" && x !== "-y");
if (a.length < 2) {
console.log("Usage: node src/index.js [--yes|-y] <output.pdf> <fileOrFolder1> [fileOrFolder2 ...]");
process.exit(1);
}
const out = a[0];
const inputs = a.slice(1);
let files = [];
inputs.forEach((p) => { files = files.concat(listPdfs(p)); });
if (files.length === 0) { console.log("❌ No PDFs found in given paths."); process.exit(1); }
await merge(files, out, autoYes);
})();

View file

@ -0,0 +1,13 @@
const readline = require("readline");
function askYesNo(question) {
return new Promise((resolve) => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question(`${question} (y/n): `, (ans) => {
rl.close();
resolve(String(ans).trim().toLowerCase().startsWith("y"));
});
});
}
module.exports = { askYesNo };

View file

@ -0,0 +1,33 @@
const fs = require("fs");
const path = require("path");
const { PDFDocument } = require("pdf-lib");
function gather(p) {
if (!fs.existsSync(p)) return [];
const stat = fs.lstatSync(p);
if (stat.isDirectory()) {
return fs.readdirSync(p)
.filter((f) => f.toLowerCase().endsWith(".pdf"))
.map((f) => path.join(p, f));
}
return p.toLowerCase().endsWith(".pdf") ? [p] : [];
}
(async () => {
const target = process.argv[2] || "examples";
const files = gather(target);
if (files.length === 0) { console.log("No PDFs found in", target); process.exit(0); }
console.log(`🔍 Scanning ${files.length} file(s) in: ${target}\n`);
for (const f of files) {
const buf = fs.readFileSync(f);
const name = path.basename(f);
let encrypted = false;
try { await PDFDocument.load(buf); }
catch (e) { encrypted = String(e.message).toLowerCase().includes("encrypted"); }
const signed = buf.includes("/Sig");
console.log(`${name}\n - Encrypted: ${encrypted}\n - Signed: ${signed}\n`);
}
})();