Skip to content
Snippets Groups Projects
Commit 52a1720d authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

scale down the qrcode and insert link

parent 60b08df6
No related branches found
No related tags found
1 merge request!1Draft: Resolve "[Document Sealing] Implement PDF parser"
Pipeline #49113 passed with stages
in 49 seconds
......@@ -6,5 +6,6 @@ declare class PDFparser {
constructor(document: Buffer);
getPDFMeta: () => Promise<IgetMetaResponse>;
insertQrCode: (imgBytes: ArrayBuffer) => Promise<ArrayBuffer>;
private createPageLinkAnnotation;
}
export default PDFparser;
......@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const verify_pdf_1 = __importDefault(require("@ninja-labs/verify-pdf"));
const pdf_lib_1 = require("pdf-lib");
const pdfdataextract_1 = require("pdfdataextract");
const config_1 = require("./config");
const { PDFDocument } = require("pdf-lib");
......@@ -48,25 +49,35 @@ class PDFparser {
this.insertQrCode = (imgBytes) => __awaiter(this, void 0, void 0, function* () {
const pdfDoc = yield PDFDocument.load(this.document);
const img = yield pdfDoc.embedPng(imgBytes);
// const imagePage = pdfDoc.insertPage(0);
const scaled = img.scale(0.5);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const { width, height } = firstPage.getSize();
firstPage.drawImage(img, {
x: firstPage.getWidth() / 2 - img.width / 2,
y: firstPage.getHeight() / 2 - img.height / 2,
width: img.width,
height: img.height,
x: firstPage.getWidth() / 2 - scaled.width / 2,
y: firstPage.getHeight() / 2 - scaled.height / 2,
width: scaled.width,
height: scaled.height,
});
// firstPage.drawImage(img, {
// x: 0,
// y: 0,
// width: firstPage.getWidth(),
// height: firstPage.getHeight(),
// });
const link = this.createPageLinkAnnotation(firstPage, "https://pdf-lib.js.org/", {
imgXPos: firstPage.getWidth() / 2 - scaled.width / 2,
imgYPos: firstPage.getHeight() / 2 - scaled.height / 2,
imgWidth: scaled.width,
imagHeight: scaled.height,
});
firstPage.node.set(pdf_lib_1.PDFName.of("Annots"), pdfDoc.context.obj([link]));
const pdfBytes = yield pdfDoc.save();
return pdfBytes;
});
this.createPageLinkAnnotation = (page, uri, { imgXPos, imgYPos, imgWidth, imagHeight }) => page.doc.context.register(page.doc.context.obj({
Type: "Annot",
Subtype: "Link",
Rect: [imgXPos, imgYPos, imgXPos + imgWidth, imgYPos + imagHeight],
A: {
Type: "Action",
S: "URI",
URI: pdf_lib_1.PDFString.of(uri),
},
}));
this.document = document;
this.config = config_1.config;
}
......
import verifyPDF from "@ninja-labs/verify-pdf";
import { PDFName, PDFPage, PDFString } from "pdf-lib";
import { PdfData } from "pdfdataextract";
import { config } from "./config";
import { IgetMetaResponse } from "./types";
const { PDFDocument } = require("pdf-lib");
class PDFparser {
readonly document;
readonly config;
......@@ -44,31 +44,55 @@ class PDFparser {
insertQrCode = async (imgBytes: ArrayBuffer): Promise<ArrayBuffer> => {
const pdfDoc = await PDFDocument.load(this.document);
const img = await pdfDoc.embedPng(imgBytes);
// const imagePage = pdfDoc.insertPage(0);
const scaled = img.scale(0.5);
const pages = pdfDoc.getPages();
const firstPage = pages[0];
const { width, height } = firstPage.getSize();
const firstPage = pages[0];
firstPage.drawImage(img, {
x: firstPage.getWidth() / 2 - img.width / 2,
y: firstPage.getHeight() / 2 - img.height / 2,
width: img.width,
height: img.height,
x: firstPage.getWidth() / 2 - scaled.width / 2,
y: firstPage.getHeight() / 2 - scaled.height / 2,
width: scaled.width,
height: scaled.height,
});
// firstPage.drawImage(img, {
// x: 0,
// y: 0,
// width: firstPage.getWidth(),
// height: firstPage.getHeight(),
// });
const link = this.createPageLinkAnnotation(
firstPage,
"https://pdf-lib.js.org/",
{
imgXPos: firstPage.getWidth() / 2 - scaled.width / 2,
imgYPos: firstPage.getHeight() / 2 - scaled.height / 2,
imgWidth: scaled.width,
imagHeight: scaled.height,
}
);
firstPage.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link]));
const pdfBytes = await pdfDoc.save();
return pdfBytes;
};
private createPageLinkAnnotation = (
page: PDFPage,
uri: string,
{ imgXPos, imgYPos, imgWidth, imagHeight }
) =>
page.doc.context.register(
page.doc.context.obj({
Type: "Annot",
Subtype: "Link",
Rect: [imgXPos, imgYPos, imgXPos + imgWidth, imgYPos + imagHeight],
A: {
Type: "Action",
S: "URI",
URI: PDFString.of(uri),
},
})
);
}
export default PDFparser;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment