Skip to content

Instantly share code, notes, and snippets.

@guaracyalima
Created August 6, 2018 23:49
Show Gist options
  • Save guaracyalima/c336d3be48b139eb2b08b8f0f815be8f to your computer and use it in GitHub Desktop.
Save guaracyalima/c336d3be48b139eb2b08b8f0f815be8f to your computer and use it in GitHub Desktop.
Coloca marca dagua e texto de dedscrição em documentos .pdff
const express = require('express');
const multer = require('multer');
const ejs = require('ejs');
const path = require('path');
var hummus = require('hummus');
const moment = require('moment')
moment.locale("pt-br")
const img = __dirname + '/public/img/carimbo.png';
const name = Date.now();
const hoje = moment().format('LLLL')
//assina o documento
function assina(file) {
}
// Set The Storage Engine
const storage = multer.diskStorage({
destination: './public/uploads/',
filename: function (req, file, cb) {
var xs = file.fieldname + Date.now() + path.extname(file.originalname)
cb(null, xs);
assina(xs)
console.log(xs);
setTimeout(() => {
var pdfWriter = hummus.createWriterToModify(__dirname + '/public/uploads/' + xs, {
modifiedFilePath: __dirname + '/public/output/' + name + '.pdf'
});
var pageModifier = new hummus.PDFPageModifier(pdfWriter, 0);
var ctx = pageModifier.startContext().getContext();
pageModifier.startContext().getContext().writeText(
'Revisado e aprovado pelo Departamento Jurídico em ' + hoje,
100, 20, {
font: pdfWriter.getFontForFile(__dirname + '/public/fonts/Couri.ttf'),
size: 8,
colorspace: 'gray',
color: 0x00
}
);
ctx.drawImage(450, 40, img, {
transformation: {
width: 100,
height: 100
}
});
pageModifier.endContext().writePage();
pdfWriter.end();
}, 999)
}
});
// Init Upload
const upload = multer({
storage: storage,
limits: {
fileSize: 1000000
},
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
}
}).single('myImage');
// Check File Type
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif|pdf/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype) {
return cb(null, true);
} else {
cb('Error: Erro ao assinar documento!');
}
}
const app = express();
// EJS
app.set('view engine', 'ejs');
app.use(express.static('./public'));
app.get('/', (req, res) => res.render('index'));
app.post('/upload', (req, res) => {
upload(req, res, (err) => {
if (err) {
res.render('index', {
msg: err
});
} else {
if (req.file == undefined) {
res.render('index', {
msg: 'Erro: Nenhum arquivo enviado!'
});
} else {
setTimeout(() => {
res.set({
'Content-Type': 'text/plain',
'Location': '/'
});
res.download(__dirname + `/public/output/${name}.pdf`);
}, 1200)
}
}
});
});
const port = 3002;
app.listen(port, () => console.log(`Signatures is running on por ${port}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment