pdfdancer-client-typescript 1.0.4 → 1.0.6
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 +5 -5
- package/dist/__tests__/e2e/test-helpers.d.ts +2 -1
- package/dist/__tests__/e2e/test-helpers.d.ts.map +1 -1
- package/dist/__tests__/e2e/test-helpers.js +7 -3
- package/dist/__tests__/e2e/test-helpers.js.map +1 -1
- package/dist/client-v1.js +1 -1
- package/dist/client-v1.js.map +1 -1
- package/dist/client-v2.d.ts +129 -0
- package/dist/client-v2.d.ts.map +1 -0
- package/dist/client-v2.js +696 -0
- package/dist/client-v2.js.map +1 -0
- package/dist/image-builder.d.ts +13 -0
- package/dist/image-builder.d.ts.map +1 -0
- package/dist/image-builder.js +44 -0
- package/dist/image-builder.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +5 -10
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +3 -13
- package/dist/models.js.map +1 -1
- package/dist/paragraph-builder.d.ts +19 -10
- package/dist/paragraph-builder.d.ts.map +1 -1
- package/dist/paragraph-builder.js +65 -26
- package/dist/paragraph-builder.js.map +1 -1
- package/dist/pdfdancer_v1.d.ts +202 -0
- package/dist/pdfdancer_v1.d.ts.map +1 -0
- package/dist/pdfdancer_v1.js +702 -0
- package/dist/pdfdancer_v1.js.map +1 -0
- package/dist/types.d.ts +56 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +92 -0
- package/dist/types.js.map +1 -0
- package/package.json +1 -1
- package/scripts/release.js +1 -1
- package/src/__tests__/client-v1.test.ts +46 -87
- package/src/__tests__/e2e/acroform.test.ts +60 -57
- package/src/__tests__/e2e/form_x_object.test.ts +17 -16
- package/src/__tests__/e2e/image.test.ts +53 -56
- package/src/__tests__/e2e/line.test.ts +47 -48
- package/src/__tests__/e2e/page.test.ts +37 -36
- package/src/__tests__/e2e/paragraph.test.ts +107 -101
- package/src/__tests__/e2e/path.test.ts +67 -64
- package/src/__tests__/e2e/test-helpers.ts +71 -67
- package/src/__tests__/e2e/token_from_env.test.ts +35 -0
- package/src/image-builder.ts +52 -0
- package/src/index.ts +1 -1
- package/src/models.ts +5 -21
- package/src/paragraph-builder.ts +217 -162
- package/src/{client-v1.ts → pdfdancer_v1.ts} +248 -53
- package/src/types.ts +133 -0
- package/example.ts +0 -99
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* PDFDancer TypeScript Client V1
|
|
4
|
+
*
|
|
5
|
+
* A TypeScript client that provides session-based PDF manipulation operations with strict validation.
|
|
6
|
+
*/
|
|
7
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
8
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.PDFDancer = void 0;
|
|
12
|
+
const exceptions_1 = require("./exceptions");
|
|
13
|
+
const models_1 = require("./models");
|
|
14
|
+
const paragraph_builder_1 = require("./paragraph-builder");
|
|
15
|
+
const types_1 = require("./types");
|
|
16
|
+
const image_builder_1 = require("./image-builder");
|
|
17
|
+
const fs_1 = __importDefault(require("fs"));
|
|
18
|
+
class PageClient {
|
|
19
|
+
constructor(client, pageIndex) {
|
|
20
|
+
this.type = models_1.ObjectType.PAGE;
|
|
21
|
+
this._client = client;
|
|
22
|
+
this._pageIndex = pageIndex;
|
|
23
|
+
this.internalId = `PAGE-${this._pageIndex}`;
|
|
24
|
+
this.position = models_1.Position.atPage(this._pageIndex);
|
|
25
|
+
// Cast to the internal interface to get access
|
|
26
|
+
this._internals = this._client;
|
|
27
|
+
}
|
|
28
|
+
async selectPathsAt(x, y) {
|
|
29
|
+
return this._internals.toPathObjects(await this._internals.findPaths(models_1.Position.atPageCoordinates(this._pageIndex, x, y)));
|
|
30
|
+
}
|
|
31
|
+
async selectImages() {
|
|
32
|
+
return this._internals.toImageObjects(await this._internals._findImages(models_1.Position.atPage(this._pageIndex)));
|
|
33
|
+
}
|
|
34
|
+
async selectImagesAt(x, y) {
|
|
35
|
+
return this._internals.toImageObjects(await this._internals._findImages(models_1.Position.atPageCoordinates(this._pageIndex, x, y)));
|
|
36
|
+
}
|
|
37
|
+
async delete() {
|
|
38
|
+
return this._internals.deletePage(this.ref());
|
|
39
|
+
}
|
|
40
|
+
ref() {
|
|
41
|
+
return new models_1.ObjectRef(this.internalId, this.position, this.type);
|
|
42
|
+
}
|
|
43
|
+
// noinspection JSUnusedGlobalSymbols
|
|
44
|
+
async selectForms() {
|
|
45
|
+
return this._internals.toFormXObjects(await this._internals.findFormXObjects(models_1.Position.atPage(this._pageIndex)));
|
|
46
|
+
}
|
|
47
|
+
async selectFormsAt(x, y) {
|
|
48
|
+
return this._internals.toFormXObjects(await this._internals.findFormXObjects(models_1.Position.atPageCoordinates(this._pageIndex, x, y)));
|
|
49
|
+
}
|
|
50
|
+
async selectFormFields() {
|
|
51
|
+
return this._internals.toFormFields(await this._internals.findFormFields(models_1.Position.atPage(this._pageIndex)));
|
|
52
|
+
}
|
|
53
|
+
async selectFormFieldsAt(x, y) {
|
|
54
|
+
return this._internals.toFormFields(await this._internals.findFormFields(models_1.Position.atPageCoordinates(this._pageIndex, x, y)));
|
|
55
|
+
}
|
|
56
|
+
// noinspection JSUnusedGlobalSymbols
|
|
57
|
+
async selectFormFieldsByName(fieldName) {
|
|
58
|
+
let pos = models_1.Position.atPage(this._pageIndex);
|
|
59
|
+
pos.name = fieldName;
|
|
60
|
+
return this._internals.toFormFields(await this._internals.findFormFields(pos));
|
|
61
|
+
}
|
|
62
|
+
async selectParagraphs() {
|
|
63
|
+
return this._internals.toParagraphObjects(await this._internals.findParagraphs(models_1.Position.atPage(this._pageIndex)));
|
|
64
|
+
}
|
|
65
|
+
async selectParagraphsStartingWith(text) {
|
|
66
|
+
let pos = models_1.Position.atPage(this._pageIndex);
|
|
67
|
+
pos.textStartsWith = text;
|
|
68
|
+
return this._internals.toParagraphObjects(await this._internals.findParagraphs(pos));
|
|
69
|
+
}
|
|
70
|
+
async selectParagraphsMatching(pattern) {
|
|
71
|
+
let pos = models_1.Position.atPage(this._pageIndex);
|
|
72
|
+
pos.textPattern = pattern;
|
|
73
|
+
return this._internals.toParagraphObjects(await this._internals.findParagraphs(pos));
|
|
74
|
+
}
|
|
75
|
+
async selectParagraphsAt(x, y) {
|
|
76
|
+
return this._internals.toParagraphObjects(await this._internals.findParagraphs(models_1.Position.atPageCoordinates(this._pageIndex, x, y)));
|
|
77
|
+
}
|
|
78
|
+
async selectTextLinesStartingWith(text) {
|
|
79
|
+
let pos = models_1.Position.atPage(this._pageIndex);
|
|
80
|
+
pos.textStartsWith = text;
|
|
81
|
+
return this._internals.toTextLineObjects(await this._internals.findTextLines(pos));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new ParagraphBuilder for fluent paragraph construction.
|
|
85
|
+
*/
|
|
86
|
+
newParagraph() {
|
|
87
|
+
return new paragraph_builder_1.ParagraphBuilder(this._client, this.position.pageIndex);
|
|
88
|
+
}
|
|
89
|
+
async selectTextLines() {
|
|
90
|
+
return this._internals.toTextLineObjects(await this._internals.findTextLines(models_1.Position.atPage(this._pageIndex)));
|
|
91
|
+
}
|
|
92
|
+
// noinspection JSUnusedGlobalSymbols
|
|
93
|
+
async selectTextLinesMatching(pattern) {
|
|
94
|
+
let pos = models_1.Position.atPage(this._pageIndex);
|
|
95
|
+
pos.textPattern = pattern;
|
|
96
|
+
return this._internals.toTextLineObjects(await this._internals.findTextLines(pos));
|
|
97
|
+
}
|
|
98
|
+
// noinspection JSUnusedGlobalSymbols
|
|
99
|
+
async selectTextLinesAt(x, y) {
|
|
100
|
+
return this._internals.toTextLineObjects(await this._internals.findTextLines(models_1.Position.atPageCoordinates(this._pageIndex, x, y)));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// noinspection ExceptionCaughtLocallyJS,JSUnusedLocalSymbols
|
|
104
|
+
/**
|
|
105
|
+
* REST API client for interacting with the PDFDancer PDF manipulation service.
|
|
106
|
+
* This client provides a convenient TypeScript interface for performing PDF operations
|
|
107
|
+
* including session management, object searching, manipulation, and retrieval.
|
|
108
|
+
* Handles authentication, session lifecycle, and HTTP communication transparently.
|
|
109
|
+
*
|
|
110
|
+
*/
|
|
111
|
+
class PDFDancer {
|
|
112
|
+
/**
|
|
113
|
+
* Creates a new client with PDF data.
|
|
114
|
+
* This constructor initializes the client, uploads the PDF data to open
|
|
115
|
+
* a new session, and prepares the client for PDF manipulation operations.
|
|
116
|
+
*/
|
|
117
|
+
constructor(token, pdfData, baseUrl = "http://localhost:8080", readTimeout = 30000) {
|
|
118
|
+
if (!token || !token.trim()) {
|
|
119
|
+
throw new exceptions_1.ValidationException("Authentication token cannot be null or empty");
|
|
120
|
+
}
|
|
121
|
+
this._token = token.trim();
|
|
122
|
+
this._baseUrl = baseUrl.replace(/\/$/, ''); // Remove trailing slash
|
|
123
|
+
this._readTimeout = readTimeout;
|
|
124
|
+
// Process PDF data with validation
|
|
125
|
+
this._pdfBytes = this._processPdfData(pdfData);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Initialize the client by creating a session.
|
|
129
|
+
* Must be called after constructor before using the client.
|
|
130
|
+
*/
|
|
131
|
+
async init() {
|
|
132
|
+
this._sessionId = await this._createSession();
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
static async open(pdfData, token, baseUrl, timeout) {
|
|
136
|
+
const resolvedToken = token ?? process.env.PDFDANCER_TOKEN;
|
|
137
|
+
const resolvedBaseUrl = baseUrl ??
|
|
138
|
+
process.env.PDFDANCER_BASE_URL ??
|
|
139
|
+
"https://api.pdfdancer.com";
|
|
140
|
+
const resolvedTimeout = timeout ?? 30000;
|
|
141
|
+
if (!resolvedToken) {
|
|
142
|
+
throw new Error("Missing PDFDancer token (pass it explicitly or set PDFDANCER_TOKEN in environment).");
|
|
143
|
+
}
|
|
144
|
+
const client = new PDFDancer(resolvedToken, pdfData, resolvedBaseUrl, resolvedTimeout);
|
|
145
|
+
return await client.init();
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Process PDF data from various input types with strict validation.
|
|
149
|
+
*/
|
|
150
|
+
_processPdfData(pdfData) {
|
|
151
|
+
if (!pdfData) {
|
|
152
|
+
throw new exceptions_1.ValidationException("PDF data cannot be null");
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
if (pdfData instanceof Uint8Array) {
|
|
156
|
+
if (pdfData.length === 0) {
|
|
157
|
+
throw new exceptions_1.ValidationException("PDF data cannot be empty");
|
|
158
|
+
}
|
|
159
|
+
return pdfData;
|
|
160
|
+
}
|
|
161
|
+
else if (pdfData instanceof ArrayBuffer) {
|
|
162
|
+
const uint8Array = new Uint8Array(pdfData);
|
|
163
|
+
if (uint8Array.length === 0) {
|
|
164
|
+
throw new exceptions_1.ValidationException("PDF data cannot be empty");
|
|
165
|
+
}
|
|
166
|
+
return uint8Array;
|
|
167
|
+
}
|
|
168
|
+
else if (pdfData instanceof File) {
|
|
169
|
+
// Note: File reading will be handled asynchronously in the session creation
|
|
170
|
+
return new Uint8Array(); // Placeholder, will be replaced in _createSession
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
throw new exceptions_1.ValidationException(`Unsupported PDF data type: ${typeof pdfData}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
if (error instanceof exceptions_1.ValidationException) {
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
throw new exceptions_1.PdfDancerException(`Failed to process PDF data: ${error}`, error);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Extract meaningful error messages from API response.
|
|
185
|
+
* Parses JSON error responses with _embedded.errors structure.
|
|
186
|
+
*/
|
|
187
|
+
async _extractErrorMessage(response) {
|
|
188
|
+
if (!response) {
|
|
189
|
+
return "Unknown error";
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
const errorData = await response.json();
|
|
193
|
+
// Check for embedded errors structure
|
|
194
|
+
if (errorData._embedded?.errors) {
|
|
195
|
+
const errors = errorData._embedded.errors;
|
|
196
|
+
if (Array.isArray(errors)) {
|
|
197
|
+
const messages = errors
|
|
198
|
+
.filter(error => error.message)
|
|
199
|
+
.map(error => error.message);
|
|
200
|
+
if (messages.length > 0) {
|
|
201
|
+
return messages.join("; ");
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Check for top-level message
|
|
206
|
+
if (errorData.message) {
|
|
207
|
+
return errorData.message;
|
|
208
|
+
}
|
|
209
|
+
// Fallback to response text or status
|
|
210
|
+
return await response.text() || `HTTP ${response.status}`;
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
// If JSON parsing fails, return response text or status
|
|
214
|
+
try {
|
|
215
|
+
return await response.text() || `HTTP ${response.status}`;
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
return `HTTP ${response.status}`;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Creates a new PDF processing session by uploading the PDF data.
|
|
224
|
+
*/
|
|
225
|
+
async _createSession() {
|
|
226
|
+
try {
|
|
227
|
+
const formData = new FormData();
|
|
228
|
+
// Handle File objects by reading their content
|
|
229
|
+
if (this._pdfBytes instanceof File) {
|
|
230
|
+
formData.append('pdf', this._pdfBytes, 'document.pdf');
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
const blob = new Blob([this._pdfBytes.buffer], { type: 'application/pdf' });
|
|
234
|
+
formData.append('pdf', blob, 'document.pdf');
|
|
235
|
+
}
|
|
236
|
+
const response = await fetch(`${this._baseUrl}/session/create`, {
|
|
237
|
+
method: 'POST',
|
|
238
|
+
headers: {
|
|
239
|
+
'Authorization': `Bearer ${this._token}`
|
|
240
|
+
},
|
|
241
|
+
body: formData,
|
|
242
|
+
signal: this._readTimeout > 0 ? AbortSignal.timeout(this._readTimeout) : undefined
|
|
243
|
+
});
|
|
244
|
+
if (!response.ok) {
|
|
245
|
+
const errorMessage = await this._extractErrorMessage(response);
|
|
246
|
+
throw new exceptions_1.HttpClientException(`Failed to create session: ${errorMessage}`, response);
|
|
247
|
+
}
|
|
248
|
+
const sessionId = (await response.text()).trim();
|
|
249
|
+
if (!sessionId) {
|
|
250
|
+
throw new exceptions_1.SessionException("Server returned empty session ID");
|
|
251
|
+
}
|
|
252
|
+
return sessionId;
|
|
253
|
+
}
|
|
254
|
+
catch (error) {
|
|
255
|
+
if (error instanceof exceptions_1.HttpClientException || error instanceof exceptions_1.SessionException) {
|
|
256
|
+
throw error;
|
|
257
|
+
}
|
|
258
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
259
|
+
throw new exceptions_1.HttpClientException(`Failed to create session: ${errorMessage}`, undefined, error);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Make HTTP request with session headers and error handling.
|
|
264
|
+
*/
|
|
265
|
+
async _makeRequest(method, path, data, params) {
|
|
266
|
+
const url = new URL(`${this._baseUrl}${path}`);
|
|
267
|
+
if (params) {
|
|
268
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
269
|
+
url.searchParams.append(key, value);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
const headers = {
|
|
273
|
+
'Authorization': `Bearer ${this._token}`,
|
|
274
|
+
'X-Session-Id': this._sessionId,
|
|
275
|
+
'Content-Type': 'application/json'
|
|
276
|
+
};
|
|
277
|
+
try {
|
|
278
|
+
const response = await fetch(url.toString(), {
|
|
279
|
+
method,
|
|
280
|
+
headers,
|
|
281
|
+
body: data ? JSON.stringify(data) : undefined,
|
|
282
|
+
signal: this._readTimeout > 0 ? AbortSignal.timeout(this._readTimeout) : undefined
|
|
283
|
+
});
|
|
284
|
+
// Handle FontNotFoundException
|
|
285
|
+
if (response.status === 404) {
|
|
286
|
+
try {
|
|
287
|
+
const errorData = await response.json();
|
|
288
|
+
if (errorData.error === 'FontNotFoundException') {
|
|
289
|
+
throw new exceptions_1.FontNotFoundException(errorData.message || 'Font not found');
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
catch (e) {
|
|
293
|
+
if (e instanceof exceptions_1.FontNotFoundException) {
|
|
294
|
+
throw e;
|
|
295
|
+
}
|
|
296
|
+
// Continue with normal error handling if JSON parsing fails
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (!response.ok) {
|
|
300
|
+
const errorMessage = await this._extractErrorMessage(response);
|
|
301
|
+
throw new exceptions_1.HttpClientException(`API request failed: ${errorMessage}`, response);
|
|
302
|
+
}
|
|
303
|
+
return response;
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
if (error instanceof exceptions_1.FontNotFoundException || error instanceof exceptions_1.HttpClientException) {
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
310
|
+
throw new exceptions_1.HttpClientException(`API request failed: ${errorMessage}`, undefined, error);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// Search Operations
|
|
314
|
+
/**
|
|
315
|
+
* Searches for PDF objects matching the specified criteria.
|
|
316
|
+
* This method provides flexible search capabilities across all PDF content,
|
|
317
|
+
* allowing filtering by object type and position constraints.
|
|
318
|
+
*/
|
|
319
|
+
async find(objectType, position) {
|
|
320
|
+
const requestData = new models_1.FindRequest(objectType, position).toDict();
|
|
321
|
+
const response = await this._makeRequest('POST', '/pdf/find', requestData);
|
|
322
|
+
const objectsData = await response.json();
|
|
323
|
+
return objectsData.map((objData) => this._parseObjectRef(objData));
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Searches for paragraph objects at the specified position.
|
|
327
|
+
*/
|
|
328
|
+
async findParagraphs(position) {
|
|
329
|
+
return this.find(models_1.ObjectType.PARAGRAPH, position);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Searches for image objects at the specified position.
|
|
333
|
+
*/
|
|
334
|
+
async _findImages(position) {
|
|
335
|
+
return this.find(models_1.ObjectType.IMAGE, position);
|
|
336
|
+
}
|
|
337
|
+
async selectImages() {
|
|
338
|
+
return this.toImageObjects(await this.find(models_1.ObjectType.IMAGE));
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Searches for form X objects at the specified position.
|
|
342
|
+
*/
|
|
343
|
+
async findFormXObjects(position) {
|
|
344
|
+
return this.find(models_1.ObjectType.FORM_X_OBJECT, position);
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Searches for vector path objects at the specified position.
|
|
348
|
+
*/
|
|
349
|
+
async findPaths(position) {
|
|
350
|
+
return this.find(models_1.ObjectType.PATH, position);
|
|
351
|
+
}
|
|
352
|
+
async selectPaths() {
|
|
353
|
+
return this.toPathObjects(await this.find(models_1.ObjectType.PATH));
|
|
354
|
+
}
|
|
355
|
+
async selectForms() {
|
|
356
|
+
return this.toFormXObjects(await this.find(models_1.ObjectType.FORM_X_OBJECT));
|
|
357
|
+
}
|
|
358
|
+
async selectFormFields() {
|
|
359
|
+
return this.toFormFields(await this.findFormFields());
|
|
360
|
+
}
|
|
361
|
+
async selectFieldsByName(fieldName) {
|
|
362
|
+
return this.toFormFields(await this.findFormFields(models_1.Position.byName(fieldName)));
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Searches for text line objects at the specified position.
|
|
366
|
+
*/
|
|
367
|
+
async findTextLines(position) {
|
|
368
|
+
return this.find(models_1.ObjectType.TEXT_LINE, position);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Searches for form fields at the specified position.
|
|
372
|
+
* Returns FormFieldRef objects with name and value properties.
|
|
373
|
+
*/
|
|
374
|
+
async findFormFields(position) {
|
|
375
|
+
const requestData = new models_1.FindRequest(models_1.ObjectType.FORM_FIELD, position).toDict();
|
|
376
|
+
const response = await this._makeRequest('POST', '/pdf/find', requestData);
|
|
377
|
+
const objectsData = await response.json();
|
|
378
|
+
return objectsData.map((objData) => this._parseFormFieldRef(objData));
|
|
379
|
+
}
|
|
380
|
+
// Page Operations
|
|
381
|
+
/**
|
|
382
|
+
* Retrieves references to all pages in the PDF document.
|
|
383
|
+
*/
|
|
384
|
+
async getPages() {
|
|
385
|
+
const response = await this._makeRequest('POST', '/pdf/page/find');
|
|
386
|
+
const pagesData = await response.json();
|
|
387
|
+
return pagesData.map((pageData) => this._parseObjectRef(pageData));
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Retrieves a reference to a specific page by its page index.
|
|
391
|
+
*/
|
|
392
|
+
async _getPage(pageIndex) {
|
|
393
|
+
if (pageIndex < 0) {
|
|
394
|
+
throw new exceptions_1.ValidationException(`Page index must be >= 0, got ${pageIndex}`);
|
|
395
|
+
}
|
|
396
|
+
const params = { pageIndex: pageIndex.toString() };
|
|
397
|
+
const response = await this._makeRequest('POST', '/pdf/page/find', undefined, params);
|
|
398
|
+
const pagesData = await response.json();
|
|
399
|
+
if (!pagesData || pagesData.length === 0) {
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
return this._parseObjectRef(pagesData[0]);
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Deletes a page from the PDF document.
|
|
406
|
+
*/
|
|
407
|
+
async deletePage(pageRef) {
|
|
408
|
+
if (!pageRef) {
|
|
409
|
+
throw new exceptions_1.ValidationException("Page reference cannot be null");
|
|
410
|
+
}
|
|
411
|
+
const requestData = pageRef.toDict();
|
|
412
|
+
const response = await this._makeRequest('DELETE', '/pdf/page/delete', requestData);
|
|
413
|
+
return await response.json();
|
|
414
|
+
}
|
|
415
|
+
// Manipulation Operations
|
|
416
|
+
/**
|
|
417
|
+
* Deletes the specified PDF object from the document.
|
|
418
|
+
*/
|
|
419
|
+
async delete(objectRef) {
|
|
420
|
+
if (!objectRef) {
|
|
421
|
+
throw new exceptions_1.ValidationException("Object reference cannot be null");
|
|
422
|
+
}
|
|
423
|
+
const requestData = new models_1.DeleteRequest(objectRef).toDict();
|
|
424
|
+
const response = await this._makeRequest('DELETE', '/pdf/delete', requestData);
|
|
425
|
+
return await response.json();
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Moves a PDF object to a new position within the document.
|
|
429
|
+
*/
|
|
430
|
+
async move(objectRef, position) {
|
|
431
|
+
if (!objectRef) {
|
|
432
|
+
throw new exceptions_1.ValidationException("Object reference cannot be null");
|
|
433
|
+
}
|
|
434
|
+
if (!position) {
|
|
435
|
+
throw new exceptions_1.ValidationException("Position cannot be null");
|
|
436
|
+
}
|
|
437
|
+
const requestData = new models_1.MoveRequest(objectRef, position).toDict();
|
|
438
|
+
const response = await this._makeRequest('PUT', '/pdf/move', requestData);
|
|
439
|
+
return await response.json();
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Changes the value of a form field.
|
|
443
|
+
*/
|
|
444
|
+
async changeFormField(formFieldRef, newValue) {
|
|
445
|
+
if (!formFieldRef) {
|
|
446
|
+
throw new exceptions_1.ValidationException("Form field reference cannot be null");
|
|
447
|
+
}
|
|
448
|
+
const requestData = new models_1.ChangeFormFieldRequest(formFieldRef, newValue).toDict();
|
|
449
|
+
const response = await this._makeRequest('PUT', '/pdf/modify/formField', requestData);
|
|
450
|
+
return await response.json();
|
|
451
|
+
}
|
|
452
|
+
// Add Operations
|
|
453
|
+
/**
|
|
454
|
+
* Adds an image to the PDF document.
|
|
455
|
+
*/
|
|
456
|
+
async addImage(image, position) {
|
|
457
|
+
if (!image) {
|
|
458
|
+
throw new exceptions_1.ValidationException("Image cannot be null");
|
|
459
|
+
}
|
|
460
|
+
if (position) {
|
|
461
|
+
image.setPosition(position);
|
|
462
|
+
}
|
|
463
|
+
if (!image.getPosition()) {
|
|
464
|
+
throw new exceptions_1.ValidationException("Image position is null");
|
|
465
|
+
}
|
|
466
|
+
return this._addObject(image);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Adds a paragraph to the PDF document.
|
|
470
|
+
*/
|
|
471
|
+
async addParagraph(paragraph) {
|
|
472
|
+
if (!paragraph) {
|
|
473
|
+
throw new exceptions_1.ValidationException("Paragraph cannot be null");
|
|
474
|
+
}
|
|
475
|
+
if (!paragraph.getPosition()) {
|
|
476
|
+
throw new exceptions_1.ValidationException("Paragraph position is null");
|
|
477
|
+
}
|
|
478
|
+
if (paragraph.getPosition().pageIndex === undefined) {
|
|
479
|
+
throw new exceptions_1.ValidationException("Paragraph position page index is null");
|
|
480
|
+
}
|
|
481
|
+
if (paragraph.getPosition().pageIndex < 0) {
|
|
482
|
+
throw new exceptions_1.ValidationException("Paragraph position page index is less than 0");
|
|
483
|
+
}
|
|
484
|
+
return this._addObject(paragraph);
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Internal method to add any PDF object.
|
|
488
|
+
*/
|
|
489
|
+
async _addObject(pdfObject) {
|
|
490
|
+
const requestData = new models_1.AddRequest(pdfObject).toDict();
|
|
491
|
+
const response = await this._makeRequest('POST', '/pdf/add', requestData);
|
|
492
|
+
return await response.json();
|
|
493
|
+
}
|
|
494
|
+
// Modify Operations
|
|
495
|
+
/**
|
|
496
|
+
* Modifies a paragraph object or its text content.
|
|
497
|
+
*/
|
|
498
|
+
async modifyParagraph(objectRef, newParagraph) {
|
|
499
|
+
if (!objectRef) {
|
|
500
|
+
throw new exceptions_1.ValidationException("Object reference cannot be null");
|
|
501
|
+
}
|
|
502
|
+
if (newParagraph === null || newParagraph === undefined) {
|
|
503
|
+
throw new exceptions_1.ValidationException("New paragraph cannot be null");
|
|
504
|
+
}
|
|
505
|
+
if (typeof newParagraph === 'string') {
|
|
506
|
+
// Text modification
|
|
507
|
+
const requestData = new models_1.ModifyTextRequest(objectRef, newParagraph).toDict();
|
|
508
|
+
const response = await this._makeRequest('PUT', '/pdf/text/paragraph', requestData);
|
|
509
|
+
return await response.json();
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
// Object modification
|
|
513
|
+
const requestData = new models_1.ModifyRequest(objectRef, newParagraph).toDict();
|
|
514
|
+
const response = await this._makeRequest('PUT', '/pdf/modify', requestData);
|
|
515
|
+
return await response.json();
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Modifies a text line object.
|
|
520
|
+
*/
|
|
521
|
+
async modifyTextLine(objectRef, newText) {
|
|
522
|
+
if (!objectRef) {
|
|
523
|
+
throw new exceptions_1.ValidationException("Object reference cannot be null");
|
|
524
|
+
}
|
|
525
|
+
if (newText === null || newText === undefined) {
|
|
526
|
+
throw new exceptions_1.ValidationException("New text cannot be null");
|
|
527
|
+
}
|
|
528
|
+
const requestData = new models_1.ModifyTextRequest(objectRef, newText).toDict();
|
|
529
|
+
const response = await this._makeRequest('PUT', '/pdf/text/line', requestData);
|
|
530
|
+
return await response.json();
|
|
531
|
+
}
|
|
532
|
+
// Font Operations
|
|
533
|
+
/**
|
|
534
|
+
* Finds available fonts matching the specified name and size.
|
|
535
|
+
*/
|
|
536
|
+
async findFonts(fontName, fontSize) {
|
|
537
|
+
if (!fontName || !fontName.trim()) {
|
|
538
|
+
throw new exceptions_1.ValidationException("Font name cannot be null or empty");
|
|
539
|
+
}
|
|
540
|
+
if (fontSize <= 0) {
|
|
541
|
+
throw new exceptions_1.ValidationException(`Font size must be positive, got ${fontSize}`);
|
|
542
|
+
}
|
|
543
|
+
const params = { fontName: fontName.trim() };
|
|
544
|
+
const response = await this._makeRequest('GET', '/font/find', undefined, params);
|
|
545
|
+
const fontNames = await response.json();
|
|
546
|
+
return fontNames.map((name) => new models_1.Font(name, fontSize));
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Registers a custom font for use in PDF operations.
|
|
550
|
+
*/
|
|
551
|
+
async registerFont(ttfFile) {
|
|
552
|
+
if (!ttfFile) {
|
|
553
|
+
throw new exceptions_1.ValidationException("TTF file cannot be null");
|
|
554
|
+
}
|
|
555
|
+
try {
|
|
556
|
+
let fontData;
|
|
557
|
+
let filename;
|
|
558
|
+
if (ttfFile instanceof Uint8Array) {
|
|
559
|
+
if (ttfFile.length === 0) {
|
|
560
|
+
throw new exceptions_1.ValidationException("Font data cannot be empty");
|
|
561
|
+
}
|
|
562
|
+
fontData = ttfFile;
|
|
563
|
+
filename = 'font.ttf';
|
|
564
|
+
}
|
|
565
|
+
else if (ttfFile instanceof File) {
|
|
566
|
+
if (ttfFile.size === 0) {
|
|
567
|
+
throw new exceptions_1.ValidationException("Font file is empty");
|
|
568
|
+
}
|
|
569
|
+
fontData = new Uint8Array(await ttfFile.arrayBuffer());
|
|
570
|
+
filename = ttfFile.name;
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
throw new exceptions_1.ValidationException(`Unsupported font file type: ${typeof ttfFile}`);
|
|
574
|
+
}
|
|
575
|
+
// Upload font file
|
|
576
|
+
const formData = new FormData();
|
|
577
|
+
const blob = new Blob([fontData.buffer], { type: 'font/ttf' });
|
|
578
|
+
formData.append('ttfFile', blob, filename);
|
|
579
|
+
const response = await fetch(`${this._baseUrl}/font/register`, {
|
|
580
|
+
method: 'POST',
|
|
581
|
+
headers: {
|
|
582
|
+
'Authorization': `Bearer ${this._token}`,
|
|
583
|
+
'X-Session-Id': this._sessionId
|
|
584
|
+
},
|
|
585
|
+
body: formData,
|
|
586
|
+
signal: AbortSignal.timeout(30000)
|
|
587
|
+
});
|
|
588
|
+
if (!response.ok) {
|
|
589
|
+
const errorMessage = await this._extractErrorMessage(response);
|
|
590
|
+
throw new exceptions_1.HttpClientException(`Font registration failed: ${errorMessage}`, response);
|
|
591
|
+
}
|
|
592
|
+
return (await response.text()).trim();
|
|
593
|
+
}
|
|
594
|
+
catch (error) {
|
|
595
|
+
if (error instanceof exceptions_1.ValidationException || error instanceof exceptions_1.HttpClientException) {
|
|
596
|
+
throw error;
|
|
597
|
+
}
|
|
598
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
599
|
+
throw new exceptions_1.PdfDancerException(`Failed to read font file: ${errorMessage}`, error);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
// Document Operations
|
|
603
|
+
/**
|
|
604
|
+
* Downloads the current state of the PDF document with all modifications applied.
|
|
605
|
+
*/
|
|
606
|
+
async getPdfFile() {
|
|
607
|
+
const response = await this._makeRequest('GET', `/session/${this._sessionId}/pdf`);
|
|
608
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Saves the current PDF to a file (browser environment).
|
|
612
|
+
* Downloads the PDF in the browser.
|
|
613
|
+
*/
|
|
614
|
+
async save(filename) {
|
|
615
|
+
if (!filename) {
|
|
616
|
+
throw new exceptions_1.ValidationException("Filename cannot be null or empty");
|
|
617
|
+
}
|
|
618
|
+
try {
|
|
619
|
+
const pdfData = await this.getPdfFile();
|
|
620
|
+
fs_1.default.writeFileSync(filename, pdfData);
|
|
621
|
+
}
|
|
622
|
+
catch (error) {
|
|
623
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
624
|
+
throw new exceptions_1.PdfDancerException(`Failed to save PDF file: ${errorMessage}`, error);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
// Utility Methods
|
|
628
|
+
/**
|
|
629
|
+
* Parse JSON object data into ObjectRef instance.
|
|
630
|
+
*/
|
|
631
|
+
_parseObjectRef(objData) {
|
|
632
|
+
const positionData = objData.position || {};
|
|
633
|
+
const position = positionData ? this._parsePosition(positionData) : new models_1.Position();
|
|
634
|
+
const objectType = objData.type;
|
|
635
|
+
return new models_1.ObjectRef(objData.internalId, position, objectType);
|
|
636
|
+
}
|
|
637
|
+
_parseFormFieldRef(objData) {
|
|
638
|
+
const positionData = objData.position || {};
|
|
639
|
+
const position = positionData ? this._parsePosition(positionData) : new models_1.Position();
|
|
640
|
+
const objectType = objData.type;
|
|
641
|
+
return new models_1.FormFieldRef(objData.internalId, position, objectType, objData.name || undefined, objData.value !== undefined ? objData.value : null);
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Parse JSON position data into Position instance.
|
|
645
|
+
*/
|
|
646
|
+
_parsePosition(posData) {
|
|
647
|
+
const position = new models_1.Position();
|
|
648
|
+
position.pageIndex = posData.pageIndex;
|
|
649
|
+
position.textStartsWith = posData.textStartsWith;
|
|
650
|
+
if (posData.shape) {
|
|
651
|
+
position.shape = models_1.ShapeType[posData.shape];
|
|
652
|
+
}
|
|
653
|
+
if (posData.mode) {
|
|
654
|
+
position.mode = models_1.PositionMode[posData.mode];
|
|
655
|
+
}
|
|
656
|
+
if (posData.boundingRect) {
|
|
657
|
+
const rectData = posData.boundingRect;
|
|
658
|
+
position.boundingRect = new models_1.BoundingRect(rectData.x, rectData.y, rectData.width, rectData.height);
|
|
659
|
+
}
|
|
660
|
+
return position;
|
|
661
|
+
}
|
|
662
|
+
// Builder Pattern Support
|
|
663
|
+
toPathObjects(objectRefs) {
|
|
664
|
+
return objectRefs.map(ref => types_1.PathObject.fromRef(this, ref));
|
|
665
|
+
}
|
|
666
|
+
toFormXObjects(objectRefs) {
|
|
667
|
+
return objectRefs.map(ref => types_1.FormXObject.fromRef(this, ref));
|
|
668
|
+
}
|
|
669
|
+
toImageObjects(objectRefs) {
|
|
670
|
+
return objectRefs.map(ref => types_1.ImageObject.fromRef(this, ref));
|
|
671
|
+
}
|
|
672
|
+
newImage() {
|
|
673
|
+
return new image_builder_1.ImageBuilder(this);
|
|
674
|
+
}
|
|
675
|
+
page(pageIndex) {
|
|
676
|
+
if (pageIndex < 0) {
|
|
677
|
+
throw new exceptions_1.ValidationException(`Page index must be >= 0, got ${pageIndex}`);
|
|
678
|
+
}
|
|
679
|
+
return new PageClient(this, pageIndex);
|
|
680
|
+
}
|
|
681
|
+
async pages() {
|
|
682
|
+
let pageRefs = await this.getPages();
|
|
683
|
+
return pageRefs.map((_, pageIndex) => new PageClient(this, pageIndex));
|
|
684
|
+
}
|
|
685
|
+
toFormFields(objectRefs) {
|
|
686
|
+
return objectRefs.map(ref => types_1.FormFieldObject.fromRef(this, ref));
|
|
687
|
+
}
|
|
688
|
+
async selectParagraphs() {
|
|
689
|
+
return this.toParagraphObjects(await this.findParagraphs());
|
|
690
|
+
}
|
|
691
|
+
toParagraphObjects(objectRefs) {
|
|
692
|
+
return objectRefs.map(ref => types_1.ParagraphObject.fromRef(this, ref));
|
|
693
|
+
}
|
|
694
|
+
toTextLineObjects(objectRefs) {
|
|
695
|
+
return objectRefs.map(ref => types_1.TextLineObject.fromRef(this, ref));
|
|
696
|
+
}
|
|
697
|
+
async selectLines() {
|
|
698
|
+
return this.toTextLineObjects(await this.findTextLines());
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
exports.PDFDancer = PDFDancer;
|
|
702
|
+
//# sourceMappingURL=pdfdancer_v1.js.map
|