Skip to content

Commit

Permalink
#10 uses buffer instead of writing pdf to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
dangerdak committed Nov 27, 2017
1 parent b3a3496 commit 2dd0350
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions src/controllers/send.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
const path = require('path');
const fs = require('fs');
const nodemailer = require('nodemailer');
const PDFDocument = require('pdfkit');
require('env2')('config.env');

const { getForm } = require('../model/form_queries');
const { validateSendEmail } = require('./helpers/validate');

const generatePdf = (filePath, formData) => {
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream(filePath));

const generatePdf = (doc, formData) => {
Object.keys(formData).forEach((field) => {
doc.text(`${field}: `);
if (field.includes('_svg') && formData[field]) {
Expand All @@ -20,10 +15,8 @@ const generatePdf = (filePath, formData) => {
doc.text(formData[field]);
}
});
doc.end();
};


exports.get = (req, res) => {
res.render('send', {
errorMessages: req.flash('error'),
Expand All @@ -42,39 +35,47 @@ exports.post = (req, res) => {
res.redirect('send');
} else {
getForm(req.session.id).then((data) => {
const fileName = `form-${req.session.id}.pdf`;
const filePath = path.join(__dirname, '..', '..', 'assets');
generatePdf(path.join(filePath, fileName), data);
//nodemailer
let smtpConfig = {
host: process.env.MAILGUN_SMTP_SERVER,
auth: {
user: process.env.MAILGUN_SMTP_LOGIN,
pass: process.env.MAILGUN_SMTP_PASSWORD,
},
};
let transporter = nodemailer.createTransport(smtpConfig);
let message = {
from: process.env.FROM_EMAIL,
to: req.body.email,
subject: 'Form submission',
text: `Answers from ${req.session.user}`,
attachments: [
{
filename: fileName,
path: path.join(filePath, fileName),
const fileName = `form-${req.session.user}.pdf`;
// pdfkit
const doc = new PDFDocument();
let buffers = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', () => {
const pdfData = Buffer.concat(buffers);
// nodemailer
const smtpConfig = {
host: process.env.MAILGUN_SMTP_SERVER,
auth: {
user: process.env.MAILGUN_SMTP_LOGIN,
pass: process.env.MAILGUN_SMTP_PASSWORD,
},
],
};
transporter.sendMail(message, (err, info) => {
if (err) {
console.log('transporter', err);
} else {
console.log(info);
res.redirect('finish');
}
};
const transporter = nodemailer.createTransport(smtpConfig);
const message = {
from: process.env.FROM_EMAIL,
to: req.body.email,
subject: 'Form submission',
text: `Answers from ${req.session.user}`,
attachments: [
{
filename: fileName,
content: pdfData,
},
],
};
transporter.sendMail(message, (err, info) => {
if (err) {
console.log('transporter', err);
} else {
console.log(info);
res.redirect('finish');
}
});
// end nodemailer
});
//end nodemailer
generatePdf(doc, data);
doc.end();
// end pdfkit
}).catch((error) => {
console.log('general', error);
res.status(500).render('error', {
Expand Down

0 comments on commit 2dd0350

Please sign in to comment.