UNPKG

@mfgames-writing/weasyprint-format

Version:

A formatter plugin for mfgames-writing-format that creates PDF files.

115 lines 4.33 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.loadWeasyPrintFormatter = loadWeasyPrintFormatter; const child = __importStar(require("child_process")); const semver = __importStar(require("semver")); const WeasyPrintFormatter_1 = require("./WeasyPrintFormatter"); /* * This module is working around a number of limitations in WeasyPrint (0.40). * * 1. We can't inject the page number so all documents start at 1. * * To get around this, we generate each section with a reset page number as a * separate PDF and then combine the results suing pdftk. */ function loadWeasyPrintFormatter(args) { if (!checkWeasyPrint(args)) { return null; } if (!checkPdfTk(args)) { return null; } return new WeasyPrintFormatter_1.WeasyPrintFormatter(); } /** * Checks the minimum version of WeasyPrint for processing. */ function checkPdfTk(args) { // Get the version from WeasyPrint so we can parse it. const cmd = child.spawnSync("pdftk", ["--version"], { stdio: [0, "pipe", "pipe"], }); if (!cmd.stdout) { args.logger.error("Cannot find the `pdftk` executable"); return false; } // Old versions of WeasyPrint only used a two-part version which means // too old for us. const cmdOutput = cmd.stdout.toString().replace(/[\r\n]/g, " "); const cmdVersion = cmdOutput.replace(/^.*pdftk (\d+\.\d+).*$/, "$1"); args.logger.debug("Using PdfTK: " + cmdVersion); return !!cmdVersion; } /** * Checks the minimum version of WeasyPrint for processing. */ function checkWeasyPrint(args) { // Get the version from WeasyPrint so we can parse it. const cmd = child.spawnSync("weasyprint", ["--version"], { stdio: [0, "pipe", "pipe"], }); if (!cmd.stderr) { args.logger.error("Cannot find the `weasyprint` executable"); return false; } // The most recent version of WeasyPrint (50), writes to stdout (as opposed // to stderr) and only has a single number. WeasyPrint also includes // patches to their output, so this should handle all three dotted versions. const cmdOutput = cmd.stdout.toString().trim(); const cmdVersion = cmdOutput .replace(/^.*version ([\d.]+)$/, "$1") .replace(/^(\d+)$/, "$1.0") .replace(/^(\d+\.\d+)$/, "$1.0"); const version = semver.valid(cmdVersion); args.logger.debug("Using WeasyPrint: " + cmdVersion); if (!version) { args.logger.error("Cannot parse version from `weasyprint` (" + cmdVersion + "): " + cmdOutput); return false; } // Make sure we have our minimum version. if (semver.lt(version, "0.42.0")) { args.logger.error("mfgames-writing-weasyprint requires WeasyPrint 0.42.0 or later" + ", found " + cmdVersion); return false; } // We are good. return true; } //# sourceMappingURL=weasyprint.js.map