node-weasyprint
Version:
A NodeJS wrapper for [WeasyPrint](https://doc.courtbouillon.org/weasyprint/stable/index.html)
100 lines • 3.71 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const change_case_1 = require("change-case");
const child_process_1 = require("child_process");
const debug_1 = __importDefault(require("debug"));
const log = (0, debug_1.default)("weasyprint:log");
const err = (0, debug_1.default)("weasyprint:error");
function weasyprint(input, _a = {}) {
var _b;
var { command = "weasyprint", output, buffer } = _a, options = __rest(_a, ["command", "output", "buffer"]);
let child;
const isUrl = /^(https?|file):\/\//.test(input);
const args = [command];
Object.entries(options).forEach(function parseOption([camelCaseArg, value]) {
const arg = (0, change_case_1.paramCase)(camelCaseArg);
if (typeof value === "boolean") {
if (value) {
args.push(`--${arg}`);
}
}
else if (typeof value === "string") {
args.push(`--${arg}`, value);
}
else if (Array.isArray(value)) {
value.forEach((v) => parseOption([arg, v]));
}
});
args.push(isUrl ? input : "-"); // stdin if HTML given directly
args.push(output ? output : "-"); // stdout if no output file
log("Spawning %s with args %o...", args[0], args);
child = (0, child_process_1.spawn)(args[0], args.slice(1));
if (!child.stdout || !child.stderr) {
throw new Error("Failed to spawn process");
}
// write input to stdin if it isn't a url
if (!isUrl) {
(_b = child.stdin) === null || _b === void 0 ? void 0 : _b.end(input);
}
if (!output && !buffer) {
// If the user doesn't want the output to a file or as a buffer, let's just return the child
return child;
}
const buffers = [];
const errBuffers = [];
if (!output) {
child.stdout.on("data", (chunk) => {
buffers.push(Buffer.from(chunk));
});
}
child.stderr.on("data", (chunk) => {
errBuffers.push(Buffer.from(chunk));
err(chunk.toString("utf8").trim());
});
return new Promise((resolve, reject) => {
let settled = false;
child.on("error", (error) => {
if (!settled) {
settled = true;
if ("code" in error && error.code === "ENOENT") {
reject(new Error(`Command \`${command}\` not found. Is it installed?`));
}
else {
reject(error);
}
}
});
child.on("exit", () => {
if (!settled) {
settled = true;
if (child.exitCode) {
reject(new Error(Buffer.concat(errBuffers).toString("utf8")));
}
else {
if (!output) {
resolve(Buffer.concat(buffers));
}
else {
resolve();
}
}
}
});
});
}
exports.default = weasyprint;
//# sourceMappingURL=index.js.map