printmaker
Version:
Generate PDF documents and from JavaScript objects
88 lines • 4.24 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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;
};
import fontkit from '@pdf-lib/fontkit';
import { PDFDocument, PDFHexString, PDFName } from 'pdf-lib';
import { embedFonts, parseFonts } from './fonts.js';
import { embedImages, parseImages } from './images.js';
import { asArray, asDate, asObject, asString, check, getFrom, optional, pickDefined, } from './types.js';
export function createDocument(def) {
return __awaiter(this, void 0, void 0, function* () {
const pdfDoc = yield PDFDocument.create();
pdfDoc.registerFontkit(fontkit);
const fonts = yield embedFonts(getFrom(def, 'fonts', parseFonts), pdfDoc);
const images = yield embedImages(getFrom(def, 'images', parseImages), pdfDoc);
setMetadata(getFrom(def, 'info', optional(parseInfo)), pdfDoc);
return { fonts, images, pdfDoc };
});
}
export function parseInfo(input) {
const obj = asObject(input);
const { title, subject, keywords, author, creationDate, creator, producer } = obj, custom = __rest(obj, ["title", "subject", "keywords", "author", "creationDate", "creator", "producer"]);
return pickDefined({
title: check(title, 'title', optional(asString)),
subject: check(subject, 'subject', optional(asString)),
keywords: check(keywords, 'keywords', optional(asStringArray)),
author: check(author, 'author', optional(asString)),
creationDate: check(creationDate, 'creationDate', optional(asDate)),
creator: check(creator, 'creator', optional(asString)),
producer: check(producer, 'producer', optional(asString)),
custom: parseCustomAttrs(custom),
});
}
function parseCustomAttrs(custom) {
if (custom == null || !Object.keys(custom).length)
return undefined;
Object.entries(asObject(custom)).forEach(([key, value]) => check(value, key, asString));
return custom;
}
function asStringArray(input) {
return asArray(input).map((el, idx) => check(el, `[${idx}]`, asString));
}
function setMetadata(info, doc) {
if (info === null || info === void 0 ? void 0 : info.title) {
doc.setTitle(info.title);
}
if (info === null || info === void 0 ? void 0 : info.subject) {
doc.setSubject(info.subject);
}
if (info === null || info === void 0 ? void 0 : info.keywords) {
doc.setKeywords(info.keywords);
}
if (info === null || info === void 0 ? void 0 : info.author) {
doc.setAuthor(info.author);
}
if (info === null || info === void 0 ? void 0 : info.creationDate) {
doc.setCreationDate(info.creationDate);
}
if (info === null || info === void 0 ? void 0 : info.creator) {
doc.setCreator(info.creator);
}
if (info === null || info === void 0 ? void 0 : info.producer) {
doc.setProducer(info.producer);
}
if (info === null || info === void 0 ? void 0 : info.custom) {
const dict = doc.getInfoDict();
for (const [key, value] of Object.entries(info.custom)) {
dict.set(PDFName.of(key), PDFHexString.fromText(value));
}
}
}
//# sourceMappingURL=document.js.map