diff --git a/Rev1/node/public/img/kase.png b/Rev1/node/public/img/kase.png index 8065346..0843926 100644 Binary files a/Rev1/node/public/img/kase.png and b/Rev1/node/public/img/kase.png differ diff --git a/Rev1/node/public/upload/converted/AnkaSmartTeknolojiJLCPCB_Ithalat_deleted.pdf b/Rev1/node/public/upload/converted/AnkaSmartTeknolojiJLCPCB_Ithalat_deleted.pdf new file mode 100644 index 0000000..e612bf0 Binary files /dev/null and b/Rev1/node/public/upload/converted/AnkaSmartTeknolojiJLCPCB_Ithalat_deleted.pdf differ diff --git a/Rev1/node/public/upload/converted/Enpara.com Şirketim işlem dekontu.pdf b/Rev1/node/public/upload/converted/Enpara.com Şirketim işlem dekontu.pdf new file mode 100644 index 0000000..2ef21d5 Binary files /dev/null and b/Rev1/node/public/upload/converted/Enpara.com Şirketim işlem dekontu.pdf differ diff --git a/Rev1/node/public/upload/converted/zbe-dekont-1.pdf b/Rev1/node/public/upload/converted/zbe-dekont-1.pdf new file mode 100644 index 0000000..a636802 Binary files /dev/null and b/Rev1/node/public/upload/converted/zbe-dekont-1.pdf differ diff --git a/Rev1/node/routes/invoice_sign.js b/Rev1/node/routes/invoice_sign.js index 44107a7..4fa74e4 100644 --- a/Rev1/node/routes/invoice_sign.js +++ b/Rev1/node/routes/invoice_sign.js @@ -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;