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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ npx docusaurus-pdf from-build-config
- Mandatory: `dirPath` which points to the build directory created with `docusaurus build`.
- Mandatory: `firstDocPagePath` is the URL path segment (without `baseUrl`) of your first docs page you whish to have included in the PDF.
- Optional: If you have a `baseUrl` configured in your `docusaurus.config.js` then pass this value as `baseUrl`.
- Optional: You can specify larger/smaller margins, e.g. `--margin "1cm 1.5cm 1cm 2cm"` (order top right bottom left, like the css margin field).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this but unfortunately this doesn't work for me:

   ~/Dow/tm/docusaurus-pdf/t/test-website     add-puppeteer-opts !1  node ../../bin/index.js from-build-config --margin "1cm 1.5cm 1cm 2cm"
Error: Was expecting exactly 4 margin specifiers, instead got 5. Margin specifier was "1cm 1.5cm 1cm 2cm".

There seems to be a bug if a decimal number is used 🙃

Copy link
Copy Markdown
Collaborator Author

@aloisklink aloisklink Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I didn't even think of people using 1.5 cm, even though it's something I always do with LaTeX. I think I'll probably use a library like https://github.com/jedmao/parse-css-sides, since there seems to be quite a lot of edge cases, instead of rolling my own parser.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @aloisklink! Any progress on this? Let me know if I can help you bringing this PR into production!

- Optional: You can change the size of the paper by using `--format`, e.g. `--format A3`
- Note: There is a optional parameter to set a custom filename. You can see further details using `npx docusaurus-pdf from-build --help`.

## Docker usage
Expand Down
113 changes: 71 additions & 42 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,55 @@ const {
generatePdfFromBuildWithConfig,
} = require("../lib");

function generatePdfOptions({ sandbox, margin, format, printBackground }) {
return {
puppeteerArgs: sandbox ? [] : ["--no-sandbox"],
puppeteerPdfOpts: { margin, format, printBackground },
};
}

function parseMargin(marginString) {
const matches = [...marginString.match(/\d+\w{0,2}/g)];
if (matches.length !== 4) {
throw Error(
`Was expecting exactly 4 margin specifiers, instead got ${matches.length}. Margin specifier was "${marginString}".`
);
}
const [top, right, bottom, left] = matches;
return { top, right, bottom, left };
}

program
.version(require("../package.json").version)
.name("docusaurus-pdf")
.usage("<initialDocsUrl> [filename]")
.description("Generate PDF from initial docs url")
.arguments("<initialDocsUrl> [filename]")
.action((initialDocsUrl, filename) => {
generatePdf(initialDocsUrl, filename)
.then(() => {
console.log(chalk.green("Finish generating PDF!"));
process.exit(0);
})
.catch((err) => {
console.error(chalk.red(err.stack));
process.exit(1);
});
Comment thread
maxarndt marked this conversation as resolved.
.description("Generate a PDF from a docusaurus website")
.option("--no-sandbox", "Start puppeteer with --no-sandbox flag")
.option(
"--margin <margin>",
"Set margins of the pdf document with units in order top, right, bottom, left (units px,in,mm,cm)",
parseMargin,
"25px 35px 25px 35px"
)
.option(
"--format <format>",
"Set the size of the page, e.g. (A4, Letter)",
"A4"
)
.option("--no-print-background", "Disable printing the page background");

program
.command("from-website <initialDocsUrl> [filename]", {
isDefault: true,
})
.description("Generate PDF from an already hosted website")
.action(async (initialDocsUrl, filename = "docusaurus.pdf") => {
await generatePdf(
initialDocsUrl,
filename,
generatePdfOptions(program.opts())
);
console.log(chalk.green("Finish generating PDF!"));
});

program
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to show the new options --margin and --format for the other commands as well?
If I run one of the following I don't see those options listed:

node bin/index.js from-website --help
node bin/index.js from-build --help
node bin/index.js from-build-config --help

I think it would be helpful to see the options on every command which supports them.

Copy link
Copy Markdown
Collaborator Author

@aloisklink aloisklink Jul 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

I think they currently only show up when you run node bin/index.js --help, since they're global options for all commands.

I'll see if I can add a message saying something like:

$ node bin/index.js from-website --help
Usage: docusaurus-pdf from-website [options] <initialDocsUrl> [filename]

Generate PDF from an already hosted website

Global options can be seen by running `docusaurus-pdf --help`

Options:
  -h, --help  display help for command

Expand All @@ -36,34 +69,25 @@ program
"Specify your file name.",
"docusaurus.pdf"
)
.option("--no-sandbox", "Start puppeteer with --no-sandbox flag")
.action((dirPath, firstDocPagePath, baseUrl, options) => {
const puppeteerArgs = options.sandbox ? [] : ["--no-sandbox"];
generatePdfFromBuildSources(
.action(async (dirPath, firstDocPagePath, baseUrl, { outputFile }) => {
await generatePdfFromBuildSources(
dirPath,
firstDocPagePath,
baseUrl,
options.outputFile,
puppeteerArgs
)
.then(() => {
console.log(chalk.green("Finish generating PDF!"));
process.exit(0);
})
.catch((err) => {
console.error(chalk.red(err.stack));
process.exit(1);
});
outputFile,
generatePdfOptions(program.opts())
);
console.log(chalk.green("Finish generating PDF!"));
});

program
.command("from-build-config [dirPath]")
.description(
"Generate PDF from a docusaurs build artifact, loading from a docusaurus.config.js file"
"Generate PDF from a docusaurus build artifact, loading from a docusaurus.config.js file"
)
.option(
"--site-dir <dir>",
"The full path for the docusuarus site directory, relative to the current workspace." +
"The full path for the docusaurus site directory, relative to the current workspace." +
" Equivalent to the siteDir in `npx docusaurus build`",
"./"
)
Expand All @@ -72,21 +96,26 @@ program
"Specify your file name",
"docusaurus.pdf"
)
.option("--no-sandbox", "Start puppeteer with --no-sandbox flag")
.action((dirPath = "build", { siteDir, outputFile, sandbox }) => {
const puppeteerArgs = sandbox ? [] : ["--no-sandbox"];
generatePdfFromBuildWithConfig(siteDir, dirPath, outputFile, puppeteerArgs)
.then(() => {
console.log(chalk.green("Finish generating PDF!"));
process.exit(0);
})
.catch((err) => {
console.error(chalk.red(err.stack));
process.exit(1);
});
.action(async (dirPath = "build", { siteDir, outputFile }) => {
await generatePdfFromBuildWithConfig(
siteDir,
dirPath,
outputFile,
generatePdfOptions(program.opts())
);
console.log(chalk.green("Finish generating PDF!"));
});

program.parse(process.argv);
async function main() {
try {
await program.parseAsync(process.argv);
} catch (error) {
console.error(chalk.red(error.stack));
process.exit(1);
}
}

main();

if (!process.argv.slice(2).length) {
program.outputHelp();
Expand Down
Loading