Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
Don't track content of these folders
node_modules/
someOtherfoler/


# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
.idea/

# ignore all lockfiles
*-lock.json
*-lock.yaml
147 changes: 72 additions & 75 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,88 @@ var Promise = require('bluebird');
const hb = require('handlebars')
const inlineCss = require('inline-css')
module.exports

async function generatePdf(file, options, callback) {
// we are using headless mode
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
];
if(options.args) {
args = options.args;
delete options.args;
}
// we are using headless mode
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
];
if (options.args) {
args = options.args;
delete options.args;
}

const browser = await puppeteer.launch({
args: args
});
const page = await browser.newPage();
const browser = await puppeteer.launch({
args: args
});
const page = await browser.newPage();

if(file.content) {
data = await inlineCss(file.content, {url:"/"});
console.log("Compiling the template with handlebars")
// we have compile our code with handlebars
const template = hb.compile(data, { strict: true });
const result = template(data);
const html = result;
if (file.content) {
data = await inlineCss(file.content, {url: "/"});
// we have compiled our code with handlebars
const template = hb.compile(data, {strict: true});
const html = template(data);

// We set the page content as the generated html by handlebars
await page.setContent(html, {
waitUntil: 'networkidle0', // wait for page to load completely
});
} else {
await page.goto(file.url, {
waitUntil:[ 'load', 'networkidle0'], // wait for page to load completely
});
}
// We set the page content as the generated html by handlebars
await page.setContent(html, {
waitUntil: 'networkidle0', // wait for page to load completely
});
} else {
await page.goto(file.url, {
waitUntil: ['load', 'networkidle0'], // wait for page to load completely
});
}

return Promise.props(page.pdf(options))
.then(async function(data) {
await browser.close();
return Promise.props(page.pdf(options))
.then(async function (data) {
await browser.close();

return Buffer.from(Object.values(data));
}).asCallback(callback);
return Buffer.from(Object.values(data));
}).asCallback(callback);
}

async function generatePdfs(files, options, callback) {
// we are using headless mode
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
];
if(options.args) {
args = options.args;
delete options.args;
}
const browser = await puppeteer.launch({
args: args
});
let pdfs = [];
const page = await browser.newPage();
for(let file of files) {
if(file.content) {
data = await inlineCss(file.content, {url:"/"})
console.log("Compiling the template with handlebars")
// we have compile our code with handlebars
const template = hb.compile(data, { strict: true });
const result = template(data);
const html = result;
// We set the page content as the generated html by handlebars
await page.setContent(html, {
waitUntil: 'networkidle0', // wait for page to load completely
});
} else {
await page.goto(file.url, {
waitUntil: 'networkidle0', // wait for page to load completely
});
}
let pdfObj = JSON.parse(JSON.stringify(file));
delete pdfObj['content'];
pdfObj['buffer'] = Buffer.from(Object.values(await page.pdf(options)));
pdfs.push(pdfObj);
}
// we are using headless mode
let args = [
'--no-sandbox',
'--disable-setuid-sandbox',
];
if (options.args) {
args = options.args;
delete options.args;
}
const browser = await puppeteer.launch({
args: args
});
let pdfs = [];
const page = await browser.newPage();
for (let file of files) {
if (file.content) {
data = await inlineCss(file.content, {url: "/"})
// we have compiled our code with handlebars
const template = hb.compile(data, {strict: true});
const html = template(data);
// We set the page content as the generated html by handlebars
await page.setContent(html, {
waitUntil: 'networkidle0', // wait for page to load completely
});
} else {
await page.goto(file.url, {
waitUntil: 'networkidle0', // wait for page to load completely
});
}
let pdfObj = JSON.parse(JSON.stringify(file));
delete pdfObj['content'];
pdfObj['buffer'] = Buffer.from(Object.values(await page.pdf(options)));
pdfs.push(pdfObj);
}

return Promise.resolve(pdfs)
.then(async function(data) {
await browser.close();
return data;
}).asCallback(callback);
return Promise.resolve(pdfs)
.then(async function (data) {
await browser.close();
return data;
}).asCallback(callback);
}

module.exports.generatePdf = generatePdf;
Expand Down
Loading