You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.5 KiB
JavaScript

const express = require("express");
const router = express.Router();
const fs = require("fs");
const path = require("path");
const lastFilesDir = "./public/upload/converted/";
const rawDir = "./public/upload/raw/";
const renderPageName = "invoice_sign";
const getMostRecentFile = (dir) => {
const files = orderReccentFiles(dir);
return files.length ? files[0] : undefined;
};
const orderReccentFiles = (dir) => {
return fs
.readdirSync(dir)
.filter((file) => fs.lstatSync(path.join(dir, file)).isFile())
.map((file) => ({ file, mtime: fs.lstatSync(path.join(dir, file)).mtime }))
.sort((a, b) => b.mtime.getTime() - a.mtime.getTime());
};
const { PDFDocument } = require("pdf-lib");
const signDir = "./public/img/imza.png";
const signPDF = async (sourceFile) => {
// Fetch PNG image
const pngUrl = signDir;
const pngImageBytes = fs.readFileSync(signDir);
const pBytes = fs.readFileSync(rawDir + sourceFile);
const pdfDoc = await PDFDocument.load(pBytes);
// Embed the JPG image bytes and PNG image bytes
const pngImage = await pdfDoc.embedPng(pngImageBytes);
// Get the width/height of the PNG image scaled down to 50% of its original size
const pngDims = pngImage.scale(0.1);
// Add a blank page to the document
const pages = pdfDoc.getPages();
// Draw the PNG image near the lower right corner of the JPG image
page = pages[0];
page.drawImage(pngImage, {
x: page.getWidth() / 2 - pngDims.width / 2 + 75,
y: page.getHeight() / 2 - pngDims.height,
width: pngDims.width,
height: pngDims.height,
});
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save();
fs.writeFileSync(lastFilesDir + sourceFile, await pdfDoc.save());
return pdfBytes;
};
// load the page
router.get("/", function (req, res) {
let lastFiles = [];
if (!fs.existsSync(lastFilesDir)) {
fs.mkdirSync(lastFilesDir);
}
// const dir = fs.opendirSync(lastFilesDir);
let filesOrdered = orderReccentFiles(lastFilesDir);
if (filesOrdered.length > 5) {
for (let index = 5; index < filesOrdered.length; index++) {
filePath = lastFilesDir + filesOrdered[index].file;
fs.unlinkSync(filePath);
}
}
filesOrdered = orderReccentFiles(lastFilesDir);
for (file of filesOrdered) {
lastFiles.push({ type: "f", name: file.file });
}
// lastFiles.push({ type: "f", name: getMostRecentFile(lastFilesDir).file });
res.render(renderPageName, { lastFiles });
});
//Post the upload file
// For handling the upload request
router.post("/", function (req, res) {
// When a file has been uploaded
if (req.files && Object.keys(req.files).length !== 0) {
// Uploaded path
const uploadedFile = req.files.uploadFile;
// Logging uploading file
console.log(uploadedFile);
// Upload path
const uploadPath = rawDir;
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath);
}
// To save the file using mv() function
uploadedFile.mv(uploadPath + uploadedFile.name, function (err) {
if (err) {
console.log(err);
res.send("Failed !!");
}
// res.send("Successfully Uploaded !!");
else {
signPDF(uploadedFile.name);
res.redirect(renderPageName);
}
});
} else res.send("No file uploaded !!");
});
// To handle the download file request
router.get("/download", function (req, res) {
const downloadFile = lastFilesDir + req.query.downloadFile;
console.log(downloadFile);
res.download(downloadFile, function (err) {});
});
module.exports = router;