-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint-email-to-pdf-page.js
More file actions
46 lines (42 loc) · 1.6 KB
/
Copy pathprint-email-to-pdf-page.js
File metadata and controls
46 lines (42 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const _ = require('lodash');
const striptags = require('striptags');
const parseEmailBody = require('./parse-email-body');
const headerSize = 12;
const textSize = 8;
const maxHeight = 400;
const width = 144;
const embeddedImageRegEx = /\[image: (.*)\]/;
function printDefaultEmail(doc, email, maxWidth) {
const parsedEmail = parseEmailBody(email.bodies[0]);
console.log('parsed body is', JSON.stringify(parsedEmail, null, 2));
console.log('adding text body only');
// add the subject
doc.fontSize(headerSize)
.font('./fonts/MiriamLibre-Bold.ttf')
.text(email.subject, {
align: 'center'
});
// add the body and any embedded images by looking for [image: <filename>] sections
const textBody = striptags(parsedEmail.textBody);
_.forEach(textBody.split(/(\[image: .*\])/), (textSection) => {
const checkImageEmbed = textSection.match(embeddedImageRegEx);
if (checkImageEmbed) {
const imageName = checkImageEmbed[1];
const embeddedImage = _.find(parsedEmail.images, (imageInfo) => { return imageInfo.name == imageName });
doc.image(embeddedImage.body, {
align: 'center',
fit: [maxWidth, maxHeight],
});
} else {
doc.fontSize(textSize)
.font('./fonts/Abel-Regular.ttf')
.text(striptags(parsedEmail.textBody), {
align: 'left',
height: maxHeight,
});
}
});
};
module.exports = function printEmailToPdfPage(doc, email) {
printDefaultEmail(doc, email);
};