This commit is contained in:
selcuk committed 2022-07-11 18:28:42 +03:00
1 parent 9368202494
commit d0aafe80be
5 files changed
+30 -18

No files matched your search

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