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
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ html_to_pdf.generatePdfs(file, options).then(output => {
console.log("PDF Buffer:-", output); // PDF Buffer:- [{url: "https://example.com", name: "example.pdf", buffer: <PDF buffer>}]
});
```


***Usage with Font:***
```js
const woffFont = fs.readFileSync(`__pathToFontFile__`, 'base64');
const pdfBuffer = await html_to_pdf.generatePdf(file, options, undefined, {
name: fontName,
data: woffFont,
});
```
172 changes: 92 additions & 80 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,93 +1,105 @@
const puppeteer = require('puppeteer');
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;
}
const puppeteer = require("puppeteer");
var Promise = require("bluebird");
const hb = require("handlebars");
const fs = require("fs");
const inlineCss = require("inline-css");
module.exports;

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;
async function generatePdf(file, options, callback, font) {
// we are using headless mode
let args = ["--no-sandbox", "--disable-setuid-sandbox"];
if (options.args) {
args = options.args;
delete options.args;
}

// 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
const browser = await puppeteer.launch({
args: args,
});
}
const page = await browser.newPage();
let embeddedFontStyle = "";
if (file.content) {
if (font) {
embeddedFontStyle = `
<style>
@font-face {
font-family: ${font.name};
src: url(data:application/font-woff;charset=utf-8;base64,${font.data}) format('woff');
font-weight: normal;
font-style: normal;
}
</style>
`;
}
data = await inlineCss(file.content, {url: "/"});
data = embeddedFontStyle + data;

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: ["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
});
// 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);
}
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