Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add option to wait for document render #36

Merged
merged 2 commits into from
Feb 4, 2022
Merged
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 src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ program
.option('--coverImage <src>', 'image for PDF cover. *.svg file not working!')
.option('--disableTOC', 'disable table of contents')
.option('--coverSub <subtitle>', 'subtitle for PDF cover')
.option('--waitForRender <timeout>', 'wait for document render')
.action((options: generatePDFOptions) => {
generatePDF(options)
.then(() => {
Expand Down
19 changes: 14 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface generatePDFOptions {
coverImage: string;
disableTOC: boolean;
coverSub: string;
waitForRender: number;
}

export async function generatePDF({
Expand All @@ -34,6 +35,7 @@ export async function generatePDF({
coverImage,
disableTOC,
coverSub,
waitForRender,
}: generatePDFOptions): Promise<void> {
const browser = await puppeteer.launch({ args: puppeteerArgs });
const page = await browser.newPage();
Expand All @@ -47,11 +49,18 @@ export async function generatePDF({
console.log(chalk.cyan(`Retrieving html from ${nextPageURL}`));
console.log();

// Go to the page specified by nextPageURL
await page.goto(`${nextPageURL}`, {
waitUntil: 'networkidle0',
timeout: 0,
});
if (waitForRender) {
await page.goto(`${nextPageURL}`);
console.log(chalk.green('Rendering...'));
await page.waitFor(waitForRender);
} else {
// Go to the page specified by nextPageURL
await page.goto(`${nextPageURL}`, {
waitUntil: 'networkidle0',
timeout: 0,
});
}

// Get the HTML string of the content section.
const html = await page.evaluate(
({ contentSelector }) => {
Expand Down