UNPKG

vitpress-generate-pdf

Version:

Generate pdf from document

73 lines (72 loc) 3.21 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("./utils"); const chalk = require("chalk"); const program = require("commander"); const express = require("express"); const commander_options_1 = require("./commander-options"); program .name('mr-pdf') .option('--initialDocURLs <urls>', 'set urls to start generating PDF from', commander_options_1.commaSeparatedList) .option('--excludeURLs <urls>', 'urls to be excluded in PDF', commander_options_1.commaSeparatedList) .requiredOption('--contentSelector <selector>', 'used to find the part of main content') .requiredOption('--paginationSelector <selector>', 'used to find next url') .option('--excludeSelectors <selectors>', 'exclude selector ex: .nav', commander_options_1.commaSeparatedList) .option('--cssStyle <cssString>', 'css style to adjust PDF output ex: body{padding-top: 0;}') .option('--outputPDFFilename <filename>', 'name of output PDF file') .option('--pdfMargin <margin>', 'set margin around PDF file', commander_options_1.generatePuppeteerPDFMargin) .option('--pdfFormat <format>', 'pdf format ex: A3, A4...') .option('--coverTitle <title>', 'title for PDF cover') .option('--coverImage <src>', 'image for PDF cover. *.svg file not working!') .option('--disableTOC', 'disable table of contents') .option('--tocOnlyH1', 'disable table of contents level only includes h1') .option('--coverSub <subtitle>', 'subtitle for PDF cover') .option('--waitForRender <timeout>', 'wait for document render') .option('--headerTemplate <html>', 'html template for page header') .option('--footerTemplate <html>', 'html template for page footer') .option('--buildDirPath <path>', 'location of build directory') .option('--firstDocPath <path>', 'location of first doc path') .action(async (options) => { const app = express(); const httpServer = await app.listen(); const address = httpServer.address(); if (!address || !utils_1.isAddressInfo(address)) { httpServer.close(); throw new Error('Something went wrong spinning up the express webserver.'); } if (options.buildDirPath) { app.use('/', express.static(options.buildDirPath)); try { utils_1.generatePDF({ ...options, initialDocURLs: [ `http://127.0.0.1:${address.port}/${options.firstDocPath}`, ], }) .then(() => { console.log(chalk.green('Finish generating PDF!')); process.exit(0); }) .catch((err) => { console.error(chalk.red(err.stack)); process.exit(1); }); } catch (err) { httpServer.close(); } } else { utils_1.generatePDF(options) .then(() => { console.log(chalk.green('Finish generating PDF!')); process.exit(0); }) .catch((err) => { console.error(chalk.red(err.stack)); process.exit(1); }); } }); program.parse(process.argv);