pdfdancer-client-typescript 1.0.1
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/.eslintrc.js +19 -0
- package/README.md +267 -0
- package/dist/__tests__/e2e/test-helpers.d.ts +32 -0
- package/dist/__tests__/e2e/test-helpers.d.ts.map +1 -0
- package/dist/__tests__/e2e/test-helpers.js +157 -0
- package/dist/__tests__/e2e/test-helpers.js.map +1 -0
- package/dist/client-v1.d.ts +169 -0
- package/dist/client-v1.d.ts.map +1 -0
- package/dist/client-v1.js +558 -0
- package/dist/client-v1.js.map +1 -0
- package/dist/exceptions.d.ts +43 -0
- package/dist/exceptions.d.ts.map +1 -0
- package/dist/exceptions.js +66 -0
- package/dist/exceptions.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +216 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +427 -0
- package/dist/models.js.map +1 -0
- package/dist/paragraph-builder.d.ts +67 -0
- package/dist/paragraph-builder.d.ts.map +1 -0
- package/dist/paragraph-builder.js +160 -0
- package/dist/paragraph-builder.js.map +1 -0
- package/example.ts +103 -0
- package/fixtures/DancingScript-Regular.ttf +0 -0
- package/fixtures/JetBrainsMono-Regular.ttf +0 -0
- package/fixtures/ObviouslyAwesome.pdf +0 -0
- package/fixtures/README.md +25 -0
- package/fixtures/basic-paths.pdf +0 -0
- package/fixtures/logo-80.png +0 -0
- package/fixtures/mixed-form-types.pdf +0 -0
- package/jest.config.js +26 -0
- package/package.json +38 -0
- package/scripts/release.js +91 -0
- package/scripts/test-release.js +59 -0
- package/src/__tests__/client-v1.test.ts +111 -0
- package/src/__tests__/e2e/form.test.ts +51 -0
- package/src/__tests__/e2e/image.test.ts +108 -0
- package/src/__tests__/e2e/line.test.ts +134 -0
- package/src/__tests__/e2e/page.test.ts +45 -0
- package/src/__tests__/e2e/paragraph.test.ts +210 -0
- package/src/__tests__/e2e/path.test.ts +72 -0
- package/src/__tests__/e2e/test-helpers.ts +132 -0
- package/src/client-v1.ts +673 -0
- package/src/exceptions.ts +67 -0
- package/src/index.ts +34 -0
- package/src/models.ts +476 -0
- package/src/paragraph-builder.ts +184 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ParagraphBuilder for the PDFDancer TypeScript client.
|
|
3
|
+
* Closely mirrors the Python ParagraphBuilder class with TypeScript conventions.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ValidationException } from './exceptions';
|
|
7
|
+
import { Paragraph, Font, Color, Position } from './models';
|
|
8
|
+
import type { ClientV1 } from './client-v1';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Builder class for constructing Paragraph objects with fluent interface.
|
|
12
|
+
* Mirrors the Python ParagraphBuilder class exactly.
|
|
13
|
+
*/
|
|
14
|
+
export class ParagraphBuilder {
|
|
15
|
+
private _paragraph: Paragraph;
|
|
16
|
+
private _lineSpacing: number = 1.2;
|
|
17
|
+
private _textColor: Color = new Color(0, 0, 0); // Black by default
|
|
18
|
+
private _text?: string;
|
|
19
|
+
private _font?: Font;
|
|
20
|
+
|
|
21
|
+
constructor(private _client: ClientV1) {
|
|
22
|
+
if (!_client) {
|
|
23
|
+
throw new ValidationException("Client cannot be null");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this._paragraph = new Paragraph();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Set the text content for the paragraph.
|
|
31
|
+
* Equivalent to fromString() methods in Python ParagraphBuilder.
|
|
32
|
+
*/
|
|
33
|
+
fromString(text: string, color?: Color): ParagraphBuilder {
|
|
34
|
+
if (text === null || text === undefined) {
|
|
35
|
+
throw new ValidationException("Text cannot be null");
|
|
36
|
+
}
|
|
37
|
+
if (!text.trim()) {
|
|
38
|
+
throw new ValidationException("Text cannot be empty");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this._text = text;
|
|
42
|
+
if (color) {
|
|
43
|
+
this._textColor = color;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Set the font for the paragraph using an existing Font object.
|
|
51
|
+
* Equivalent to withFont(Font) in Python ParagraphBuilder.
|
|
52
|
+
*/
|
|
53
|
+
withFont(font: Font): ParagraphBuilder {
|
|
54
|
+
if (!font) {
|
|
55
|
+
throw new ValidationException("Font cannot be null");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
this._font = font;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Set the font for the paragraph using a TTF file.
|
|
64
|
+
* Equivalent to withFont(File, double) in Python ParagraphBuilder.
|
|
65
|
+
*/
|
|
66
|
+
async withFontFile(ttfFile: Uint8Array | File, fontSize: number): Promise<ParagraphBuilder> {
|
|
67
|
+
if (!ttfFile) {
|
|
68
|
+
throw new ValidationException("TTF file cannot be null");
|
|
69
|
+
}
|
|
70
|
+
if (fontSize <= 0) {
|
|
71
|
+
throw new ValidationException(`Font size must be positive, got ${fontSize}`);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Register font and create Font object
|
|
75
|
+
this._font = await this._registerTtf(ttfFile, fontSize);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Set the line spacing for the paragraph.
|
|
81
|
+
* Equivalent to withLineSpacing() in Python ParagraphBuilder.
|
|
82
|
+
*/
|
|
83
|
+
withLineSpacing(spacing: number): ParagraphBuilder {
|
|
84
|
+
if (spacing <= 0) {
|
|
85
|
+
throw new ValidationException(`Line spacing must be positive, got ${spacing}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this._lineSpacing = spacing;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Set the text color for the paragraph.
|
|
94
|
+
* Equivalent to withColor() in Python ParagraphBuilder.
|
|
95
|
+
*/
|
|
96
|
+
withColor(color: Color): ParagraphBuilder {
|
|
97
|
+
if (!color) {
|
|
98
|
+
throw new ValidationException("Color cannot be null");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this._textColor = color;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Set the position for the paragraph.
|
|
107
|
+
* Equivalent to withPosition() in Python ParagraphBuilder.
|
|
108
|
+
*/
|
|
109
|
+
withPosition(position: Position): ParagraphBuilder {
|
|
110
|
+
if (!position) {
|
|
111
|
+
throw new ValidationException("Position cannot be null");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this._paragraph.setPosition(position);
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Build and return the final Paragraph object.
|
|
120
|
+
* Equivalent to build() in Python ParagraphBuilder.
|
|
121
|
+
*/
|
|
122
|
+
build(): Paragraph {
|
|
123
|
+
// Validate required fields
|
|
124
|
+
if (!this._text) {
|
|
125
|
+
throw new ValidationException("Text must be set before building paragraph");
|
|
126
|
+
}
|
|
127
|
+
if (!this._font) {
|
|
128
|
+
throw new ValidationException("Font must be set before building paragraph");
|
|
129
|
+
}
|
|
130
|
+
if (!this._paragraph.getPosition()) {
|
|
131
|
+
throw new ValidationException("Position must be set before building paragraph");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Set paragraph properties
|
|
135
|
+
this._paragraph.font = this._font;
|
|
136
|
+
this._paragraph.color = this._textColor;
|
|
137
|
+
this._paragraph.lineSpacing = this._lineSpacing;
|
|
138
|
+
|
|
139
|
+
// Process text into lines
|
|
140
|
+
this._paragraph.textLines = this._processTextLines(this._text);
|
|
141
|
+
|
|
142
|
+
return this._paragraph;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Register a TTF font with the client and return a Font object.
|
|
147
|
+
* Equivalent to registerTTF() private method in Python ParagraphBuilder.
|
|
148
|
+
*/
|
|
149
|
+
private async _registerTtf(ttfFile: Uint8Array | File, fontSize: number): Promise<Font> {
|
|
150
|
+
try {
|
|
151
|
+
const fontName = await this._client.registerFont(ttfFile);
|
|
152
|
+
return new Font(fontName, fontSize);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
throw new ValidationException(`Failed to register font file: ${error}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Process text into lines for the paragraph.
|
|
160
|
+
* This is a simplified version - the full implementation would handle
|
|
161
|
+
* word wrapping, line breaks, and other text formatting based on the font
|
|
162
|
+
* and paragraph width.
|
|
163
|
+
*/
|
|
164
|
+
private _processTextLines(text: string): string[] {
|
|
165
|
+
// Handle escaped newlines (\\n) as actual newlines
|
|
166
|
+
const processedText = text.replace(/\\\\n/g, '\n');
|
|
167
|
+
|
|
168
|
+
// Simple implementation - split on newlines
|
|
169
|
+
// In the full version, this would implement proper text layout
|
|
170
|
+
let lines = processedText.split('\n');
|
|
171
|
+
|
|
172
|
+
// Remove empty lines at the end but preserve intentional line breaks
|
|
173
|
+
while (lines.length > 0 && !lines[lines.length - 1].trim()) {
|
|
174
|
+
lines.pop();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Ensure at least one line
|
|
178
|
+
if (lines.length === 0) {
|
|
179
|
+
lines = [''];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return lines;
|
|
183
|
+
}
|
|
184
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2020", "DOM"],
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./src",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"declaration": true,
|
|
13
|
+
"declarationMap": true,
|
|
14
|
+
"sourceMap": true,
|
|
15
|
+
"moduleResolution": "node",
|
|
16
|
+
"resolveJsonModule": true
|
|
17
|
+
},
|
|
18
|
+
"include": ["src/**/*"],
|
|
19
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
20
|
+
}
|