first
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Verifikasi PDF hasil generate:
|
||||
* 1. membaca ukuran halaman fisik (MediaBox) memakai pdf-lib
|
||||
* 2. merender tiap halaman menjadi raster memakai mupdf
|
||||
* 3. memindai QR Code tiap label memakai jsQR
|
||||
*
|
||||
* Pemakaian: node scripts/verify-pdf.js output/labels-sample.pdf [dpi]
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const { PNG } = require('pngjs');
|
||||
const jsQR = require('jsqr');
|
||||
|
||||
const MM_PER_INCH = 25.4;
|
||||
|
||||
async function main() {
|
||||
const file = process.argv[2];
|
||||
const dpi = Number(process.argv[3] || 300);
|
||||
if (!file) {
|
||||
process.stderr.write('Pakai: node scripts/verify-pdf.js <file.pdf> [dpi]\n');
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
const mupdf = await import('mupdf');
|
||||
const { PDFDocument } = require('pdf-lib');
|
||||
const LabelLayout = require('../src/layout');
|
||||
|
||||
const bytes = fs.readFileSync(file);
|
||||
const pdf = await PDFDocument.load(bytes);
|
||||
const pageSizes = pdf.getPages().map(function (page) {
|
||||
const size = page.getSize();
|
||||
return {
|
||||
pt: [Number(size.width.toFixed(4)), Number(size.height.toFixed(4))],
|
||||
mm: [Number((size.width / 72 * MM_PER_INCH).toFixed(3)), Number((size.height / 72 * MM_PER_INCH).toFixed(3))]
|
||||
};
|
||||
});
|
||||
|
||||
const layout = LabelLayout.create();
|
||||
const perPage = layout.perPage;
|
||||
const ppm = dpi / MM_PER_INCH;
|
||||
|
||||
function decodeQrAt(png, boxMm) {
|
||||
const margin = 2;
|
||||
const x = Math.round((boxMm.xMm - margin) * ppm);
|
||||
const y = Math.round((boxMm.yMm - margin) * ppm);
|
||||
const w = Math.round((boxMm.widthMm + margin * 2) * ppm);
|
||||
const h = Math.round((boxMm.heightMm + margin * 2) * ppm);
|
||||
if (x < 0 || y < 0 || x + w > png.width || y + h > png.height) { return null; }
|
||||
const data = new Uint8ClampedArray(w * h * 4);
|
||||
for (let r = 0; r < h; r++) {
|
||||
for (let c = 0; c < w; c++) {
|
||||
const si = ((y + r) * png.width + (x + c)) * 4;
|
||||
const di = (r * w + c) * 4;
|
||||
data[di] = png.data[si];
|
||||
data[di + 1] = png.data[si + 1];
|
||||
data[di + 2] = png.data[si + 2];
|
||||
data[di + 3] = 255;
|
||||
}
|
||||
}
|
||||
const result = jsQR(data, w, h);
|
||||
return result ? result.data : null;
|
||||
}
|
||||
|
||||
const doc = mupdf.Document.openDocument(bytes, 'application/pdf');
|
||||
const matrix = mupdf.Matrix.scale(dpi / 72, dpi / 72);
|
||||
const pages = [];
|
||||
|
||||
for (let i = 0; i < doc.countPages(); i++) {
|
||||
const page = doc.loadPage(i);
|
||||
const pixmap = page.toPixmap(matrix, mupdf.ColorSpace.DeviceRGB, false, false);
|
||||
const raw = pixmap.asPNG();
|
||||
const png = PNG.sync.read(Buffer.from(
|
||||
raw.buffer ? raw.buffer : raw,
|
||||
raw.byteOffset || 0,
|
||||
raw.byteLength || raw.length
|
||||
));
|
||||
const structured = page.toStructuredText('preserve-whitespace');
|
||||
const decoded = [];
|
||||
for (let label = i * perPage; label < (i + 1) * perPage; label++) {
|
||||
const boxes = layout.contentBoxes(label);
|
||||
const value = decodeQrAt(png, boxes.qrBox);
|
||||
if (value) { decoded.push(value); }
|
||||
}
|
||||
pages.push({
|
||||
page: i + 1,
|
||||
raster: png.width + 'x' + png.height + ' px @' + dpi + ' dpi',
|
||||
qrTerbaca: decoded.length,
|
||||
contohPayload: decoded[0] || null,
|
||||
teks: String(structured.asText()).split('\n').filter(function (line) { return line.trim(); }).slice(0, 3)
|
||||
});
|
||||
pixmap.destroy();
|
||||
}
|
||||
|
||||
process.stdout.write(JSON.stringify({
|
||||
file: path.basename(file),
|
||||
jumlahHalaman: pdf.getPageCount(),
|
||||
pageSizes: pageSizes,
|
||||
pages: pages
|
||||
}, null, 2) + '\n');
|
||||
}
|
||||
|
||||
main().catch(function (err) {
|
||||
process.stderr.write('Error: ' + err.stack + '\n');
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user