-
Notifications
You must be signed in to change notification settings - Fork 18
Add option to pick PDF margins and format (A4/A3/etc.) #20
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
| }); | ||
|
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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to show the new options I think it would be helpful to see the options on every command which supports them.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! I think they currently only show up when you run 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 |
||
|
|
@@ -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`", | ||
| "./" | ||
| ) | ||
|
|
@@ -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(); | ||
|
|
||
There was a problem hiding this comment.
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:
There seems to be a bug if a decimal number is used 🙃
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!