|
|
|
@ -19,21 +19,25 @@ const orderReccentFiles = (dir) => {
|
|
|
|
|
|
|
|
|
|
|
|
const { PDFDocument } = require("pdf-lib");
|
|
|
|
const { PDFDocument } = require("pdf-lib");
|
|
|
|
|
|
|
|
|
|
|
|
const signPDF = async (sourceFile) => {
|
|
|
|
const signPDF = async (sourceFile, { x = 0, y = 0, sf = 1.0 }) => {
|
|
|
|
// Fetch PNG image
|
|
|
|
// Fetch PNG image
|
|
|
|
const pngUrl = signDir;
|
|
|
|
const pngUrl = signDir;
|
|
|
|
const pngSignBytes = fs.readFileSync(signDir);
|
|
|
|
const pngSignBytes = fs.readFileSync(signDir);
|
|
|
|
const pngStampBytes = fs.readFileSync(stampDir);
|
|
|
|
const pngStampBytes = fs.readFileSync(stampDir);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const pBytes = fs.readFileSync(rawDir + sourceFile);
|
|
|
|
const pBytes = fs.readFileSync(rawDir + sourceFile);
|
|
|
|
const pdfDoc = await PDFDocument.load(pBytes);
|
|
|
|
const pdfDoc = await PDFDocument.load(pBytes);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Embed the JPG image bytes and PNG image bytes
|
|
|
|
// Embed the JPG image bytes and PNG image bytes
|
|
|
|
const pngSign = await pdfDoc.embedPng(pngSignBytes);
|
|
|
|
const pngSign = await pdfDoc.embedPng(pngSignBytes);
|
|
|
|
const pngStamp = await pdfDoc.embedPng(pngStampBytes);
|
|
|
|
const pngStamp = await pdfDoc.embedPng(pngStampBytes);
|
|
|
|
|
|
|
|
|
|
|
|
// Get the width/height of the PNG image scaled down to 50% of its original size
|
|
|
|
// Get the width/height of the PNG image scaled down to 50% of its original size
|
|
|
|
const pngSignDims = pngSign.scale(0.1);
|
|
|
|
const pngSignDims = pngSign.scale(sf);
|
|
|
|
const pngStampDims = pngStamp.scale(0.1);
|
|
|
|
const pngStampDims = pngStamp.scale(sf);
|
|
|
|
|
|
|
|
|
|
|
|
// Add a blank page to the document
|
|
|
|
// Add a blank page to the document
|
|
|
|
const pages = pdfDoc.getPages();
|
|
|
|
const pages = pdfDoc.getPages();
|
|
|
|
@ -41,14 +45,14 @@ const signPDF = async (sourceFile) => {
|
|
|
|
// Draw the PNG image near the lower right corner of the JPG image
|
|
|
|
// Draw the PNG image near the lower right corner of the JPG image
|
|
|
|
page = pages[0];
|
|
|
|
page = pages[0];
|
|
|
|
page.drawImage(pngStamp, {
|
|
|
|
page.drawImage(pngStamp, {
|
|
|
|
x: page.getWidth() / 2 - pngStampDims.width / 2 + 75,
|
|
|
|
x: page.getWidth() / 2 - pngStampDims.width / 2 + x,
|
|
|
|
y: page.getHeight() / 2 - pngStampDims.height,
|
|
|
|
y: page.getHeight() / 2 - pngStampDims.height + y,
|
|
|
|
width: pngStampDims.width,
|
|
|
|
width: pngStampDims.width,
|
|
|
|
height: pngStampDims.height,
|
|
|
|
height: pngStampDims.height,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
page.drawImage(pngSign, {
|
|
|
|
page.drawImage(pngSign, {
|
|
|
|
x: page.getWidth() / 2 - pngSignDims.width / 2 + 75,
|
|
|
|
x: page.getWidth() / 2 - pngSignDims.width / 2 + x,
|
|
|
|
y: page.getHeight() / 2 - pngSignDims.height,
|
|
|
|
y: page.getHeight() / 2 - pngSignDims.height + y,
|
|
|
|
width: pngSignDims.width,
|
|
|
|
width: pngSignDims.width,
|
|
|
|
height: pngSignDims.height,
|
|
|
|
height: pngSignDims.height,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
@ -144,9 +148,17 @@ const fileStorageEngine = multer.diskStorage({
|
|
|
|
const upload = multer({ storage: fileStorageEngine });
|
|
|
|
const upload = multer({ storage: fileStorageEngine });
|
|
|
|
router.post("/upload_files", upload.array("files"), uploadFiles);
|
|
|
|
router.post("/upload_files", upload.array("files"), uploadFiles);
|
|
|
|
function uploadFiles(req, res) {
|
|
|
|
function uploadFiles(req, res) {
|
|
|
|
signPDF(req.files[0].originalname);
|
|
|
|
console.log(req.body);
|
|
|
|
// console.log(req.files);
|
|
|
|
try {
|
|
|
|
// console.log(req.files[0].toString("latin1"));
|
|
|
|
signPDF(req.files[0].originalname, {
|
|
|
|
|
|
|
|
x: parseFloat(req.body.x),
|
|
|
|
|
|
|
|
y: parseFloat(req.body.y),
|
|
|
|
|
|
|
|
sf: parseFloat(req.body.sf),
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
res.err("Failed to parse files");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
res.setHeader("content-type", "text/html; charset=utf-8");
|
|
|
|
res.setHeader("content-type", "text/html; charset=utf-8");
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
|
|
res.json({ message: "Successfully uploaded files" });
|
|
|
|
res.json({ message: "Successfully uploaded files" });
|
|
|
|
|