UNPKG

@cocalc/project

Version:
62 lines (60 loc) 2.17 kB
"use strict"; /* Note that this doesn't actually use upstream nbconvert itself at all! - pdf: takes html, then uses headless chrome via chromium-browser or google-chrome, if installed to convert to pdf NOTE: Firefox does *not* support printing to pdf in headless mode according to https://stackoverflow.com/questions/48358556/firefox-headless-print-to-pdf-option */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const which_1 = __importDefault(require("which")); const path_1 = require("path"); // time google-chrome --headless --disable-gpu --no-sandbox --print-to-pdf=a.pdf --run-all-compositor-stages-before-draw --virtual-time-budget=10000 a.html const { execute_code } = require("@cocalc/backend/misc_node"); const async_utils_1 = require("@cocalc/util/async-utils"); async function htmlToPDF(path, timeout = 30) { const { dir, name } = (0, path_1.parse)(path); const outfile = (0, path_1.join)(dir, name + ".pdf"); const command = await getCommand(); const args = [ "--headless", "--disable-gpu", "--no-sandbox", `--print-to-pdf=${outfile}`, "--run-all-compositor-stages-before-draw", `--virtual-time-budget=${timeout * 1000}`, path, ]; const output = await (0, async_utils_1.callback_opts)(execute_code)({ command, args, err_on_exit: false, timeout, ulimit_timeout: true, bash: true, }); if (output.exit_code != 0) { throw Error(output.stderr); } return outfile; } exports.default = htmlToPDF; const COMMANDS = ["google-chrome", "chromium-browser"]; let cache = ""; async function getCommand() { if (cache) return cache; for (const cmd of COMMANDS) { try { await (0, which_1.default)(cmd); cache = cmd; return cmd; } catch (_err) { } } throw Error(`one of ${COMMANDS.join(" or ")} must be installed to convert to PDF`); } //# sourceMappingURL=html-to-pdf.js.map