pdfdancer-client-typescript 1.0.13 → 1.0.14
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 +10 -6
- package/dist/__tests__/e2e/test-helpers.d.ts.map +1 -1
- package/dist/__tests__/e2e/test-helpers.js +8 -3
- package/dist/__tests__/e2e/test-helpers.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +18 -15
- package/dist/models.d.ts.map +1 -1
- package/dist/models.js +21 -18
- package/dist/models.js.map +1 -1
- package/dist/pdfdancer_v1.d.ts +2 -1
- package/dist/pdfdancer_v1.d.ts.map +1 -1
- package/dist/pdfdancer_v1.js +66 -23
- package/dist/pdfdancer_v1.js.map +1 -1
- package/docs/openapi.yml +637 -73
- package/jest.config.js +1 -1
- package/package.json +1 -1
- package/src/__tests__/e2e/image-showcase.test.ts +6 -6
- package/src/__tests__/e2e/image.test.ts +5 -5
- package/src/__tests__/e2e/line-showcase.test.ts +6 -6
- package/src/__tests__/e2e/page-showcase.test.ts +12 -12
- package/src/__tests__/e2e/page.test.ts +3 -3
- package/src/__tests__/e2e/path.test.ts +4 -4
- package/src/__tests__/e2e/snapshot-showcase.test.ts +10 -10
- package/src/__tests__/e2e/snapshot.test.ts +18 -18
- package/src/__tests__/e2e/test-helpers.ts +8 -3
- package/src/__tests__/e2e/token_from_env.test.ts +0 -14
- package/src/index.ts +3 -1
- package/src/models.ts +21 -17
- package/src/pdfdancer_v1.ts +80 -31
package/src/models.ts
CHANGED
|
@@ -255,28 +255,25 @@ export enum FontType {
|
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
/**
|
|
258
|
-
* Represents
|
|
258
|
+
* Represents font mapping information for a document font.
|
|
259
259
|
*/
|
|
260
|
-
export class
|
|
260
|
+
export class DocumentFontInfo {
|
|
261
261
|
constructor(
|
|
262
|
-
public
|
|
263
|
-
public
|
|
264
|
-
public similarityScore: number
|
|
262
|
+
public documentFontName: string,
|
|
263
|
+
public systemFontName: string
|
|
265
264
|
) {}
|
|
266
265
|
|
|
267
|
-
|
|
268
|
-
return this.
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
getFontType(): FontType {
|
|
272
|
-
return this.fontType;
|
|
266
|
+
getDocumentFontName(): string {
|
|
267
|
+
return this.documentFontName;
|
|
273
268
|
}
|
|
274
269
|
|
|
275
|
-
|
|
276
|
-
return this.
|
|
270
|
+
getSystemFontName(): string {
|
|
271
|
+
return this.systemFontName;
|
|
277
272
|
}
|
|
278
273
|
}
|
|
279
274
|
|
|
275
|
+
export { DocumentFontInfo as FontRecommendation };
|
|
276
|
+
|
|
280
277
|
/**
|
|
281
278
|
* Status information for text objects.
|
|
282
279
|
*/
|
|
@@ -285,7 +282,7 @@ export class TextStatus {
|
|
|
285
282
|
public modified: boolean,
|
|
286
283
|
public encodable: boolean,
|
|
287
284
|
public fontType: FontType,
|
|
288
|
-
public
|
|
285
|
+
public fontInfo?: DocumentFontInfo
|
|
289
286
|
) {}
|
|
290
287
|
|
|
291
288
|
isModified(): boolean {
|
|
@@ -300,8 +297,15 @@ export class TextStatus {
|
|
|
300
297
|
return this.fontType;
|
|
301
298
|
}
|
|
302
299
|
|
|
303
|
-
|
|
304
|
-
return this.
|
|
300
|
+
getFontInfo(): DocumentFontInfo | undefined {
|
|
301
|
+
return this.fontInfo;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* @deprecated Use getFontInfo() instead.
|
|
306
|
+
*/
|
|
307
|
+
getFontRecommendation(): DocumentFontInfo | undefined {
|
|
308
|
+
return this.getFontInfo();
|
|
305
309
|
}
|
|
306
310
|
}
|
|
307
311
|
|
|
@@ -986,7 +990,7 @@ export class PageSnapshot {
|
|
|
986
990
|
export class DocumentSnapshot {
|
|
987
991
|
constructor(
|
|
988
992
|
public pageCount: number,
|
|
989
|
-
public fonts:
|
|
993
|
+
public fonts: DocumentFontInfo[],
|
|
990
994
|
public pages: PageSnapshot[]
|
|
991
995
|
) {}
|
|
992
996
|
|
package/src/pdfdancer_v1.ts
CHANGED
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
DocumentSnapshot,
|
|
24
24
|
FindRequest,
|
|
25
25
|
Font,
|
|
26
|
-
|
|
26
|
+
DocumentFontInfo,
|
|
27
27
|
FontType,
|
|
28
28
|
FormFieldRef,
|
|
29
29
|
Image,
|
|
@@ -359,7 +359,7 @@ export class PDFDancer {
|
|
|
359
359
|
token: string,
|
|
360
360
|
pdfData: Uint8Array | File | ArrayBuffer,
|
|
361
361
|
baseUrl: string | null = null,
|
|
362
|
-
readTimeout: number =
|
|
362
|
+
readTimeout: number = 60000
|
|
363
363
|
) {
|
|
364
364
|
|
|
365
365
|
if (!token || !token.trim()) {
|
|
@@ -403,15 +403,15 @@ export class PDFDancer {
|
|
|
403
403
|
}
|
|
404
404
|
|
|
405
405
|
static async open(pdfData: Uint8Array, token?: string, baseUrl?: string, timeout?: number): Promise<PDFDancer> {
|
|
406
|
-
const resolvedToken = token ?? process.env.PDFDANCER_TOKEN;
|
|
407
406
|
const resolvedBaseUrl =
|
|
408
407
|
baseUrl ??
|
|
409
408
|
process.env.PDFDANCER_BASE_URL ??
|
|
410
409
|
"https://api.pdfdancer.com";
|
|
411
|
-
const resolvedTimeout = timeout ??
|
|
410
|
+
const resolvedTimeout = timeout ?? 60000;
|
|
412
411
|
|
|
412
|
+
let resolvedToken = token?.trim() ?? process.env.PDFDANCER_TOKEN?.trim() ?? null;
|
|
413
413
|
if (!resolvedToken) {
|
|
414
|
-
|
|
414
|
+
resolvedToken = await PDFDancer._obtainAnonymousToken(resolvedBaseUrl, resolvedTimeout);
|
|
415
415
|
}
|
|
416
416
|
|
|
417
417
|
const client = new PDFDancer(resolvedToken, pdfData, resolvedBaseUrl, resolvedTimeout);
|
|
@@ -427,7 +427,7 @@ export class PDFDancer {
|
|
|
427
427
|
* @param options.initialPageCount Number of initial pages (default: 1)
|
|
428
428
|
* @param token Authentication token (optional, can use PDFDANCER_TOKEN env var)
|
|
429
429
|
* @param baseUrl Base URL for the PDFDancer API (optional)
|
|
430
|
-
* @param timeout Request timeout in milliseconds (default:
|
|
430
|
+
* @param timeout Request timeout in milliseconds (default: 60000)
|
|
431
431
|
*/
|
|
432
432
|
static async new(
|
|
433
433
|
options?: {
|
|
@@ -439,15 +439,15 @@ export class PDFDancer {
|
|
|
439
439
|
baseUrl?: string,
|
|
440
440
|
timeout?: number
|
|
441
441
|
): Promise<PDFDancer> {
|
|
442
|
-
const resolvedToken = token ?? process.env.PDFDANCER_TOKEN;
|
|
443
442
|
const resolvedBaseUrl =
|
|
444
443
|
baseUrl ??
|
|
445
444
|
process.env.PDFDANCER_BASE_URL ??
|
|
446
445
|
"https://api.pdfdancer.com";
|
|
447
|
-
const resolvedTimeout = timeout ??
|
|
446
|
+
const resolvedTimeout = timeout ?? 60000;
|
|
448
447
|
|
|
448
|
+
let resolvedToken = token?.trim() ?? process.env.PDFDANCER_TOKEN?.trim() ?? null;
|
|
449
449
|
if (!resolvedToken) {
|
|
450
|
-
|
|
450
|
+
resolvedToken = await PDFDancer._obtainAnonymousToken(resolvedBaseUrl, resolvedTimeout);
|
|
451
451
|
}
|
|
452
452
|
|
|
453
453
|
let createRequest: CreatePdfRequest;
|
|
@@ -517,6 +517,47 @@ export class PDFDancer {
|
|
|
517
517
|
}
|
|
518
518
|
}
|
|
519
519
|
|
|
520
|
+
private static async _obtainAnonymousToken(baseUrl: string, timeout: number = 60000): Promise<string> {
|
|
521
|
+
const normalizedBaseUrl = (baseUrl || "https://api.pdfdancer.com").replace(/\/+$/, '');
|
|
522
|
+
const url = `${normalizedBaseUrl}/keys/anon`;
|
|
523
|
+
|
|
524
|
+
try {
|
|
525
|
+
const fingerprint = await generateFingerprint();
|
|
526
|
+
const response = await fetch(url, {
|
|
527
|
+
method: 'POST',
|
|
528
|
+
headers: {
|
|
529
|
+
'Content-Type': 'application/json',
|
|
530
|
+
'X-Fingerprint': fingerprint,
|
|
531
|
+
'X-Generated-At': generateTimestamp()
|
|
532
|
+
},
|
|
533
|
+
signal: timeout > 0 ? AbortSignal.timeout(timeout) : undefined
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
if (!response.ok) {
|
|
537
|
+
const errorText = await response.text().catch(() => '');
|
|
538
|
+
throw new HttpClientException(
|
|
539
|
+
`Failed to obtain anonymous token: ${errorText || `HTTP ${response.status}`}`,
|
|
540
|
+
response
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
const tokenPayload: any = await response.json().catch(() => null);
|
|
545
|
+
const tokenValue = typeof tokenPayload?.token === 'string' ? tokenPayload.token.trim() : '';
|
|
546
|
+
|
|
547
|
+
if (!tokenValue) {
|
|
548
|
+
throw new HttpClientException("Invalid anonymous token response format", response);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
return tokenValue;
|
|
552
|
+
} catch (error) {
|
|
553
|
+
if (error instanceof HttpClientException) {
|
|
554
|
+
throw error;
|
|
555
|
+
}
|
|
556
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
557
|
+
throw new HttpClientException(`Failed to obtain anonymous token: ${errorMessage}`, undefined, error as Error);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
520
561
|
/**
|
|
521
562
|
* Process PDF data from various input types with strict validation.
|
|
522
563
|
*/
|
|
@@ -1428,7 +1469,7 @@ export class PDFDancer {
|
|
|
1428
1469
|
'X-Fingerprint': fingerprint
|
|
1429
1470
|
},
|
|
1430
1471
|
body: formData,
|
|
1431
|
-
signal: AbortSignal.timeout(
|
|
1472
|
+
signal: AbortSignal.timeout(60000)
|
|
1432
1473
|
});
|
|
1433
1474
|
|
|
1434
1475
|
logGeneratedAtHeader(response, 'POST', '/font/register');
|
|
@@ -1534,25 +1575,30 @@ export class PDFDancer {
|
|
|
1534
1575
|
let status: TextStatus | undefined;
|
|
1535
1576
|
const statusData = objData.status;
|
|
1536
1577
|
if (statusData && typeof statusData === 'object') {
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
// Create empty font recommendation if not provided
|
|
1548
|
-
fontRec = new FontRecommendation('', FontType.SYSTEM, 0.0);
|
|
1578
|
+
const fontInfoSource = statusData.fontInfoDto ?? statusData.fontRecommendation;
|
|
1579
|
+
let fontInfo: DocumentFontInfo | undefined;
|
|
1580
|
+
if (fontInfoSource && typeof fontInfoSource === 'object') {
|
|
1581
|
+
const documentFontName = typeof fontInfoSource.documentFontName === 'string'
|
|
1582
|
+
? fontInfoSource.documentFontName
|
|
1583
|
+
: (typeof fontInfoSource.fontName === 'string' ? fontInfoSource.fontName : '');
|
|
1584
|
+
const systemFontName = typeof fontInfoSource.systemFontName === 'string'
|
|
1585
|
+
? fontInfoSource.systemFontName
|
|
1586
|
+
: (typeof fontInfoSource.fontName === 'string' ? fontInfoSource.fontName : '');
|
|
1587
|
+
fontInfo = new DocumentFontInfo(documentFontName, systemFontName);
|
|
1549
1588
|
}
|
|
1550
1589
|
|
|
1590
|
+
const modified = statusData.modified !== undefined ? Boolean(statusData.modified) : false;
|
|
1591
|
+
const encodable = statusData.encodable !== undefined ? Boolean(statusData.encodable) : true;
|
|
1592
|
+
const fontTypeValue = typeof statusData.fontType === 'string'
|
|
1593
|
+
&& (Object.values(FontType) as string[]).includes(statusData.fontType)
|
|
1594
|
+
? statusData.fontType as FontType
|
|
1595
|
+
: FontType.SYSTEM;
|
|
1596
|
+
|
|
1551
1597
|
status = new TextStatus(
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1598
|
+
modified,
|
|
1599
|
+
encodable,
|
|
1600
|
+
fontTypeValue,
|
|
1601
|
+
fontInfo
|
|
1556
1602
|
);
|
|
1557
1603
|
}
|
|
1558
1604
|
|
|
@@ -1704,14 +1750,17 @@ export class PDFDancer {
|
|
|
1704
1750
|
const pageCount = typeof data.pageCount === 'number' ? data.pageCount : 0;
|
|
1705
1751
|
|
|
1706
1752
|
// Parse fonts
|
|
1707
|
-
const fonts:
|
|
1753
|
+
const fonts: DocumentFontInfo[] = [];
|
|
1708
1754
|
if (Array.isArray(data.fonts)) {
|
|
1709
1755
|
for (const fontData of data.fonts) {
|
|
1710
1756
|
if (fontData && typeof fontData === 'object') {
|
|
1711
|
-
const
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1757
|
+
const documentFontName = typeof fontData.documentFontName === 'string'
|
|
1758
|
+
? fontData.documentFontName
|
|
1759
|
+
: (typeof fontData.fontName === 'string' ? fontData.fontName : '');
|
|
1760
|
+
const systemFontName = typeof fontData.systemFontName === 'string'
|
|
1761
|
+
? fontData.systemFontName
|
|
1762
|
+
: (typeof fontData.fontName === 'string' ? fontData.fontName : '');
|
|
1763
|
+
fonts.push(new DocumentFontInfo(documentFontName, systemFontName));
|
|
1715
1764
|
}
|
|
1716
1765
|
}
|
|
1717
1766
|
}
|