@projektemacher/vivliostyle-batch-cli 0.3.1 → 0.3.2

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/README.md CHANGED
@@ -79,6 +79,8 @@ vivliostyle-cli -i index.html -o output.pdf -- --size A4 --viewer-param pixelRat
79
79
  | `--pdfa <level>` | Label the output as PDF/A: `1a`, `1b`, `2a`, `2b`, `2u`, `3a`, `3b`, `3u`, `4`. Embeds the sRGB output intent unless overridden |
80
80
  | `--pdfx <version>` | Label the output as PDF/X: `X-4` or `X-1a:2003` (requires `--icc-profile`) |
81
81
  | `--icc-profile <path>` | ICC profile embedded as the PDF output intent (required for `--pdfx`) |
82
+ | `--no-validate` | Skip the built-in compliance checks run for `--pdfa`/`--pdfx` output |
83
+ | `--verapdf` | Additionally run the verapdf binary (must be on the PATH) against the output and fail the build on full ISO rule-set violations |
82
84
  | `--static <mapping>` | Map virtual path to local path: `/virtual/path:/local/path` (repeatable) |
83
85
  | `--no-scripts` | Do not map `<script src>` tags as static assets |
84
86
  | `--asset-base <urlBase=localBase>` | Map URLs starting with `urlBase` to files under `localBase` (repeatable) |
@@ -100,6 +102,9 @@ vivliostyle-cli -i index.html -o output.pdf -- --size A4 --viewer-param pixelRat
100
102
  ## Notes
101
103
 
102
104
  - **PDF metadata and standards labeling**: every build stamps a producer/creator identity (`vivliostyle-batch-cli`) and a document language. Title, author, subject, keywords and language resolve with this precedence: CLI flags → publication manifest (`.json` input) → the document itself (`<title>`, `<meta name="author">`/`dc.creator`, `<meta name="description">`, `<meta name="keywords">`, `html[lang]`) → language fallback `de`. The values land in both the PDF information dictionary and an XMP metadata stream kept consistent with it, plus the catalog `/Lang` entry. `--pdfa <level>` and `--pdfx <version>` additionally write the standards identification (XMP `pdfaid`/`pdfxid` properties, an OutputIntent with an embedded ICC profile and a file identifier). This is a **label, not a conversion**: the rendered content is neither converted nor checked, so conformance of the content is the author's responsibility — verify with veraPDF when it matters. Chrome's output carries transparency, so `--pdfa 2b` (or newer) is the realistic level; PDF/A-1b will usually fail validation.
105
+
106
+ Validation happens in two layers: every labeled build runs the built-in checks (a practical subset covering the PDF version of the labeled part, the XMP identification, the OutputIntent, the file identifier, embedded fonts and the absence of JavaScript; `--no-validate` skips them), and `--verapdf` additionally runs the [veraPDF](https://verapdf.org/) binary against the output for the full ISO rule set. `--verapdf` requires a veraPDF installation on the PATH (it needs a Java runtime) and makes the build fail if either the binary is missing or the output violates the labeled standard. For CI, the repository ships a reusable setup action (`.github/actions/setup-verapdf`) that installs veraPDF unattended.
107
+
103
108
  - The bundled `src/render/srgb.icc` is the ICC's `sRGB_v4_ICC_preference.icc`, redistributed unchanged under its license ("permission to use, copy and distribute this file for any purpose… provided that the file is not changed"), see <https://www.color.org/srgbprofiles.xalter>. Pass `--icc-profile` to embed a print provider's profile instead.
104
109
  - HTML input (`.html`/`.htm`) is auto-detected by file extension.
105
110
  - For HTML input, `<link href>` and `<script src>` tags are parsed and automatically mapped.
package/dist/index.d.ts CHANGED
@@ -113,6 +113,8 @@ type HtmlRewriteOptions = {
113
113
  dumpDir?: string | null;
114
114
  /** Let the browser load references without a local file from the web. */
115
115
  fetchMissing?: boolean;
116
+ /** Origins mapped via --asset-base; page-script fetches route to the server. */
117
+ mappedOrigins?: string[];
116
118
  /** Delay pagination until the page's own async work finished. */
117
119
  settle?: SettleGate | null;
118
120
  /** Origin that page-script requests are routed to. */
@@ -144,6 +146,13 @@ type RuntimeScriptOptions = {
144
146
  origin: string;
145
147
  documentBaseUrl: string;
146
148
  gate: SettleGate | null;
149
+ /**
150
+ * Origins mapped via --asset-base. Page scripts fetch data from these
151
+ * (the blog's sparql/graph components fetch their datasets from the site
152
+ * origin); routing them to the static server keeps builds deterministic
153
+ * instead of silently depending on the live site.
154
+ */
155
+ mappedOrigins?: string[];
147
156
  /**
148
157
  * Open every `<details>` element. Vivliostyle loads the page's scripts itself
149
158
  * and only re-dispatches `DOMContentLoaded` on the window, so scripts that
@@ -1330,14 +1330,33 @@ async function renderPdf(request) {
1330
1330
  polling: 500
1331
1331
  });
1332
1332
  if (request.waitForSettle === true) {
1333
+ // The gate image keeps the viewer's document processing pending until
1334
+ // the settle fires; only then does vivliostyle drain the deferred and
1335
+ // module scripts it queued (end-of-document execution) and dispatch
1336
+ // DOMContentLoaded. Pages that depend on those late globals flip
1337
+ // __vivReady after the gate settled, so hold print briefly for them.
1338
+ const POST_SETTLE_READY_GRACE_MS = 2000;
1339
+ let settledAt = -1;
1333
1340
  const handle = await page.waitForFunction(() => {
1334
1341
  const settleState = window.__vivSettle;
1335
1342
  // Pages with mocked/measured async work set __vivReady when painted;
1336
1343
  // Vivliostyle re-runs document scripts in the viewer context, so both
1337
1344
  // live on the same window the renderer polls here.
1338
- const ready = window.__vivReady === true;
1339
- if (ready)
1345
+ const readyState = window.__vivReady;
1346
+ if (readyState === true)
1340
1347
  return { reason: "ready", at: 0 };
1348
+ // An explicit not-ready beats a settled gate: quietness alone must
1349
+ // not print the pre-paint DOM (async tails emit no network signal).
1350
+ if (readyState === false) {
1351
+ if (settleState?.done === true) {
1352
+ if (settledAt < 0)
1353
+ settledAt = Date.now();
1354
+ if (Date.now() - settledAt >= POST_SETTLE_READY_GRACE_MS) {
1355
+ return { reason: settleState.reason ?? "settled", at: settleState.at ?? 0 };
1356
+ }
1357
+ }
1358
+ return false;
1359
+ }
1341
1360
  return settleState?.done === true ? { reason: settleState.reason ?? "settled", at: settleState.at ?? 0 } : false;
1342
1361
  }, { polling: 200, timeout: timeoutMs });
1343
1362
  const state = (await handle.jsonValue());
@@ -1385,7 +1404,8 @@ async function renderPdf(request) {
1385
1404
  if (request.verapdf === true && request.pdfLabel !== undefined) {
1386
1405
  const vera = runVeraPdf(request.output, request.pdfLabel);
1387
1406
  if (!vera.ran) {
1388
- log.warn(`[pdf] verapdf skipped: ${vera.reason}`);
1407
+ throw new Error(`--verapdf was requested, but veraPDF could not be run: ${vera.reason}. ` +
1408
+ "Install veraPDF (https://verapdf.org/, requires Java) or drop --verapdf.");
1389
1409
  }
1390
1410
  else if (vera.passed !== true) {
1391
1411
  throw new Error(`verapdf reports ${describeLabel(request.pdfLabel)} violations (exit ${vera.exitCode ?? "n/a"}):\n${vera.output}`);
@@ -1661,11 +1681,12 @@ function buildSettleGateImage(origin) {
1661
1681
  function buildRuntimeScript(options) {
1662
1682
  const { origin, documentBaseUrl, gate } = options;
1663
1683
  const expandDetails = options.expandDetails === true ? EXPAND_DETAILS_SNIPPET : "";
1684
+ const mappedOrigins = JSON.stringify(options.mappedOrigins ?? []);
1664
1685
  // Vivliostyle paginates incrementally, so collapsed content has to be opened
1665
1686
  // again while it lays the document out, not just once up front.
1666
1687
  const expandDetailsTick = options.expandDetails === true ? "expandDetails(document);" : "";
1667
1688
  return `(function(){
1668
- var ORIGIN=${JSON.stringify(origin)},BASE=${JSON.stringify(documentBaseUrl)};
1689
+ var ORIGIN=${JSON.stringify(origin)},BASE=${JSON.stringify(documentBaseUrl)},MAPPED_ORIGINS=${mappedOrigins};
1669
1690
  var QUIET=${gate?.quietMs ?? 400},DEADLINE=${gate?.deadlineMs ?? 10000},FRAME_GRACE=3000;
1670
1691
  var pending=0,frames=0,last=0,mutations=0,done=false,longtasks=0;
1671
1692
  // Force preserveDrawingBuffer on every WebGL context the page creates:
@@ -1706,7 +1727,13 @@ var state=window.__vivSettle={done:false,reason:null,at:0,pending:0,frames:0,mut
1706
1727
  function fix(u){
1707
1728
  if(typeof u!=="string"||u==="")return u;
1708
1729
  if(u.slice(0,2)==="//"||u.charAt(0)==="#"||u.charAt(0)==="?")return u;
1709
- if(/^[a-z][a-z0-9+.-]*:/i.test(u))return u;
1730
+ if(/^[a-z][a-z0-9+.-]*:/i.test(u)){
1731
+ try{
1732
+ var mu=new URL(u);
1733
+ if(MAPPED_ORIGINS.indexOf(mu.origin)>=0)return ORIGIN+mu.pathname+mu.search+mu.hash;
1734
+ }catch(e){}
1735
+ return u;
1736
+ }
1710
1737
  return u.charAt(0)==="/"?ORIGIN+u:BASE+u;
1711
1738
  }
1712
1739
 
@@ -1824,6 +1851,10 @@ last=performance.now();
1824
1851
  // Deterministic pages (charts, WebGL, OSD fixtures) set __vivReady when
1825
1852
  // painted instead of making the gate guess from network quietness. Shader
1826
1853
  // compile and tile decode finish after the last fetch, so readiness wins.
1854
+ // An explicit __vivReady=false holds the quiet path too: async tails (module
1855
+ // imports, wasm, deferred timers) emit no network signal, so quietness alone
1856
+ // would settle on the pre-paint DOM. The deadline above still bounds it.
1857
+ if(window.__vivReady===false){setTimeout(check,50);return;}
1827
1858
  if(window.__vivReady===true&&pending===0&&frames===0){signal("ready");return;}
1828
1859
  if(pending===0&&frames===0&&now-last>=QUIET){signal("quiet");return;}
1829
1860
  setTimeout(check,50);
@@ -2450,7 +2481,13 @@ function prepareInputHtmlForBuild(inputAbs, assetBases, staticMap, serverBaseUrl
2450
2481
  const origin = options.shimOrigin ?? serverBaseUrl;
2451
2482
  const documentBaseUrl = options.documentBaseUrl ?? `${origin}/`;
2452
2483
  // Only the PDF run expands `<details>`; the preview stays interactive.
2453
- injectRuntimeScriptInDom(document, { origin, documentBaseUrl, gate: options.settle ?? null, expandDetails: true });
2484
+ injectRuntimeScriptInDom(document, {
2485
+ origin,
2486
+ documentBaseUrl,
2487
+ gate: options.settle ?? null,
2488
+ mappedOrigins: options.mappedOrigins ?? [],
2489
+ expandDetails: true
2490
+ });
2454
2491
  log.trace("prepareInputHtmlForBuild: injected runtime script", { origin, documentBaseUrl, gate: options.settle });
2455
2492
  }
2456
2493
  if (!absChanged &&
@@ -3085,6 +3122,9 @@ async function execute(options, extraArgs = []) {
3085
3122
  if (options.iccProfile !== undefined && pdfLabel === undefined) {
3086
3123
  log.warn("[pdf] --icc-profile has no effect without --pdfa or --pdfx");
3087
3124
  }
3125
+ if (options.verapdf === true && pdfLabel === undefined) {
3126
+ log.warn("[pdf] --verapdf has no effect without --pdfa or --pdfx");
3127
+ }
3088
3128
  const iccProfile = resolveIccProfile(options, pdfLabel);
3089
3129
  const fetchMissing = options.fetchMissing === true || options.allowRemote === true;
3090
3130
  // The wait arms automatically on script-driven pages: without it, charts,
@@ -3098,6 +3138,10 @@ async function execute(options, extraArgs = []) {
3098
3138
  fetchMissing,
3099
3139
  dumpDir: options.dumpHtml ?? null,
3100
3140
  settle,
3141
+ // Page scripts fetch their data from the mapped origins (the blog's
3142
+ // sparql/graph components); route those to the static server so builds
3143
+ // stay deterministic instead of hitting the live site.
3144
+ mappedOrigins: [...new Set(assetBases.map((base) => new URL(base.urlBase).origin))],
3101
3145
  ...(typeof extraConfig.css === "string" ? { css: extraConfig.css } : {}),
3102
3146
  ...(typeof extraConfig.userStyle === "string" ? { userStyle: extraConfig.userStyle } : {})
3103
3147
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@projektemacher/vivliostyle-batch-cli",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "A vivliostyle-cli with more options for batch processing",
6
6
  "bin": {
package/src/render/pdf.ts CHANGED
@@ -458,14 +458,32 @@ export async function renderPdf(request: RenderPdfRequest): Promise<RenderPdfRes
458
458
  });
459
459
 
460
460
  if (request.waitForSettle === true) {
461
+ // The gate image keeps the viewer's document processing pending until
462
+ // the settle fires; only then does vivliostyle drain the deferred and
463
+ // module scripts it queued (end-of-document execution) and dispatch
464
+ // DOMContentLoaded. Pages that depend on those late globals flip
465
+ // __vivReady after the gate settled, so hold print briefly for them.
466
+ const POST_SETTLE_READY_GRACE_MS = 2000;
467
+ let settledAt = -1;
461
468
  const handle = await page.waitForFunction(
462
469
  () => {
463
470
  const settleState = (window as { __vivSettle?: { done?: boolean; reason?: string; at?: number } }).__vivSettle;
464
471
  // Pages with mocked/measured async work set __vivReady when painted;
465
472
  // Vivliostyle re-runs document scripts in the viewer context, so both
466
473
  // live on the same window the renderer polls here.
467
- const ready = (window as { __vivReady?: boolean }).__vivReady === true;
468
- if (ready) return { reason: "ready", at: 0 };
474
+ const readyState = (window as { __vivReady?: boolean }).__vivReady;
475
+ if (readyState === true) return { reason: "ready", at: 0 };
476
+ // An explicit not-ready beats a settled gate: quietness alone must
477
+ // not print the pre-paint DOM (async tails emit no network signal).
478
+ if (readyState === false) {
479
+ if (settleState?.done === true) {
480
+ if (settledAt < 0) settledAt = Date.now();
481
+ if (Date.now() - settledAt >= POST_SETTLE_READY_GRACE_MS) {
482
+ return { reason: settleState.reason ?? "settled", at: settleState.at ?? 0 };
483
+ }
484
+ }
485
+ return false;
486
+ }
469
487
  return settleState?.done === true ? { reason: settleState.reason ?? "settled", at: settleState.at ?? 0 } : false;
470
488
  },
471
489
  { polling: 200, timeout: timeoutMs }
@@ -520,7 +538,10 @@ export async function renderPdf(request: RenderPdfRequest): Promise<RenderPdfRes
520
538
  if (request.verapdf === true && request.pdfLabel !== undefined) {
521
539
  const vera = runVeraPdf(request.output, request.pdfLabel);
522
540
  if (!vera.ran) {
523
- log.warn(`[pdf] verapdf skipped: ${vera.reason}`);
541
+ throw new Error(
542
+ `--verapdf was requested, but veraPDF could not be run: ${vera.reason}. ` +
543
+ "Install veraPDF (https://verapdf.org/, requires Java) or drop --verapdf."
544
+ );
524
545
  } else if (vera.passed !== true) {
525
546
  throw new Error(`verapdf reports ${describeLabel(request.pdfLabel)} violations (exit ${vera.exitCode ?? "n/a"}):\n${vera.output}`);
526
547
  } else {
@@ -72,6 +72,8 @@ export type HtmlRewriteOptions = {
72
72
  dumpDir?: string | null;
73
73
  /** Let the browser load references without a local file from the web. */
74
74
  fetchMissing?: boolean;
75
+ /** Origins mapped via --asset-base; page-script fetches route to the server. */
76
+ mappedOrigins?: string[];
75
77
  /** Delay pagination until the page's own async work finished. */
76
78
  settle?: SettleGate | null;
77
79
  /** Origin that page-script requests are routed to. */
@@ -154,6 +156,13 @@ export type RuntimeScriptOptions = {
154
156
  origin: string;
155
157
  documentBaseUrl: string;
156
158
  gate: SettleGate | null;
159
+ /**
160
+ * Origins mapped via --asset-base. Page scripts fetch data from these
161
+ * (the blog's sparql/graph components fetch their datasets from the site
162
+ * origin); routing them to the static server keeps builds deterministic
163
+ * instead of silently depending on the live site.
164
+ */
165
+ mappedOrigins?: string[];
157
166
  /**
158
167
  * Open every `<details>` element. Vivliostyle loads the page's scripts itself
159
168
  * and only re-dispatches `DOMContentLoaded` on the window, so scripts that
@@ -207,12 +216,13 @@ export function buildSettleGateImage(origin: string): string {
207
216
  export function buildRuntimeScript(options: RuntimeScriptOptions): string {
208
217
  const { origin, documentBaseUrl, gate } = options;
209
218
  const expandDetails = options.expandDetails === true ? EXPAND_DETAILS_SNIPPET : "";
219
+ const mappedOrigins = JSON.stringify(options.mappedOrigins ?? []);
210
220
  // Vivliostyle paginates incrementally, so collapsed content has to be opened
211
221
  // again while it lays the document out, not just once up front.
212
222
  const expandDetailsTick = options.expandDetails === true ? "expandDetails(document);" : "";
213
223
 
214
224
  return `(function(){
215
- var ORIGIN=${JSON.stringify(origin)},BASE=${JSON.stringify(documentBaseUrl)};
225
+ var ORIGIN=${JSON.stringify(origin)},BASE=${JSON.stringify(documentBaseUrl)},MAPPED_ORIGINS=${mappedOrigins};
216
226
  var QUIET=${gate?.quietMs ?? 400},DEADLINE=${gate?.deadlineMs ?? 10000},FRAME_GRACE=3000;
217
227
  var pending=0,frames=0,last=0,mutations=0,done=false,longtasks=0;
218
228
  // Force preserveDrawingBuffer on every WebGL context the page creates:
@@ -253,7 +263,13 @@ var state=window.__vivSettle={done:false,reason:null,at:0,pending:0,frames:0,mut
253
263
  function fix(u){
254
264
  if(typeof u!=="string"||u==="")return u;
255
265
  if(u.slice(0,2)==="//"||u.charAt(0)==="#"||u.charAt(0)==="?")return u;
256
- if(/^[a-z][a-z0-9+.-]*:/i.test(u))return u;
266
+ if(/^[a-z][a-z0-9+.-]*:/i.test(u)){
267
+ try{
268
+ var mu=new URL(u);
269
+ if(MAPPED_ORIGINS.indexOf(mu.origin)>=0)return ORIGIN+mu.pathname+mu.search+mu.hash;
270
+ }catch(e){}
271
+ return u;
272
+ }
257
273
  return u.charAt(0)==="/"?ORIGIN+u:BASE+u;
258
274
  }
259
275
 
@@ -371,6 +387,10 @@ last=performance.now();
371
387
  // Deterministic pages (charts, WebGL, OSD fixtures) set __vivReady when
372
388
  // painted instead of making the gate guess from network quietness. Shader
373
389
  // compile and tile decode finish after the last fetch, so readiness wins.
390
+ // An explicit __vivReady=false holds the quiet path too: async tails (module
391
+ // imports, wasm, deferred timers) emit no network signal, so quietness alone
392
+ // would settle on the pre-paint DOM. The deadline above still bounds it.
393
+ if(window.__vivReady===false){setTimeout(check,50);return;}
374
394
  if(window.__vivReady===true&&pending===0&&frames===0){signal("ready");return;}
375
395
  if(pending===0&&frames===0&&now-last>=QUIET){signal("quiet");return;}
376
396
  setTimeout(check,50);
@@ -1079,7 +1099,13 @@ export function prepareInputHtmlForBuild(
1079
1099
  const origin = options.shimOrigin ?? serverBaseUrl;
1080
1100
  const documentBaseUrl = options.documentBaseUrl ?? `${origin}/`;
1081
1101
  // Only the PDF run expands `<details>`; the preview stays interactive.
1082
- injectRuntimeScriptInDom(document, { origin, documentBaseUrl, gate: options.settle ?? null, expandDetails: true });
1102
+ injectRuntimeScriptInDom(document, {
1103
+ origin,
1104
+ documentBaseUrl,
1105
+ gate: options.settle ?? null,
1106
+ mappedOrigins: options.mappedOrigins ?? [],
1107
+ expandDetails: true
1108
+ });
1083
1109
  log.trace("prepareInputHtmlForBuild: injected runtime script", { origin, documentBaseUrl, gate: options.settle });
1084
1110
  }
1085
1111
 
@@ -1887,6 +1913,9 @@ export async function execute(options: CliOptions, extraArgs: string[] = []): Pr
1887
1913
  if (options.iccProfile !== undefined && pdfLabel === undefined) {
1888
1914
  log.warn("[pdf] --icc-profile has no effect without --pdfa or --pdfx");
1889
1915
  }
1916
+ if (options.verapdf === true && pdfLabel === undefined) {
1917
+ log.warn("[pdf] --verapdf has no effect without --pdfa or --pdfx");
1918
+ }
1890
1919
  const iccProfile = resolveIccProfile(options, pdfLabel);
1891
1920
 
1892
1921
  const fetchMissing = options.fetchMissing === true || options.allowRemote === true;
@@ -1901,6 +1930,10 @@ export async function execute(options: CliOptions, extraArgs: string[] = []): Pr
1901
1930
  fetchMissing,
1902
1931
  dumpDir: options.dumpHtml ?? null,
1903
1932
  settle,
1933
+ // Page scripts fetch their data from the mapped origins (the blog's
1934
+ // sparql/graph components); route those to the static server so builds
1935
+ // stay deterministic instead of hitting the live site.
1936
+ mappedOrigins: [...new Set(assetBases.map((base) => new URL(base.urlBase).origin))],
1904
1937
  ...(typeof extraConfig.css === "string" ? { css: extraConfig.css } : {}),
1905
1938
  ...(typeof extraConfig.userStyle === "string" ? { userStyle: extraConfig.userStyle } : {})
1906
1939
  };