@segnavia/render 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/color/binary.js +27 -0
- package/dist/color/black-point.js +121 -0
- package/dist/color/engine.js +18 -0
- package/dist/color/interpolation.js +74 -0
- package/dist/color/profile.js +98 -0
- package/dist/color/spaces.js +138 -0
- package/dist/color/tables.js +255 -0
- package/dist/color/types.js +21 -0
- package/dist/deck-pdf.js +152 -0
- package/dist/effects/adapter.js +336 -0
- package/dist/effects/content-replacement.js +35 -0
- package/dist/effects/function-evaluator.js +72 -0
- package/dist/effects/policy.js +10 -0
- package/dist/effects/rule-1.js +40 -0
- package/dist/effects/rule-c.js +354 -0
- package/dist/effects/structure.js +86 -0
- package/dist/effects/types.d.ts +15 -0
- package/dist/effects/types.js +4 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +9 -0
- package/dist/index.d.ts +18 -8
- package/dist/index.js +96 -76
- package/dist/page-paper.js +94 -0
- package/dist/pages-imposition.js +65 -0
- package/dist/pages-pdf.js +67 -0
- package/dist/pdf-compose.js +35 -0
- package/dist/pdf-geometry.js +27 -0
- package/dist/press/adapter.js +275 -0
- package/dist/press/affine.js +60 -0
- package/dist/press/calculator-ops.js +277 -0
- package/dist/press/calculator.js +130 -0
- package/dist/press/color-policy.js +31 -0
- package/dist/press/content.js +292 -0
- package/dist/press/pattern-bounds.js +275 -0
- package/dist/press/pattern-path.js +102 -0
- package/dist/press/pdf-objects.js +88 -0
- package/dist/press/policy.js +126 -0
- package/dist/press/report.js +89 -0
- package/dist/press/sample-shading.js +266 -0
- package/dist/press/types.d.ts +18 -0
- package/dist/press/visitor.js +163 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RenderRefusedError, RendererUnavailableError } from "./errors.js";
|
|
2
|
+
import { renderDeckPdf } from "./deck-pdf.js";
|
|
3
|
+
import { rewriteEffectsPdf } from "./effects/adapter.js";
|
|
4
|
+
import { assertPrintOptions, printCss, printPage, printViewport } from "./page-paper.js";
|
|
5
|
+
import { renderPagesPdf } from "./pages-pdf.js";
|
|
6
|
+
import { rewritePressPdf } from "./press/adapter.js";
|
|
7
|
+
import { Page, printProperties } from "@segnavia/format";
|
|
2
8
|
import { frameDocument, newNonce } from "@segnavia/frame/bridge";
|
|
3
9
|
import { printableDeck } from "@segnavia/frame/print-document";
|
|
4
10
|
import { PDFDocument } from "pdf-lib";
|
|
@@ -18,15 +24,9 @@ const NO_NETWORK_ARGS = [
|
|
|
18
24
|
"--proxy-bypass-list=<-loopback>",
|
|
19
25
|
"--force-webrtc-ip-handling-policy=disable_non_proxied_udp"
|
|
20
26
|
];
|
|
21
|
-
var RendererUnavailableError = class extends Error {
|
|
22
|
-
name = "RendererUnavailableError";
|
|
23
|
-
};
|
|
24
27
|
var RenderTimeoutError = class extends Error {
|
|
25
28
|
name = "RenderTimeoutError";
|
|
26
29
|
};
|
|
27
|
-
var RenderRefusedError = class extends Error {
|
|
28
|
-
name = "RenderRefusedError";
|
|
29
|
-
};
|
|
30
30
|
function fontFaces(fonts = []) {
|
|
31
31
|
return fonts.map((font) => `@font-face{font-family:${JSON.stringify(font.family)};src:url(data:font/${font.format === "truetype" ? "ttf" : font.format === "opentype" ? "otf" : font.format};base64,${Buffer.from(font.data).toString("base64")}) format(${JSON.stringify(font.format)});font-weight:${font.weight ?? "normal"};font-style:${font.style ?? "normal"};font-display:block}`).join("");
|
|
32
32
|
}
|
|
@@ -52,6 +52,21 @@ async function enforce(pdf, limits) {
|
|
|
52
52
|
}
|
|
53
53
|
return pdf;
|
|
54
54
|
}
|
|
55
|
+
async function finishPdf(pdf, options) {
|
|
56
|
+
if (options.limits?.maxBytes !== void 0 && pdf.byteLength > options.limits.maxBytes) throw new RenderRefusedError(`The PDF is ${pdf.byteLength} bytes; the limit is ${options.limits.maxBytes}.`);
|
|
57
|
+
let output = pdf;
|
|
58
|
+
if (options.effects !== false) {
|
|
59
|
+
const rewritten = await rewriteEffectsPdf(output);
|
|
60
|
+
output = rewritten.bytes;
|
|
61
|
+
options.effects?.report?.(rewritten.report);
|
|
62
|
+
}
|
|
63
|
+
if (options.press) {
|
|
64
|
+
const converted = await rewritePressPdf(output, options.press.profile);
|
|
65
|
+
output = converted.bytes;
|
|
66
|
+
options.press.report?.(converted.report);
|
|
67
|
+
}
|
|
68
|
+
return enforce(output, options.limits);
|
|
69
|
+
}
|
|
55
70
|
async function captureImage(tab, options, fullPage) {
|
|
56
71
|
const format = options.format ?? "png";
|
|
57
72
|
if (format !== "webp") return tab.screenshot({
|
|
@@ -171,8 +186,8 @@ async function openRenderer(options) {
|
|
|
171
186
|
release();
|
|
172
187
|
}
|
|
173
188
|
}
|
|
174
|
-
function pageDocument(page, options) {
|
|
175
|
-
const css = `${fontFaces(options.fonts)}${options.css ?? ""}`;
|
|
189
|
+
function pageDocument(page, options, printing = false) {
|
|
190
|
+
const css = `${page.print ? printing ? printCss(page.print) : printProperties(page.print) : ""}${fontFaces(options.fonts)}${options.css ?? ""}`;
|
|
176
191
|
return frameDocument(page.html, {
|
|
177
192
|
scripts: page.scripts,
|
|
178
193
|
nonce: newNonce(),
|
|
@@ -183,14 +198,8 @@ async function openRenderer(options) {
|
|
|
183
198
|
reportHeight: false
|
|
184
199
|
});
|
|
185
200
|
}
|
|
186
|
-
function pageViewport(page, width) {
|
|
187
|
-
if (page.print)
|
|
188
|
-
const mm = sheetMm(page.print);
|
|
189
|
-
return {
|
|
190
|
-
width: Math.round(mm.width * PX_PER_MM),
|
|
191
|
-
height: Math.round(mm.height * PX_PER_MM)
|
|
192
|
-
};
|
|
193
|
-
}
|
|
201
|
+
function pageViewport(page, width, printing = false) {
|
|
202
|
+
if (page.print) return printViewport(page.print, printing);
|
|
194
203
|
if (page.size === "fluid") return {
|
|
195
204
|
width: width ?? FLUID_WIDTH,
|
|
196
205
|
height: 800
|
|
@@ -201,43 +210,62 @@ async function openRenderer(options) {
|
|
|
201
210
|
};
|
|
202
211
|
}
|
|
203
212
|
const image = captureImage;
|
|
213
|
+
async function renderPagePdf(page, opts) {
|
|
214
|
+
if (page.print) assertPrintOptions(page.print, opts.preferCssPageSize);
|
|
215
|
+
const viewport = pageViewport(page, void 0, true);
|
|
216
|
+
return await withTab({
|
|
217
|
+
viewport,
|
|
218
|
+
scripts: page.scripts === "sandboxed",
|
|
219
|
+
html: pageDocument(page, opts, true),
|
|
220
|
+
waitForReady: page.scripts === "sandboxed" && opts.waitForPageReady !== false,
|
|
221
|
+
boundMs: opts.boundMs,
|
|
222
|
+
beforeOutput: opts.beforeOutput
|
|
223
|
+
}, async (tab) => {
|
|
224
|
+
if (page.print) return printPage(tab, page.print, opts.preferCssPageSize);
|
|
225
|
+
const fixedHeight = typeof page.size === "object" && page.size.height !== "auto" ? page.size.height : null;
|
|
226
|
+
const height = fixedHeight ?? await contentHeight(tab);
|
|
227
|
+
return tab.pdf({
|
|
228
|
+
width: `${viewport.width}px`,
|
|
229
|
+
height: `${height}px`,
|
|
230
|
+
printBackground: true,
|
|
231
|
+
pageRanges: fixedHeight ? void 0 : "1"
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
async function renderPiecePdf(page, geometry, css, opts) {
|
|
236
|
+
const html = frameDocument(page.html, {
|
|
237
|
+
scripts: page.scripts,
|
|
238
|
+
nonce: newNonce(),
|
|
239
|
+
dir: page.dir,
|
|
240
|
+
lang: page.lang,
|
|
241
|
+
css,
|
|
242
|
+
relayKeys: false,
|
|
243
|
+
reportHeight: false
|
|
244
|
+
});
|
|
245
|
+
return withTab({
|
|
246
|
+
viewport: geometry.viewport,
|
|
247
|
+
scripts: page.scripts === "sandboxed",
|
|
248
|
+
html,
|
|
249
|
+
waitForReady: page.scripts === "sandboxed" && opts.waitForPageReady !== false,
|
|
250
|
+
boundMs: opts.boundMs,
|
|
251
|
+
beforeOutput: opts.beforeOutput
|
|
252
|
+
}, (tab) => tab.pdf({
|
|
253
|
+
width: geometry.pdf.width,
|
|
254
|
+
height: geometry.pdf.height,
|
|
255
|
+
printBackground: true,
|
|
256
|
+
preferCSSPageSize: false,
|
|
257
|
+
pageRanges: "1",
|
|
258
|
+
margin: {
|
|
259
|
+
top: "0mm",
|
|
260
|
+
right: "0mm",
|
|
261
|
+
bottom: "0mm",
|
|
262
|
+
left: "0mm"
|
|
263
|
+
}
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
204
266
|
return {
|
|
205
267
|
async pagePdf(input, opts = {}) {
|
|
206
|
-
|
|
207
|
-
const viewport = pageViewport(page);
|
|
208
|
-
return enforce(await withTab({
|
|
209
|
-
viewport,
|
|
210
|
-
scripts: page.scripts === "sandboxed",
|
|
211
|
-
html: pageDocument(page, opts),
|
|
212
|
-
waitForReady: page.scripts === "sandboxed" && opts.waitForPageReady !== false,
|
|
213
|
-
boundMs: opts.boundMs,
|
|
214
|
-
beforeOutput: opts.beforeOutput
|
|
215
|
-
}, async (tab) => {
|
|
216
|
-
if (page.print) {
|
|
217
|
-
const mm = sheetMm(page.print);
|
|
218
|
-
const margin = page.print.margin;
|
|
219
|
-
return tab.pdf({
|
|
220
|
-
width: `${mm.width}mm`,
|
|
221
|
-
height: `${mm.height}mm`,
|
|
222
|
-
printBackground: true,
|
|
223
|
-
preferCSSPageSize: opts.preferCssPageSize ?? false,
|
|
224
|
-
margin: margin ? {
|
|
225
|
-
top: `${margin.top}mm`,
|
|
226
|
-
right: `${margin.right}mm`,
|
|
227
|
-
bottom: `${margin.bottom}mm`,
|
|
228
|
-
left: `${margin.left}mm`
|
|
229
|
-
} : void 0
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
const fixedHeight = typeof page.size === "object" && page.size.height !== "auto" ? page.size.height : null;
|
|
233
|
-
const height = fixedHeight ?? await contentHeight(tab);
|
|
234
|
-
return tab.pdf({
|
|
235
|
-
width: `${viewport.width}px`,
|
|
236
|
-
height: `${height}px`,
|
|
237
|
-
printBackground: true,
|
|
238
|
-
pageRanges: fixedHeight ? void 0 : "1"
|
|
239
|
-
});
|
|
240
|
-
}), opts.limits);
|
|
268
|
+
return finishPdf(await renderPagePdf(Page.parse(input), opts), opts);
|
|
241
269
|
},
|
|
242
270
|
async pageImage(input, opts = {}) {
|
|
243
271
|
const page = Page.parse(input);
|
|
@@ -256,31 +284,23 @@ async function openRenderer(options) {
|
|
|
256
284
|
return image(tab, opts, false);
|
|
257
285
|
});
|
|
258
286
|
},
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
287
|
+
pagesPdf(inputs, opts = {}) {
|
|
288
|
+
return renderPagesPdf(inputs, opts, {
|
|
289
|
+
pagePdf: renderPagePdf,
|
|
290
|
+
piecePdf: renderPiecePdf,
|
|
291
|
+
fontFaces: (options) => fontFaces(options.fonts),
|
|
292
|
+
finish: finishPdf
|
|
293
|
+
});
|
|
294
|
+
},
|
|
295
|
+
deckPdf(deck, opts = {}) {
|
|
296
|
+
return renderDeckPdf(deck, opts, {
|
|
297
|
+
renderPdf: (setup, pdf) => withTab({
|
|
298
|
+
...setup,
|
|
299
|
+
waitForReady: false
|
|
300
|
+
}, (tab) => tab.pdf(pdf)),
|
|
301
|
+
fontFaces: (options) => fontFaces(options.fonts),
|
|
302
|
+
finish: finishPdf
|
|
264
303
|
});
|
|
265
|
-
return enforce(await withTab({
|
|
266
|
-
viewport: opts.layout === "notes" ? pageViewport({
|
|
267
|
-
html: "",
|
|
268
|
-
size: "fluid",
|
|
269
|
-
scripts: "refuse",
|
|
270
|
-
print: opts.notesPaper ?? {
|
|
271
|
-
paper: "letter",
|
|
272
|
-
orientation: "portrait"
|
|
273
|
-
}
|
|
274
|
-
}) : deck.size,
|
|
275
|
-
scripts: deck.scripts === "sandboxed",
|
|
276
|
-
html,
|
|
277
|
-
waitForReady: false,
|
|
278
|
-
boundMs: opts.boundMs,
|
|
279
|
-
beforeOutput: opts.beforeOutput
|
|
280
|
-
}, (tab) => tab.pdf({
|
|
281
|
-
preferCSSPageSize: true,
|
|
282
|
-
printBackground: true
|
|
283
|
-
})), opts.limits);
|
|
284
304
|
},
|
|
285
305
|
async slideImage(deck, index, opts = {}) {
|
|
286
306
|
const slide = deck.slides[index];
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { RenderRefusedError } from "./errors.js";
|
|
2
|
+
import { pdfBox, pdfPoint, pt } from "./pdf-geometry.js";
|
|
3
|
+
import { PX_PER_MM, printBoxes, printPadding, printProperties, sheetMm } from "@segnavia/format";
|
|
4
|
+
import { PDFDocument, PDFName, rgb } from "pdf-lib";
|
|
5
|
+
//#region src/page-paper.ts
|
|
6
|
+
const usesPressLayout = (print) => print.bleed > 0 || print.marks;
|
|
7
|
+
function printCss(print) {
|
|
8
|
+
const properties = printProperties(print);
|
|
9
|
+
if (!usesPressLayout(print)) return properties;
|
|
10
|
+
const margin = print.margin ?? {
|
|
11
|
+
top: 0,
|
|
12
|
+
right: 0,
|
|
13
|
+
bottom: 0,
|
|
14
|
+
left: 0
|
|
15
|
+
};
|
|
16
|
+
return `${properties}${printPadding({
|
|
17
|
+
top: margin.top + print.bleed,
|
|
18
|
+
right: margin.right + print.bleed,
|
|
19
|
+
bottom: margin.bottom + print.bleed,
|
|
20
|
+
left: margin.left + print.bleed
|
|
21
|
+
})}`;
|
|
22
|
+
}
|
|
23
|
+
function printViewport(print, pressLayout) {
|
|
24
|
+
const size = pressLayout && usesPressLayout(print) ? printBoxes(print).bleedBox : sheetMm(print);
|
|
25
|
+
return {
|
|
26
|
+
width: Math.round(size.width * PX_PER_MM),
|
|
27
|
+
height: Math.round(size.height * PX_PER_MM)
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function assertPrintOptions(print, preferCssPageSize) {
|
|
31
|
+
if (usesPressLayout(print) && preferCssPageSize) throw new RenderRefusedError("A page with bleed or marks takes its size from its print setting. Remove preferCssPageSize, or remove the bleed and marks, to clear this refusal.");
|
|
32
|
+
}
|
|
33
|
+
async function addPressBoxes(pdf, print) {
|
|
34
|
+
const boxes = printBoxes(print);
|
|
35
|
+
const document = await PDFDocument.load(pdf);
|
|
36
|
+
const source = document.getPage(0);
|
|
37
|
+
const content = await document.embedPage(source);
|
|
38
|
+
const page = document.addPage([pt(boxes.media.width), pt(boxes.media.height)]);
|
|
39
|
+
const bleedBox = pdfBox(boxes.bleedBox, boxes.media.height);
|
|
40
|
+
const trimBox = pdfBox(boxes.trim, boxes.media.height);
|
|
41
|
+
page.drawPage(content, {
|
|
42
|
+
x: bleedBox.x,
|
|
43
|
+
y: bleedBox.y,
|
|
44
|
+
width: source.getWidth(),
|
|
45
|
+
height: source.getHeight()
|
|
46
|
+
});
|
|
47
|
+
document.removePage(0);
|
|
48
|
+
page.node.delete(PDFName.of("CropBox"));
|
|
49
|
+
page.setBleedBox(bleedBox.x, bleedBox.y, bleedBox.width, bleedBox.height);
|
|
50
|
+
page.setTrimBox(trimBox.x, trimBox.y, trimBox.width, trimBox.height);
|
|
51
|
+
for (const mark of boxes.marks) page.drawLine({
|
|
52
|
+
start: pdfPoint(mark.x1, mark.y1, boxes.media.height),
|
|
53
|
+
end: pdfPoint(mark.x2, mark.y2, boxes.media.height),
|
|
54
|
+
thickness: .25,
|
|
55
|
+
color: rgb(0, 0, 0)
|
|
56
|
+
});
|
|
57
|
+
return document.save();
|
|
58
|
+
}
|
|
59
|
+
async function printPage(tab, print, preferCssPageSize) {
|
|
60
|
+
if (!usesPressLayout(print)) {
|
|
61
|
+
const mm = sheetMm(print);
|
|
62
|
+
const margin = print.margin;
|
|
63
|
+
return tab.pdf({
|
|
64
|
+
width: `${mm.width}mm`,
|
|
65
|
+
height: `${mm.height}mm`,
|
|
66
|
+
printBackground: true,
|
|
67
|
+
preferCSSPageSize: preferCssPageSize ?? false,
|
|
68
|
+
margin: margin ? {
|
|
69
|
+
top: `${margin.top}mm`,
|
|
70
|
+
right: `${margin.right}mm`,
|
|
71
|
+
bottom: `${margin.bottom}mm`,
|
|
72
|
+
left: `${margin.left}mm`
|
|
73
|
+
} : void 0
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const boxes = printBoxes(print);
|
|
77
|
+
const pdf = await tab.pdf({
|
|
78
|
+
width: `${boxes.bleedBox.width}mm`,
|
|
79
|
+
height: `${boxes.bleedBox.height}mm`,
|
|
80
|
+
printBackground: true,
|
|
81
|
+
preferCSSPageSize: false,
|
|
82
|
+
margin: {
|
|
83
|
+
top: "0mm",
|
|
84
|
+
right: "0mm",
|
|
85
|
+
bottom: "0mm",
|
|
86
|
+
left: "0mm"
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const papers = (await PDFDocument.load(pdf)).getPageCount();
|
|
90
|
+
if (papers > 1) throw new RenderRefusedError(`A page with bleed or marks is one printed piece, but the content needs ${papers} papers. Shorten the content, or remove the bleed and marks.`);
|
|
91
|
+
return addPressBoxes(pdf, print);
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
export { assertPrintOptions, printCss, printPage, printViewport };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { pdfBox } from "./pdf-geometry.js";
|
|
2
|
+
import { fitPage, pagePlacementLayout } from "@segnavia/format";
|
|
3
|
+
//#region src/pages-imposition.ts
|
|
4
|
+
const geometryKey = (page, pageValue, placement) => pageValue.size === "fluid" ? JSON.stringify([
|
|
5
|
+
page,
|
|
6
|
+
placement.bleedBox.width,
|
|
7
|
+
placement.bleedBox.height,
|
|
8
|
+
placement.bleed.top,
|
|
9
|
+
placement.bleed.right,
|
|
10
|
+
placement.bleed.bottom,
|
|
11
|
+
placement.bleed.left
|
|
12
|
+
]) : String(page);
|
|
13
|
+
function groupPieces(pages, imposed, mode) {
|
|
14
|
+
const groups = /* @__PURE__ */ new Map();
|
|
15
|
+
for (const placement of imposed.placements) {
|
|
16
|
+
const page = pages[placement.page];
|
|
17
|
+
if (!page) throw new RangeError(`The placement names missing page ${placement.page}.`);
|
|
18
|
+
const fit = fitPage(page, placement, mode);
|
|
19
|
+
const layout = pagePlacementLayout(page.size, placement, fit);
|
|
20
|
+
const key = geometryKey(placement.page, page, placement);
|
|
21
|
+
const group = groups.get(key) ?? {
|
|
22
|
+
key,
|
|
23
|
+
page: placement.page,
|
|
24
|
+
placements: []
|
|
25
|
+
};
|
|
26
|
+
group.placements.push({
|
|
27
|
+
placement,
|
|
28
|
+
layout
|
|
29
|
+
});
|
|
30
|
+
groups.set(key, group);
|
|
31
|
+
}
|
|
32
|
+
return [...groups.values()];
|
|
33
|
+
}
|
|
34
|
+
function pieceGeometry(item) {
|
|
35
|
+
const { placement, layout } = item;
|
|
36
|
+
if (layout.kind === "fluid") return {
|
|
37
|
+
viewport: {
|
|
38
|
+
width: Math.round(layout.piece.width),
|
|
39
|
+
height: Math.round(layout.piece.height)
|
|
40
|
+
},
|
|
41
|
+
pdf: {
|
|
42
|
+
width: `${layout.paper.width}mm`,
|
|
43
|
+
height: `${layout.paper.height}mm`
|
|
44
|
+
},
|
|
45
|
+
padding: layout.inset ? placement.bleed : void 0
|
|
46
|
+
};
|
|
47
|
+
return {
|
|
48
|
+
viewport: {
|
|
49
|
+
width: layout.piece.width,
|
|
50
|
+
height: Math.round(layout.piece.height)
|
|
51
|
+
},
|
|
52
|
+
pdf: {
|
|
53
|
+
width: `${layout.piece.width}px`,
|
|
54
|
+
height: `${layout.piece.height}px`
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function placementPdfGeometry(placement, layout, paperHeight) {
|
|
59
|
+
return {
|
|
60
|
+
draw: pdfBox(layout.paper, paperHeight),
|
|
61
|
+
clip: pdfBox(placement.bleedBox, paperHeight)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
export { groupPieces, pieceGeometry, placementPdfGeometry };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { RenderRefusedError } from "./errors.js";
|
|
2
|
+
import { groupPieces, pieceGeometry } from "./pages-imposition.js";
|
|
3
|
+
import { composeImposedPdf } from "./pdf-compose.js";
|
|
4
|
+
import { Imposition, Page, impose, printPadding, printProperties } from "@segnavia/format";
|
|
5
|
+
import { PDFDocument } from "pdf-lib";
|
|
6
|
+
//#region src/pages-pdf.ts
|
|
7
|
+
async function joinPdfs(pdfs) {
|
|
8
|
+
const joined = await PDFDocument.create();
|
|
9
|
+
for (const bytes of pdfs) {
|
|
10
|
+
const source = await PDFDocument.load(bytes);
|
|
11
|
+
const pages = await joined.copyPages(source, source.getPageIndices());
|
|
12
|
+
for (const page of pages) joined.addPage(page);
|
|
13
|
+
}
|
|
14
|
+
return joined.save();
|
|
15
|
+
}
|
|
16
|
+
function namePageError(error, page) {
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
error.message = `Page at index ${page} could not be rendered: ${error.message}`;
|
|
19
|
+
throw error;
|
|
20
|
+
}
|
|
21
|
+
throw new Error(`Page at index ${page} could not be rendered: ${String(error)}`);
|
|
22
|
+
}
|
|
23
|
+
async function composedPdf(pages, imposition, opts, services) {
|
|
24
|
+
const imposed = impose(imposition, pages.length);
|
|
25
|
+
const groups = groupPieces(pages, imposed, imposition.fit);
|
|
26
|
+
const properties = printProperties(imposition, imposition.unit);
|
|
27
|
+
const hostCss = `${services.fontFaces(opts)}${opts.css ?? ""}`;
|
|
28
|
+
const rendered = await Promise.all(groups.map(async (group) => {
|
|
29
|
+
const page = pages[group.page];
|
|
30
|
+
const first = group.placements[0];
|
|
31
|
+
if (!page || !first) throw new Error(`The piece for page ${group.page} has no placement.`);
|
|
32
|
+
const geometry = pieceGeometry(first);
|
|
33
|
+
const css = `${properties}${geometry.padding ? printPadding(geometry.padding) : ""}${hostCss}`;
|
|
34
|
+
try {
|
|
35
|
+
return {
|
|
36
|
+
group,
|
|
37
|
+
bytes: await services.piecePdf(page, geometry, css, opts)
|
|
38
|
+
};
|
|
39
|
+
} catch (error) {
|
|
40
|
+
namePageError(error, group.page);
|
|
41
|
+
}
|
|
42
|
+
}));
|
|
43
|
+
return composeImposedPdf(imposed, rendered.map(({ group, bytes }) => ({
|
|
44
|
+
key: group.key,
|
|
45
|
+
name: `The rendered piece for page ${group.page}`,
|
|
46
|
+
bytes,
|
|
47
|
+
placements: group.placements
|
|
48
|
+
})));
|
|
49
|
+
}
|
|
50
|
+
async function renderPagesPdf(inputs, opts, services) {
|
|
51
|
+
if (inputs.length === 0) throw new RenderRefusedError("There is nothing to render. Pass at least one page to clear this refusal.");
|
|
52
|
+
const pages = inputs.map((input) => Page.parse(input));
|
|
53
|
+
if (!opts.imposition) {
|
|
54
|
+
const rendered = await Promise.all(pages.map((page) => services.pagePdf(page, {
|
|
55
|
+
...opts,
|
|
56
|
+
effects: false,
|
|
57
|
+
limits: void 0,
|
|
58
|
+
press: void 0
|
|
59
|
+
})));
|
|
60
|
+
return services.finish(await joinPdfs(rendered), opts);
|
|
61
|
+
}
|
|
62
|
+
const imposition = Imposition.parse(opts.imposition);
|
|
63
|
+
if (opts.preferCssPageSize) throw new RenderRefusedError("Pages in an imposition take their size from its paper. Remove preferCssPageSize, or remove the imposition, to clear this refusal.");
|
|
64
|
+
return services.finish(await composedPdf(pages, imposition, opts, services), opts);
|
|
65
|
+
}
|
|
66
|
+
//#endregion
|
|
67
|
+
export { renderPagesPdf };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { placementPdfGeometry } from "./pages-imposition.js";
|
|
2
|
+
import { MM_PER_PT } from "@segnavia/format";
|
|
3
|
+
import { PDFDocument, clip, endPath, popGraphicsState, pushGraphicsState, rectangle } from "pdf-lib";
|
|
4
|
+
//#region src/pdf-compose.ts
|
|
5
|
+
async function embedOnePage(output, bytes, name) {
|
|
6
|
+
const [embedded] = await output.embedPdf(bytes, [0]);
|
|
7
|
+
if (!embedded) throw new Error(`${name} has no PDF page.`);
|
|
8
|
+
return embedded;
|
|
9
|
+
}
|
|
10
|
+
function drawClippedPage(target, embedded, draw, clipBox) {
|
|
11
|
+
target.pushOperators(pushGraphicsState(), rectangle(clipBox.x, clipBox.y, clipBox.width, clipBox.height), clip(), endPath());
|
|
12
|
+
target.drawPage(embedded, draw);
|
|
13
|
+
target.pushOperators(popGraphicsState());
|
|
14
|
+
}
|
|
15
|
+
function drawImposedSide(output, imposed, embedded, pieces, paperIndex, side) {
|
|
16
|
+
const paper = output.addPage([imposed.paper.width / MM_PER_PT, imposed.paper.height / MM_PER_PT]);
|
|
17
|
+
for (const piece of pieces) {
|
|
18
|
+
const image = embedded.get(piece.key);
|
|
19
|
+
if (!image) throw new Error(`${piece.name} was not embedded.`);
|
|
20
|
+
for (const { placement, layout } of piece.placements) {
|
|
21
|
+
if (placement.paper !== paperIndex || placement.side !== side) continue;
|
|
22
|
+
const { draw, clip: clipBox } = placementPdfGeometry(placement, layout, imposed.paper.height);
|
|
23
|
+
drawClippedPage(paper, image, draw, clipBox);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async function composeImposedPdf(imposed, pieces) {
|
|
28
|
+
const output = await PDFDocument.create();
|
|
29
|
+
const embedded = /* @__PURE__ */ new Map();
|
|
30
|
+
for (const piece of pieces) embedded.set(piece.key, await embedOnePage(output, piece.bytes, piece.name));
|
|
31
|
+
for (let paperIndex = 0; paperIndex < imposed.papers; paperIndex += 1) for (const side of ["front", "back"].slice(0, imposed.sides)) drawImposedSide(output, imposed, embedded, pieces, paperIndex, side);
|
|
32
|
+
return output.save();
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { composeImposedPdf, drawClippedPage, embedOnePage };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MM_PER_PT, PX_PER_MM } from "@segnavia/format";
|
|
2
|
+
//#region src/pdf-geometry.ts
|
|
3
|
+
const pt = (millimeters) => millimeters / MM_PER_PT;
|
|
4
|
+
const pixelPt = (pixels) => pt(pixels / PX_PER_MM);
|
|
5
|
+
function pdfPoint(x, y, mediaHeight) {
|
|
6
|
+
return {
|
|
7
|
+
x: pt(x),
|
|
8
|
+
y: pt(mediaHeight - y)
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function pdfBox(box, mediaHeight) {
|
|
12
|
+
return {
|
|
13
|
+
...pdfPoint(box.x, box.y + box.height, mediaHeight),
|
|
14
|
+
width: pt(box.width),
|
|
15
|
+
height: pt(box.height)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function pdfPixelBox(box, mediaHeight) {
|
|
19
|
+
return {
|
|
20
|
+
x: pixelPt(box.x),
|
|
21
|
+
y: pixelPt(mediaHeight - box.y - box.height),
|
|
22
|
+
width: pixelPt(box.width),
|
|
23
|
+
height: pixelPt(box.height)
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { pdfBox, pdfPixelBox, pdfPoint, pixelPt, pt };
|