backup, going for multer

master
Selcuk Sari 4 years ago
parent 3357f89da3
commit 6215aa15a5

Binary file not shown.

Before

Width:  |  Height:  |  Size: 431 KiB

After

Width:  |  Height:  |  Size: 418 KiB

@ -6,11 +6,8 @@ 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 signDir = "./public/img/imza.png";
const stampDir = "./public/img/kase.png";
const orderReccentFiles = (dir) => {
return fs
@ -22,34 +19,45 @@ const orderReccentFiles = (dir) => {
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 pngSignBytes = fs.readFileSync(signDir);
const pngStampBytes = fs.readFileSync(stampDir);
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);
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 pngDims = pngImage.scale(0.1);
const pngSignDims = pngSign.scale(0.1);
const pngStampDims = pngStamp.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,
page.drawImage(pngStamp, {
x: page.getWidth() / 2 - pngStampDims.width / 2 + 75,
y: page.getHeight() / 2 - pngStampDims.height,
width: pngStampDims.width,
height: pngStampDims.height,
});
page.drawImage(pngSign, {
x: page.getWidth() / 2 - pngSignDims.width / 2 + 75,
y: page.getHeight() / 2 - pngSignDims.height,
width: pngSignDims.width,
height: pngSignDims.height,
});
// Serialize the PDFDocument to bytes (a Uint8Array)
const pdfBytes = await pdfDoc.save();
if (!fs.existsSync(lastFilesDir)) {
fs.mkdirSync(lastFilesDir, { recursive: true });
}
fs.writeFileSync(lastFilesDir + sourceFile, await pdfDoc.save());
return pdfBytes;
};
@ -58,7 +66,7 @@ const signPDF = async (sourceFile) => {
router.get("/", function (req, res) {
let lastFiles = [];
if (!fs.existsSync(lastFilesDir)) {
fs.mkdirSync(lastFilesDir);
fs.mkdirSync(lastFilesDir, { recursive: true });
}
// const dir = fs.opendirSync(lastFilesDir);
let filesOrdered = orderReccentFiles(lastFilesDir);
@ -90,7 +98,7 @@ router.post("/", function (req, res) {
// Upload path
const uploadPath = rawDir;
if (!fs.existsSync(uploadPath)) {
fs.mkdirSync(uploadPath);
fs.mkdirSync(uploadPath, { recursive: true });
}
// To save the file using mv() function
uploadedFile.mv(uploadPath + uploadedFile.name, function (err) {
@ -102,6 +110,7 @@ router.post("/", function (req, res) {
else {
signPDF(uploadedFile.name);
res.redirect(renderPageName);
fs.unlinkSync(uploadPath + uploadedFile.name);
}
});
} else res.send("No file uploaded !!");
@ -111,6 +120,10 @@ router.post("/", function (req, res) {
router.get("/download", function (req, res) {
const downloadFile = lastFilesDir + req.query.downloadFile;
console.log(downloadFile);
res.setHeader(
"Content-Disposition",
"attachment; filename=" + req.query.downloadFile
);
res.download(downloadFile, function (err) {});
});
module.exports = router;

Loading…
Cancel
Save