pd4ml 0.0.0-stage → 1.0.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/lib/runtime.js ADDED
@@ -0,0 +1,87 @@
1
+ 'use strict';
2
+ // The PD4ML runtime: version, layout engines, and the value helpers the wrappers share.
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+ const java = require('./java');
6
+ const { Bridge } = require('./bridge');
7
+ const { JavaError } = require('./errors');
8
+
9
+ const FORMATS = ['PDF', 'PDFA', 'PDFUA'];
10
+ const engines = new WeakMap(); // bridge -> whether the build has Constants.LayoutEngine
11
+
12
+ /** The PD4ML (Java) version. */
13
+ function version() {
14
+ return String(java.type('com.pd4ml.PD4ML').getVersion());
15
+ }
16
+
17
+ /**
18
+ * Whether the PD4ML build chooses between layout engines (Constants.LayoutEngine, from 4.2):
19
+ * earlier builds, the published 4.1.x among them, have one engine only.
20
+ */
21
+ function hasLayoutEngines() {
22
+ const bridge = Bridge.get();
23
+ if (!engines.has(bridge)) {
24
+ try {
25
+ bridge.getField('com.pd4ml.Constants$LayoutEngine', 'v42');
26
+ engines.set(bridge, true);
27
+ } catch (e) {
28
+ if (!(e instanceof JavaError)) throw e;
29
+ engines.set(bridge, false);
30
+ }
31
+ }
32
+ return engines.get(bridge);
33
+ }
34
+
35
+ /** A Java URL for a URL string, a WHATWG URL or a local path (relative: to the working directory). */
36
+ function toUrl(location) {
37
+ if (java.isJavaObject(location)) return location;
38
+ if (location instanceof URL) location = location.href;
39
+ const URLClass = java.type('java.net.URL');
40
+ if (/^[A-Za-z][A-Za-z0-9+.-]*:\/\/|^(file|jar|data):/i.test(location) && !/^[A-Za-z]:[\\/]/.test(location)) {
41
+ return new URLClass(location);
42
+ }
43
+ const File = java.type('java.io.File');
44
+ return new File(path.resolve(location)).toURI().toURL();
45
+ }
46
+
47
+ /**
48
+ * The PdfSpec for one of its keywords ('pdf/a-3b', case-insensitive); a PdfSpec object, null
49
+ * and the format names 'PDF', 'PDFA', 'PDFUA' as they are.
50
+ * @throws {RangeError} an unknown keyword (PdfSpec.getInstance alone falls back to PDF 1.7 for it)
51
+ */
52
+ function pdfSpec(spec) {
53
+ if (spec === null || spec === undefined || typeof spec !== 'string' || FORMATS.includes(spec.toUpperCase())) {
54
+ return spec === undefined ? null : spec;
55
+ }
56
+ const PdfSpec = java.type('com.pd4ml.PdfSpec');
57
+ const keywords = [...String(PdfSpec.listKeywords()).matchAll(/'([^']+)'/g)].map(m => m[1]);
58
+ if (!keywords.some(k => k.toLowerCase() === spec.toLowerCase())) {
59
+ throw new RangeError(`unknown PDF spec '${spec}'; one of: ${keywords.join(', ')}`);
60
+ }
61
+ return PdfSpec.getInstance(spec);
62
+ }
63
+
64
+ /** Delivers output: returned (no target), written to a path, an fd, or a stream (`write()`). */
65
+ function deliver(data, target) {
66
+ if (target === undefined || target === null) return data;
67
+ if (typeof target === 'string' || target instanceof URL) {
68
+ fs.writeFileSync(target, data);
69
+ } else if (typeof target === 'number') {
70
+ fs.writeSync(target, data);
71
+ } else if (typeof target.write === 'function') {
72
+ target.write(data);
73
+ } else {
74
+ throw new TypeError('target: a path, a file descriptor or a writable stream');
75
+ }
76
+ return undefined;
77
+ }
78
+
79
+ /** The bytes of a Buffer, typed array, ArrayBuffer or string (UTF-8). */
80
+ function bytes(v) {
81
+ if (Buffer.isBuffer(v)) return v;
82
+ if (v instanceof Uint8Array) return Buffer.from(v.buffer, v.byteOffset, v.byteLength);
83
+ if (v instanceof ArrayBuffer) return Buffer.from(v);
84
+ return Buffer.from(String(v), 'utf8');
85
+ }
86
+
87
+ module.exports = { version, hasLayoutEngines, toUrl, pdfSpec, deliver, bytes };
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
- {
2
- "name": "pd4ml",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
1
+ {
2
+ "name": "pd4ml",
3
+ "version": "1.0.0",
4
+ "description": "PD4ML for Node.js: HTML and CSS to PDF, PDF/A, PDF/UA, ZUGFeRD, DOCX, RTF and images with the full PD4ML API",
5
+ "keywords": [
6
+ "pdf",
7
+ "html",
8
+ "html-to-pdf",
9
+ "pdfa",
10
+ "pdfua",
11
+ "zugferd",
12
+ "factur-x",
13
+ "docx",
14
+ "rtf",
15
+ "pd4ml"
16
+ ],
17
+ "homepage": "https://github.com/zxfr/pd4ml-node",
18
+ "license": "UNLICENSED",
19
+ "author": "PD4ML <support@pd4ml.com>",
20
+ "main": "index.js",
21
+ "types": "index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./index.d.ts",
25
+ "import": "./index.mjs",
26
+ "require": "./index.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "bin": {
31
+ "pd4ml": "bin/pd4ml.js"
32
+ },
33
+ "files": [
34
+ "index.js",
35
+ "index.mjs",
36
+ "index.d.ts",
37
+ "lib/",
38
+ "bin/",
39
+ "docs/",
40
+ "README.md"
41
+ ],
42
+ "engines": {
43
+ "node": ">=18"
44
+ },
45
+ "scripts": {
46
+ "test": "node --test \"test/*.test.js\"",
47
+ "gen-types": "node tools/gen-types.js",
48
+ "build-bridge": "node tools/build-bridge.js"
49
+ }
50
+ }