UNPKG

printmaker

Version:

Generate PDF documents and from JavaScript objects

106 lines 4.41 kB
import { renderGuide } from './guides.js'; import { createLinkAnnotation, createNamedDest } from './pdf-annotations.js'; export function renderPage(page, doc) { page.pdfPage = doc.pdfDoc.addPage([page.size.width, page.size.height]); renderFrame(page.content, page); page.header && renderFrame(page.header, page); page.footer && renderFrame(page.footer, page); } export function renderFrame(frame, page, base = null) { var _a, _b, _c, _d; const { width, height } = frame; const topLeft = { x: frame.x + ((_a = base === null || base === void 0 ? void 0 : base.x) !== null && _a !== void 0 ? _a : 0), y: frame.y + ((_b = base === null || base === void 0 ? void 0 : base.y) !== null && _b !== void 0 ? _b : 0) }; const bottomLeft = { x: topLeft.x, y: topLeft.y + height }; renderGuide(page, Object.assign(Object.assign({}, tr(bottomLeft, page)), { width, height }), frame.type); (_c = frame.objects) === null || _c === void 0 ? void 0 : _c.forEach((object) => { if (object.type === 'text') { renderText(object, page, bottomLeft); } if (object.type === 'anchor') { renderAnchor(object, page, topLeft); } if (object.type === 'link') { renderLink(object, page, bottomLeft); } if (object.type === 'rect') { renderRect(object, page, topLeft); } if (object.type === 'line') { renderLine(object, page, topLeft); } if (object.type === 'polyline') { renderPolyline(object, page, topLeft); } if (object.type === 'image') { renderImage(object, page, topLeft); } }); (_d = frame.children) === null || _d === void 0 ? void 0 : _d.forEach((frame) => { renderFrame(frame, page, topLeft); }); } function renderText(el, page, base) { const { x, y } = tr({ x: el.x + base.x, y: el.y + base.y }, page); const options = { x, y, size: el.fontSize, font: el.font }; if (el.color) options.color = el.color; page.pdfPage.drawText(el.text, options); } function renderAnchor(el, page, base) { const { x, y } = tr({ x: el.x + base.x, y: el.y + base.y }, page); createNamedDest(page.pdfPage, el.name, { x, y }); } function renderLink(el, page, base) { const { x, y } = tr({ x: el.x + base.x, y: el.y + base.y }, page); const { width, height, url } = el; createLinkAnnotation(page.pdfPage, { x, y, width, height }, url); } function renderRect(rect, page, base) { const { x, y } = tr({ x: rect.x + base.x, y: rect.y + base.y + rect.height }, page); const { width, height } = rect; const options = { x, y, width, height }; if ('strokeColor' in rect) options.borderColor = rect.strokeColor; if ('strokeWidth' in rect) options.borderWidth = rect.strokeWidth; if ('fillColor' in rect) options.color = rect.fillColor; page.pdfPage.drawRectangle(options); } function renderLine(line, page, base) { const options = { start: tr({ x: line.x1 + base.x, y: line.y1 + base.y }, page), end: tr({ x: line.x2 + base.x, y: line.y2 + base.y }, page), }; if ('strokeWidth' in line) options.thickness = line.strokeWidth; if ('strokeColor' in line) options.color = line.strokeColor; page.pdfPage.drawLine(options); } function renderPolyline(polyline, page, base) { const path = createSvgPath(polyline.points, polyline.closePath); const { x, y } = tr(base, page); const options = { x, y }; if ('strokeColor' in polyline) options.borderColor = polyline.strokeColor; if ('strokeWidth' in polyline) options.borderWidth = polyline.strokeWidth; if ('fillColor' in polyline) options.color = polyline.fillColor; page.pdfPage.drawSvgPath(path, options); } function renderImage(object, page, base) { const { x, y } = tr({ x: object.x + base.x, y: object.y + base.y + object.height }, page); const { width, height } = object; const options = { x, y, width, height }; page.pdfPage.drawImage(object.image, options); } function createSvgPath(points, closePath = false) { const z = closePath ? ' Z' : ''; return points.reduce((s, p) => (s ? `${s} L` : `M`) + `${p.x},${p.y}`, '') + z; } function tr(pos, page) { return { x: pos.x, y: page.size.height - pos.y }; } //# sourceMappingURL=page.js.map