Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Some code improvements #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 10 additions & 15 deletions src/convertHTMLToPDF.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
const puppeteer = require('puppeteer');

let convertHTMLToPDF = async (html, callback, options = null, puppeteerArgs=null, remoteContent=true) => {
async function convertHTMLToPDF(html, callback, options = { format: 'Letter' }, puppeteerArgs = {}, remoteContent = true) {
if (typeof html !== 'string') {
throw new Error(
'Invalid Argument: HTML expected as type of string and received a value of a different type. Check your request body and request headers.'
);
}
let browser;
if (puppeteerArgs) {
browser = await puppeteer.launch(puppeteerArgs);
} else {
browser = await puppeteer.launch();
}
};

const browser = await puppeteer.launch(puppeteerArgs);

const page = await browser.newPage();
if (!options) {
options = { format: 'Letter' };
}

page.setDefaultNavigationTimeout(0);

if (remoteContent === true) {
await page.goto(`data:text/html;base64,${Buffer.from(html).toString('base64')}`, {
waitUntil: 'networkidle0'
});
} else {
//page.setContent will be faster than page.goto if html is a static
await page.setContent(html);
}
};

await page.pdf(options).then(callback, function(error) {
await page.pdf(options).then(callback, function (error) {
console.log(error);
});

await browser.close();
};
}

module.exports = convertHTMLToPDF;