@pdfme/manipulator 5.4.0 → 5.4.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.
Files changed (85) hide show
  1. package/dist/cjs/__tests__/e2e/insert.e2e.test.js +32 -0
  2. package/dist/cjs/__tests__/e2e/insert.e2e.test.js.map +1 -0
  3. package/dist/cjs/__tests__/e2e/merge.e2e.test.js +20 -0
  4. package/dist/cjs/__tests__/e2e/merge.e2e.test.js.map +1 -0
  5. package/dist/cjs/__tests__/e2e/move.e2e.test.js +18 -0
  6. package/dist/cjs/__tests__/e2e/move.e2e.test.js.map +1 -0
  7. package/dist/cjs/__tests__/e2e/organize-complex.e2e.test.js +96 -0
  8. package/dist/cjs/__tests__/e2e/organize-complex.e2e.test.js.map +1 -0
  9. package/dist/cjs/__tests__/e2e/organize-single.e2e.test.js +68 -0
  10. package/dist/cjs/__tests__/e2e/organize-single.e2e.test.js.map +1 -0
  11. package/dist/cjs/__tests__/e2e/remove.e2e.test.js +28 -0
  12. package/dist/cjs/__tests__/e2e/remove.e2e.test.js.map +1 -0
  13. package/dist/cjs/__tests__/e2e/rotate.e2e.test.js +38 -0
  14. package/dist/cjs/__tests__/e2e/rotate.e2e.test.js.map +1 -0
  15. package/dist/cjs/__tests__/e2e/split.e2e.test.js +27 -0
  16. package/dist/cjs/__tests__/e2e/split.e2e.test.js.map +1 -0
  17. package/dist/cjs/__tests__/insert.test.js +18 -0
  18. package/dist/cjs/__tests__/insert.test.js.map +1 -0
  19. package/dist/cjs/__tests__/merge.test.js +16 -0
  20. package/dist/cjs/__tests__/merge.test.js.map +1 -0
  21. package/dist/cjs/__tests__/move.test.js +16 -0
  22. package/dist/cjs/__tests__/move.test.js.map +1 -0
  23. package/dist/cjs/__tests__/organize.test.js +75 -0
  24. package/dist/cjs/__tests__/organize.test.js.map +1 -0
  25. package/dist/cjs/__tests__/remove.test.js +20 -0
  26. package/dist/cjs/__tests__/remove.test.js.map +1 -0
  27. package/dist/cjs/__tests__/rotate.test.js +17 -0
  28. package/dist/cjs/__tests__/rotate.test.js.map +1 -0
  29. package/dist/cjs/__tests__/split.test.js +21 -0
  30. package/dist/cjs/__tests__/split.test.js.map +1 -0
  31. package/dist/cjs/__tests__/test-helpers.js +44 -0
  32. package/dist/cjs/__tests__/test-helpers.js.map +1 -0
  33. package/dist/esm/__tests__/e2e/insert.e2e.test.js +30 -0
  34. package/dist/esm/__tests__/e2e/insert.e2e.test.js.map +1 -0
  35. package/dist/esm/__tests__/e2e/merge.e2e.test.js +18 -0
  36. package/dist/esm/__tests__/e2e/merge.e2e.test.js.map +1 -0
  37. package/dist/esm/__tests__/e2e/move.e2e.test.js +16 -0
  38. package/dist/esm/__tests__/e2e/move.e2e.test.js.map +1 -0
  39. package/dist/esm/__tests__/e2e/organize-complex.e2e.test.js +94 -0
  40. package/dist/esm/__tests__/e2e/organize-complex.e2e.test.js.map +1 -0
  41. package/dist/esm/__tests__/e2e/organize-single.e2e.test.js +66 -0
  42. package/dist/esm/__tests__/e2e/organize-single.e2e.test.js.map +1 -0
  43. package/dist/esm/__tests__/e2e/remove.e2e.test.js +26 -0
  44. package/dist/esm/__tests__/e2e/remove.e2e.test.js.map +1 -0
  45. package/dist/esm/__tests__/e2e/rotate.e2e.test.js +36 -0
  46. package/dist/esm/__tests__/e2e/rotate.e2e.test.js.map +1 -0
  47. package/dist/esm/__tests__/e2e/split.e2e.test.js +25 -0
  48. package/dist/esm/__tests__/e2e/split.e2e.test.js.map +1 -0
  49. package/dist/esm/__tests__/insert.test.js +16 -0
  50. package/dist/esm/__tests__/insert.test.js.map +1 -0
  51. package/dist/esm/__tests__/merge.test.js +14 -0
  52. package/dist/esm/__tests__/merge.test.js.map +1 -0
  53. package/dist/esm/__tests__/move.test.js +14 -0
  54. package/dist/esm/__tests__/move.test.js.map +1 -0
  55. package/dist/esm/__tests__/organize.test.js +73 -0
  56. package/dist/esm/__tests__/organize.test.js.map +1 -0
  57. package/dist/esm/__tests__/remove.test.js +18 -0
  58. package/dist/esm/__tests__/remove.test.js.map +1 -0
  59. package/dist/esm/__tests__/rotate.test.js +15 -0
  60. package/dist/esm/__tests__/rotate.test.js.map +1 -0
  61. package/dist/esm/__tests__/split.test.js +19 -0
  62. package/dist/esm/__tests__/split.test.js.map +1 -0
  63. package/dist/esm/__tests__/test-helpers.js +32 -0
  64. package/dist/esm/__tests__/test-helpers.js.map +1 -0
  65. package/dist/types/__tests__/e2e/merge.e2e.test.d.ts +1 -0
  66. package/dist/types/__tests__/e2e/move.e2e.test.d.ts +1 -0
  67. package/dist/types/__tests__/e2e/organize-complex.e2e.test.d.ts +1 -0
  68. package/dist/types/__tests__/e2e/organize-single.e2e.test.d.ts +1 -0
  69. package/dist/types/__tests__/e2e/remove.e2e.test.d.ts +1 -0
  70. package/dist/types/__tests__/e2e/rotate.e2e.test.d.ts +1 -0
  71. package/dist/types/__tests__/e2e/split.e2e.test.d.ts +1 -0
  72. package/dist/types/__tests__/insert.test.d.ts +1 -0
  73. package/dist/types/__tests__/merge.test.d.ts +1 -0
  74. package/dist/types/__tests__/move.test.d.ts +1 -0
  75. package/dist/types/__tests__/organize.test.d.ts +1 -0
  76. package/dist/types/__tests__/remove.test.d.ts +1 -0
  77. package/dist/types/__tests__/rotate.test.d.ts +1 -0
  78. package/dist/types/__tests__/split.test.d.ts +1 -0
  79. package/dist/types/__tests__/test-helpers.d.ts +6 -0
  80. package/package.json +1 -1
  81. package/dist/cjs/__tests__/index.test.js +0 -532
  82. package/dist/cjs/__tests__/index.test.js.map +0 -1
  83. package/dist/esm/__tests__/index.test.js +0 -527
  84. package/dist/esm/__tests__/index.test.js.map +0 -1
  85. /package/dist/types/__tests__/{index.test.d.ts → e2e/insert.e2e.test.d.ts} +0 -0
@@ -0,0 +1,19 @@
1
+ import { split } from '../src/index';
2
+ import { createTestPDF, getPDFPageCount } from './test-helpers';
3
+ describe('split', () => {
4
+ test('splits PDF into ranges', async () => {
5
+ const pdf = await createTestPDF(5);
6
+ const splits = await split(pdf, [
7
+ { start: 0, end: 1 },
8
+ { start: 2, end: 4 },
9
+ ]);
10
+ expect(splits.length).toBe(2);
11
+ expect(await getPDFPageCount(splits[0])).toBe(2);
12
+ expect(await getPDFPageCount(splits[1])).toBe(3);
13
+ });
14
+ test('throws error for invalid ranges', async () => {
15
+ const pdf = await createTestPDF(3);
16
+ await expect(split(pdf, [{ start: 1, end: 0 }])).rejects.toThrow('[@pdfme/manipulator] Invalid range');
17
+ });
18
+ });
19
+ //# sourceMappingURL=split.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"split.test.js","sourceRoot":"","sources":["../../../__tests__/split.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEhE,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE;IACrB,IAAI,CAAC,wBAAwB,EAAE,KAAK,IAAI,EAAE;QACxC,MAAM,GAAG,GAAG,MAAM,aAAa,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;YAC9B,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE;YACpB,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE;SACrB,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAC9B,MAAM,CAAC,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACjD,MAAM,CAAC,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACnD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;QACjD,MAAM,GAAG,GAAG,MAAM,aAAa,CAAC,CAAC,CAAC,CAAC;QACnC,MAAM,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAC9D,oCAAoC,CACrC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,32 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { PDFDocument } from '@pdfme/pdf-lib';
4
+ import { pdf2img } from '@pdfme/converter';
5
+ export const createTestPDF = async (pageCount) => {
6
+ const pdfDoc = await PDFDocument.create();
7
+ for (let i = 0; i < pageCount; i++) {
8
+ const page = pdfDoc.addPage([500, 500]);
9
+ page.drawText(`Page ${i + 1}`, {
10
+ x: 50,
11
+ y: 450,
12
+ size: 20,
13
+ });
14
+ }
15
+ return pdfDoc.save();
16
+ };
17
+ export const pdfToImages = async (pdf) => {
18
+ const arrayBuffers = await pdf2img(pdf, { imageType: 'png' });
19
+ return arrayBuffers.map((buf) => Buffer.from(new Uint8Array(buf)));
20
+ };
21
+ export const getPDFPageCount = async (pdf) => {
22
+ const pdfDoc = await PDFDocument.load(pdf);
23
+ return pdfDoc.getPageCount();
24
+ };
25
+ export const assetPath = (fileName) => path.join(__dirname, 'assets/pdfs', fileName);
26
+ export function toArrayBuffer(buf) {
27
+ return new Uint8Array(buf);
28
+ }
29
+ export const loadTestPDF = (fileName) => {
30
+ return toArrayBuffer(fs.readFileSync(assetPath(fileName)));
31
+ };
32
+ //# sourceMappingURL=test-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-helpers.js","sourceRoot":"","sources":["../../../__tests__/test-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAE,SAAiB,EAAuB,EAAE;IAC5E,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;IAC1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,MAAM,IAAI,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE;YAC7B,CAAC,EAAE,EAAE;YACL,CAAC,EAAE,GAAG;YACN,IAAI,EAAE,EAAE;SACT,CAAC,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC;AACvB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAAE,GAA6B,EAAqB,EAAE;IACpF,MAAM,YAAY,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CAAC;IAC9D,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACrE,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,EAAE,GAA6B,EAAmB,EAAE;IACtF,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC3C,OAAO,MAAM,CAAC,YAAY,EAAE,CAAC;AAC/B,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,QAAgB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC;AAE7F,MAAM,UAAU,aAAa,CAAC,GAAW;IACvC,OAAO,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAC7B,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAgB,EAAc,EAAE;IAC1D,OAAO,aAAa,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;AAC7D,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ import 'jest-image-snapshot';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ export declare const createTestPDF: (pageCount: number) => Promise<Uint8Array>;
2
+ export declare const pdfToImages: (pdf: ArrayBuffer | Uint8Array) => Promise<Buffer[]>;
3
+ export declare const getPDFPageCount: (pdf: ArrayBuffer | Uint8Array) => Promise<number>;
4
+ export declare const assetPath: (fileName: string) => string;
5
+ export declare function toArrayBuffer(buf: Buffer): Uint8Array;
6
+ export declare const loadTestPDF: (fileName: string) => Uint8Array;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdfme/manipulator",
3
- "version": "5.4.0",
3
+ "version": "5.4.1",
4
4
  "sideEffects": false,
5
5
  "author": "hand-dot",
6
6
  "license": "MIT",
@@ -1,532 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const fs_1 = __importDefault(require("fs"));
7
- const path_1 = __importDefault(require("path"));
8
- const pdf_lib_1 = require("@pdfme/pdf-lib");
9
- const converter_1 = require("@pdfme/converter");
10
- const index_js_1 = require("../src/index.js");
11
- require("jest-image-snapshot");
12
- const createTestPDF = async (pageCount) => {
13
- const pdfDoc = await pdf_lib_1.PDFDocument.create();
14
- for (let i = 0; i < pageCount; i++) {
15
- const page = pdfDoc.addPage([500, 500]);
16
- page.drawText(`Page ${i + 1}`, {
17
- x: 50,
18
- y: 450,
19
- size: 20,
20
- });
21
- }
22
- return pdfDoc.save();
23
- };
24
- const pdfToImages = async (pdf) => {
25
- const arrayBuffers = await (0, converter_1.pdf2img)(pdf, { imageType: 'png' });
26
- return arrayBuffers.map((buf) => Buffer.from(new Uint8Array(buf)));
27
- };
28
- const getPDFPageCount = async (pdf) => {
29
- const pdfDoc = await pdf_lib_1.PDFDocument.load(pdf);
30
- return pdfDoc.getPageCount();
31
- };
32
- describe('merge', () => {
33
- test('merges multiple PDFs', async () => {
34
- const pdf1 = await createTestPDF(2);
35
- const pdf2 = await createTestPDF(3);
36
- const merged = await (0, index_js_1.merge)([pdf1, pdf2]);
37
- expect(await getPDFPageCount(merged)).toBe(5);
38
- });
39
- test('throws error when no PDFs provided', async () => {
40
- await expect((0, index_js_1.merge)([])).rejects.toThrow('[@pdfme/manipulator] At least one PDF is required');
41
- });
42
- });
43
- describe('split', () => {
44
- test('splits PDF into ranges', async () => {
45
- const pdf = await createTestPDF(5);
46
- const splits = await (0, index_js_1.split)(pdf, [
47
- { start: 0, end: 1 },
48
- { start: 2, end: 4 },
49
- ]);
50
- expect(splits.length).toBe(2);
51
- expect(await getPDFPageCount(splits[0])).toBe(2);
52
- expect(await getPDFPageCount(splits[1])).toBe(3);
53
- });
54
- test('throws error for invalid ranges', async () => {
55
- const pdf = await createTestPDF(3);
56
- await expect((0, index_js_1.split)(pdf, [{ start: 1, end: 0 }])).rejects.toThrow('[@pdfme/manipulator] Invalid range');
57
- });
58
- });
59
- describe('remove', () => {
60
- test('removes specified pages from PDF', async () => {
61
- const pdf = await createTestPDF(5);
62
- const result = await (0, index_js_1.remove)(pdf, [1, 3]);
63
- expect(await getPDFPageCount(result)).toBe(3);
64
- });
65
- test('throws error when no pages provided', async () => {
66
- const pdf = await createTestPDF(3);
67
- await expect((0, index_js_1.remove)(pdf, [])).rejects.toThrow('[@pdfme/manipulator] At least one page number is required');
68
- });
69
- test('throws error for invalid page numbers', async () => {
70
- const pdf = await createTestPDF(3);
71
- await expect((0, index_js_1.remove)(pdf, [3])).rejects.toThrow('[@pdfme/manipulator] Invalid page number');
72
- });
73
- });
74
- describe('insert', () => {
75
- test('inserts PDF at specified position', async () => {
76
- const basePdf = await createTestPDF(3);
77
- const insertPdf = await createTestPDF(2);
78
- const result = await (0, index_js_1.insert)(basePdf, [{ pdf: insertPdf, position: 1 }]);
79
- expect(await getPDFPageCount(result)).toBe(5);
80
- });
81
- test('throws error for invalid position', async () => {
82
- const basePdf = await createTestPDF(3);
83
- const insertPdf = await createTestPDF(2);
84
- await expect((0, index_js_1.insert)(basePdf, [{ pdf: insertPdf, position: 4 }])).rejects.toThrow('[@pdfme/manipulator] Invalid position');
85
- });
86
- });
87
- describe('rotate', () => {
88
- test('rotates PDF pages by specified degrees', async () => {
89
- const pdf = await createTestPDF(2);
90
- const result = await (0, index_js_1.rotate)(pdf, 90);
91
- expect(await getPDFPageCount(result)).toBe(2);
92
- });
93
- test('throws error for non-90-degree rotation', async () => {
94
- const pdf = await createTestPDF(2);
95
- // @ts-expect-error
96
- await expect((0, index_js_1.rotate)(pdf, 45)).rejects.toThrow('[@pdfme/manipulator] Rotation degrees must be a multiple of 90');
97
- });
98
- });
99
- describe('move', () => {
100
- test('moves page from one position to another', async () => {
101
- const pdf = await createTestPDF(3);
102
- const result = await (0, index_js_1.move)(pdf, { from: 0, to: 2 });
103
- expect(await getPDFPageCount(result)).toBe(3);
104
- });
105
- test('throws error for invalid page numbers', async () => {
106
- const pdf = await createTestPDF(3);
107
- await expect((0, index_js_1.move)(pdf, { from: 3, to: 0 })).rejects.toThrow('[@pdfme/manipulator] Invalid page number: from=3, to=0, total pages=3');
108
- });
109
- });
110
- describe('organize', () => {
111
- test('performs single remove operation', async () => {
112
- const pdf = await createTestPDF(5);
113
- const result = await (0, index_js_1.organize)(pdf, [
114
- { type: 'remove', data: { position: 1 } },
115
- { type: 'remove', data: { position: 3 } },
116
- ]);
117
- expect(await getPDFPageCount(result)).toBe(3);
118
- });
119
- test('performs single insert operation', async () => {
120
- const pdf = await createTestPDF(3);
121
- const insertPdf = await createTestPDF(2);
122
- const result = await (0, index_js_1.organize)(pdf, [{ type: 'insert', data: { pdf: insertPdf, position: 1 } }]);
123
- expect(await getPDFPageCount(result)).toBe(5);
124
- });
125
- test('performs single replace operation', async () => {
126
- const pdf = await createTestPDF(3);
127
- const replacePdf = await createTestPDF(1);
128
- const result = await (0, index_js_1.organize)(pdf, [
129
- { type: 'replace', data: { position: 1, pdf: replacePdf } },
130
- ]);
131
- expect(await getPDFPageCount(result)).toBe(3);
132
- });
133
- test('performs single rotate operation', async () => {
134
- const pdf = await createTestPDF(3);
135
- const result = await (0, index_js_1.organize)(pdf, [
136
- { type: 'rotate', data: { position: 0, degrees: 90 } },
137
- { type: 'rotate', data: { position: 2, degrees: 90 } },
138
- ]);
139
- expect(await getPDFPageCount(result)).toBe(3);
140
- });
141
- test('performs multiple operations in sequence', async () => {
142
- const pdf = await createTestPDF(5);
143
- const insertPdf = await createTestPDF(2);
144
- const replacePdf = await createTestPDF(1);
145
- const result = await (0, index_js_1.organize)(pdf, [
146
- { type: 'remove', data: { position: 1 } },
147
- { type: 'remove', data: { position: 3 } }, // 5 -> 3 pages
148
- { type: 'insert', data: { pdf: insertPdf, position: 1 } }, // 3 -> 5 pages
149
- { type: 'replace', data: { position: 2, pdf: replacePdf } }, // Still 5 pages
150
- { type: 'rotate', data: { position: 0, degrees: 90 } },
151
- { type: 'rotate', data: { position: 3, degrees: 90 } }, // Still 5 pages
152
- ]);
153
- expect(await getPDFPageCount(result)).toBe(5);
154
- });
155
- test('throws error for invalid page numbers', async () => {
156
- const pdf = await createTestPDF(3);
157
- await expect((0, index_js_1.organize)(pdf, [{ type: 'remove', data: { position: 3 } }])).rejects.toThrow('[@pdfme/manipulator] Invalid page number');
158
- });
159
- test('throws error for invalid position', async () => {
160
- const pdf = await createTestPDF(3);
161
- const insertPdf = await createTestPDF(1);
162
- await expect((0, index_js_1.organize)(pdf, [{ type: 'insert', data: { pdf: insertPdf, position: 4 } }])).rejects.toThrow('[@pdfme/manipulator] Invalid position');
163
- });
164
- test('throws error for invalid rotation degrees', async () => {
165
- const pdf = await createTestPDF(3);
166
- await expect(
167
- // @ts-expect-error
168
- (0, index_js_1.organize)(pdf, [{ type: 'rotate', data: { pages: [0], degrees: 45 } }])).rejects.toThrow('[@pdfme/manipulator] Rotation degrees must be a multiple of 90');
169
- });
170
- test('throws error for empty actions array', async () => {
171
- const pdf = await createTestPDF(3);
172
- await expect((0, index_js_1.organize)(pdf, [])).rejects.toThrow('[@pdfme/manipulator] At least one action is required');
173
- });
174
- test('throws error for unknown action type', async () => {
175
- const pdf = await createTestPDF(3);
176
- // @ts-expect-error
177
- await expect((0, index_js_1.organize)(pdf, [{ type: 'invalid', data: { pages: [] } }])).rejects.toThrow('[@pdfme/manipulator] Unknown action type: invalid');
178
- });
179
- });
180
- describe('organize', () => {
181
- test('performs single remove operation', async () => {
182
- const pdf = await createTestPDF(5);
183
- const result = await (0, index_js_1.organize)(pdf, [
184
- { type: 'remove', data: { position: 1 } },
185
- { type: 'remove', data: { position: 3 } },
186
- ]);
187
- expect(await getPDFPageCount(result)).toBe(3);
188
- });
189
- test('performs single insert operation', async () => {
190
- const pdf = await createTestPDF(3);
191
- const insertPdf = await createTestPDF(2);
192
- const result = await (0, index_js_1.organize)(pdf, [{ type: 'insert', data: { pdf: insertPdf, position: 1 } }]);
193
- expect(await getPDFPageCount(result)).toBe(5);
194
- });
195
- test('performs single replace operation', async () => {
196
- const pdf = await createTestPDF(3);
197
- const replacePdf = await createTestPDF(1);
198
- const result = await (0, index_js_1.organize)(pdf, [
199
- { type: 'replace', data: { position: 1, pdf: replacePdf } },
200
- ]);
201
- expect(await getPDFPageCount(result)).toBe(3);
202
- });
203
- test('performs single rotate operation', async () => {
204
- const pdf = await createTestPDF(3);
205
- const result = await (0, index_js_1.organize)(pdf, [
206
- { type: 'rotate', data: { position: 0, degrees: 90 } },
207
- { type: 'rotate', data: { position: 2, degrees: 90 } },
208
- ]);
209
- expect(await getPDFPageCount(result)).toBe(3);
210
- });
211
- test('performs multiple operations in sequence', async () => {
212
- const pdf = await createTestPDF(5);
213
- const insertPdf = await createTestPDF(2);
214
- const replacePdf = await createTestPDF(1);
215
- const result = await (0, index_js_1.organize)(pdf, [
216
- { type: 'remove', data: { position: 1 } },
217
- { type: 'remove', data: { position: 3 } }, // 5 -> 3 pages
218
- { type: 'insert', data: { pdf: insertPdf, position: 1 } }, // 3 -> 5 pages
219
- { type: 'replace', data: { position: 2, pdf: replacePdf } }, // Still 5 pages
220
- { type: 'rotate', data: { position: 0, degrees: 90 } },
221
- { type: 'rotate', data: { position: 3, degrees: 90 } }, // Still 5 pages
222
- ]);
223
- expect(await getPDFPageCount(result)).toBe(5);
224
- });
225
- test('throws error for invalid page numbers', async () => {
226
- const pdf = await createTestPDF(3);
227
- await expect((0, index_js_1.organize)(pdf, [{ type: 'remove', data: { position: 3 } }])).rejects.toThrow('[@pdfme/manipulator] Invalid page number');
228
- });
229
- test('throws error for invalid position', async () => {
230
- const pdf = await createTestPDF(3);
231
- const insertPdf = await createTestPDF(1);
232
- await expect((0, index_js_1.organize)(pdf, [{ type: 'insert', data: { pdf: insertPdf, position: 4 } }])).rejects.toThrow('[@pdfme/manipulator] Invalid position');
233
- });
234
- test('throws error for invalid rotation degrees', async () => {
235
- const pdf = await createTestPDF(3);
236
- await expect(
237
- // @ts-expect-error
238
- (0, index_js_1.organize)(pdf, [{ type: 'rotate', data: { pages: [0], degrees: 45 } }])).rejects.toThrow('[@pdfme/manipulator] Rotation degrees must be a multiple of 90');
239
- });
240
- test('throws error for empty actions array', async () => {
241
- const pdf = await createTestPDF(3);
242
- await expect((0, index_js_1.organize)(pdf, [])).rejects.toThrow('[@pdfme/manipulator] At least one action is required');
243
- });
244
- test('throws error for unknown action type', async () => {
245
- const pdf = await createTestPDF(3);
246
- // @ts-expect-error
247
- await expect((0, index_js_1.organize)(pdf, [{ type: 'invalid', data: { pages: [] } }])).rejects.toThrow('[@pdfme/manipulator] Unknown action type: invalid');
248
- });
249
- });
250
- describe('PDF manipulator E2E Tests with real PDF files', () => {
251
- const assetPath = (fileName) => path_1.default.join(__dirname, 'assets/pdfs', fileName);
252
- function toArrayBuffer(buf) {
253
- return new Uint8Array(buf);
254
- }
255
- const fiveP = toArrayBuffer(fs_1.default.readFileSync(assetPath('5p.pdf')));
256
- const aPdf = toArrayBuffer(fs_1.default.readFileSync(assetPath('a.pdf')));
257
- const bPdf = toArrayBuffer(fs_1.default.readFileSync(assetPath('b.pdf')));
258
- const cPdf = toArrayBuffer(fs_1.default.readFileSync(assetPath('c.pdf')));
259
- //
260
- // merge
261
- //
262
- test('merge: merge a.pdf, b.pdf, c.pdf in order', async () => {
263
- const mergedBuffer = await (0, index_js_1.merge)([aPdf, bPdf, cPdf]);
264
- const images = await pdfToImages(mergedBuffer);
265
- for (let i = 0; i < images.length; i++) {
266
- expect(images[i]).toMatchImageSnapshot({
267
- customSnapshotIdentifier: `merge-abc-page${i + 1}`,
268
- });
269
- }
270
- });
271
- //
272
- // split
273
- //
274
- test('split: split 5p.pdf into pages 1-2 and 3-5', async () => {
275
- const [split12, split35] = await (0, index_js_1.split)(fiveP, [
276
- { start: 0, end: 1 }, // pages 1-2
277
- { start: 2, end: 4 }, // pages 3-5
278
- ]);
279
- const images12 = await pdfToImages(split12);
280
- const images35 = await pdfToImages(split35);
281
- for (let i = 0; i < images12.length; i++) {
282
- expect(images12[i]).toMatchImageSnapshot({
283
- customSnapshotIdentifier: `split-5p-1-2-page${i + 1}`,
284
- });
285
- }
286
- for (let i = 0; i < images35.length; i++) {
287
- expect(images35[i]).toMatchImageSnapshot({
288
- customSnapshotIdentifier: `split-5p-3-5-page${i + 1}`,
289
- });
290
- }
291
- });
292
- //
293
- // remove
294
- //
295
- test('remove: remove the 1st page of 5p.pdf', async () => {
296
- const removed = await (0, index_js_1.remove)(fiveP, [0]);
297
- const images = await pdfToImages(removed);
298
- for (let i = 0; i < images.length; i++) {
299
- expect(images[i]).toMatchImageSnapshot({
300
- customSnapshotIdentifier: `remove-5p-page1-result-page${i + 1}`,
301
- });
302
- }
303
- });
304
- test('remove: remove the 1st and 3rd pages of 5p.pdf', async () => {
305
- // Note: This assumes removing all at once, not one by one with index shifting
306
- const removed = await (0, index_js_1.remove)(fiveP, [0, 2]);
307
- const images = await pdfToImages(removed);
308
- for (let i = 0; i < images.length; i++) {
309
- expect(images[i]).toMatchImageSnapshot({
310
- customSnapshotIdentifier: `remove-5p-pages1-3-result-page${i + 1}`,
311
- });
312
- }
313
- });
314
- //
315
- // insert
316
- //
317
- test('insert: insert a.pdf at position 0 in 5p.pdf', async () => {
318
- const inserted = await (0, index_js_1.insert)(fiveP, [{ pdf: aPdf, position: 0 }]);
319
- const images = await pdfToImages(inserted);
320
- for (let i = 0; i < images.length; i++) {
321
- expect(images[i]).toMatchImageSnapshot({
322
- customSnapshotIdentifier: `insert-5p-a-at-0-result-page${i + 1}`,
323
- });
324
- }
325
- });
326
- test('insert: insert a.pdf at position 0 and 2 in 5p.pdf', async () => {
327
- // Note: the second insert is done in the same buffer, so the offset changes after the first insert
328
- const inserted = await (0, index_js_1.insert)(fiveP, [
329
- { pdf: aPdf, position: 0 },
330
- { pdf: aPdf, position: 2 }, // The PDF is 6 pages after the first insert
331
- ]);
332
- const images = await pdfToImages(inserted);
333
- for (let i = 0; i < images.length; i++) {
334
- expect(images[i]).toMatchImageSnapshot({
335
- customSnapshotIdentifier: `insert-5p-a-at-0-and-2-result-page${i + 1}`,
336
- });
337
- }
338
- });
339
- //
340
- // rotate
341
- //
342
- test('rotate: rotate all pages of 5p.pdf by 90 degrees', async () => {
343
- const rotated = await (0, index_js_1.rotate)(fiveP, 90);
344
- const images = await pdfToImages(rotated);
345
- for (let i = 0; i < images.length; i++) {
346
- expect(images[i]).toMatchImageSnapshot({
347
- customSnapshotIdentifier: `rotate-5p-90deg-all-result-page${i + 1}`,
348
- });
349
- }
350
- });
351
- test('rotate: rotate only the 2nd page of 5p.pdf by 180 degrees', async () => {
352
- // pageNumbers=[1] -> only the 2nd page is rotated by 180 degrees
353
- const rotated = await (0, index_js_1.rotate)(fiveP, 180, [1]);
354
- const images = await pdfToImages(rotated);
355
- for (let i = 0; i < images.length; i++) {
356
- expect(images[i]).toMatchImageSnapshot({
357
- customSnapshotIdentifier: `rotate-5p-180deg-page1-result-page${i + 1}`,
358
- });
359
- }
360
- });
361
- test('rotate: rotate pages 2 and 4 of 5p.pdf by 270 degrees', async () => {
362
- // Rotate the 2nd and 4th pages by 270 degrees
363
- const rotated = await (0, index_js_1.rotate)(fiveP, 270, [1, 3]);
364
- const images = await pdfToImages(rotated);
365
- for (let i = 0; i < images.length; i++) {
366
- expect(images[i]).toMatchImageSnapshot({
367
- customSnapshotIdentifier: `rotate-5p-270deg-pages1-3-result-page${i + 1}`,
368
- });
369
- }
370
- });
371
- //
372
- // move
373
- //
374
- test('move: move the 1st page (index:0) of 5p.pdf to the 3rd position (index:2)', async () => {
375
- const moved = await (0, index_js_1.move)(fiveP, { from: 0, to: 2 });
376
- const images = await pdfToImages(moved);
377
- for (let i = 0; i < images.length; i++) {
378
- expect(images[i]).toMatchImageSnapshot({
379
- customSnapshotIdentifier: `move-5p-page0-to-2-result-page${i + 1}`,
380
- });
381
- }
382
- });
383
- //
384
- // organize (single operation)
385
- //
386
- test('organize: single remove (remove the 2nd page)', async () => {
387
- const result = await (0, index_js_1.organize)(fiveP, [{ type: 'remove', data: { position: 1 } }]);
388
- const images = await pdfToImages(result);
389
- for (let i = 0; i < images.length; i++) {
390
- expect(images[i]).toMatchImageSnapshot({
391
- customSnapshotIdentifier: `organize-remove-only-result-page${i + 1}`,
392
- });
393
- }
394
- });
395
- test('organize: single insert (insert a.pdf at page 1)', async () => {
396
- const result = await (0, index_js_1.organize)(fiveP, [{ type: 'insert', data: { pdf: aPdf, position: 0 } }]);
397
- const images = await pdfToImages(result);
398
- for (let i = 0; i < images.length; i++) {
399
- expect(images[i]).toMatchImageSnapshot({
400
- customSnapshotIdentifier: `organize-insert-only-result-page${i + 1}`,
401
- });
402
- }
403
- });
404
- test('organize: single replace (replace 2nd page with a.pdf)', async () => {
405
- const result = await (0, index_js_1.organize)(fiveP, [{ type: 'replace', data: { pdf: aPdf, position: 1 } }]);
406
- const images = await pdfToImages(result);
407
- for (let i = 0; i < images.length; i++) {
408
- expect(images[i]).toMatchImageSnapshot({
409
- customSnapshotIdentifier: `organize-replace-only-result-page${i + 1}`,
410
- });
411
- }
412
- });
413
- test('organize: single rotate (rotate pages 1 and 3 by 90 degrees)', async () => {
414
- const result = await (0, index_js_1.organize)(fiveP, [
415
- { type: 'rotate', data: { position: 0, degrees: 90 } },
416
- { type: 'rotate', data: { position: 2, degrees: 90 } },
417
- ]);
418
- const images = await pdfToImages(result);
419
- for (let i = 0; i < images.length; i++) {
420
- expect(images[i]).toMatchImageSnapshot({
421
- customSnapshotIdentifier: `organize-rotate-only-result-page${i + 1}`,
422
- });
423
- }
424
- });
425
- test('organize: multiple operations (remove -> remove -> insert -> replace -> rotate -> rotate)', async () => {
426
- const insertPdf = aPdf;
427
- const replacePdf = bPdf;
428
- const result = await (0, index_js_1.organize)(fiveP, [
429
- { type: 'remove', data: { position: 1 } },
430
- { type: 'remove', data: { position: 3 } }, // 5 -> 3 pages
431
- { type: 'insert', data: { pdf: insertPdf, position: 1 } }, // 3 -> 4 pages
432
- { type: 'replace', data: { position: 2, pdf: replacePdf } }, // still 4 pages
433
- { type: 'rotate', data: { position: 0, degrees: 90 } },
434
- { type: 'rotate', data: { position: 3, degrees: 90 } },
435
- ]);
436
- const images = await pdfToImages(result);
437
- for (let i = 0; i < images.length; i++) {
438
- expect(images[i]).toMatchImageSnapshot({
439
- customSnapshotIdentifier: `organize-multiple-ops-result-page${i + 1}`,
440
- });
441
- }
442
- });
443
- //
444
- // organize - complex examples
445
- //
446
- test('organize: Remove -> Insert -> Move (check index changes)', async () => {
447
- // Remove the 2nd page (index:1) => 4 pages
448
- // Insert a.pdf at the 2nd page (index:1) => 5 pages
449
- // Move the newly inserted page (index:1) to the 1st page (index:0) => 5 pages
450
- const result = await (0, index_js_1.organize)(fiveP, [
451
- { type: 'remove', data: { position: 1 } },
452
- { type: 'insert', data: { pdf: aPdf, position: 1 } },
453
- { type: 'move', data: { from: 1, to: 0 } },
454
- ]);
455
- const images = await pdfToImages(result);
456
- for (let i = 0; i < images.length; i++) {
457
- expect(images[i]).toMatchImageSnapshot({
458
- customSnapshotIdentifier: `organize-composite-1-result-page${i + 1}`,
459
- });
460
- }
461
- });
462
- test('organize: Replace -> Rotate -> Remove', async () => {
463
- // Replace the 3rd page with a.pdf => 5 pages
464
- // Rotate the 3rd page by 180 => 5 pages
465
- // Remove the 1st page => 4 pages
466
- const result = await (0, index_js_1.organize)(fiveP, [
467
- { type: 'replace', data: { pdf: aPdf, position: 2 } },
468
- { type: 'rotate', data: { position: 2, degrees: 180 } },
469
- { type: 'remove', data: { position: 0 } },
470
- ]);
471
- const images = await pdfToImages(result);
472
- for (let i = 0; i < images.length; i++) {
473
- expect(images[i]).toMatchImageSnapshot({
474
- customSnapshotIdentifier: `organize-composite-2-result-page${i + 1}`,
475
- });
476
- }
477
- });
478
- test('organize: Insert -> Insert -> Move -> Move', async () => {
479
- // Insert a.pdf at the end (index:5) => 6 pages
480
- // Insert b.pdf at the start (index:0) => 7 pages
481
- // Move the 3rd page (index:2) to the 6th page (index:5) => 7 pages
482
- // Move the 4th page (index:3) to the 2nd page (index:1) => 7 pages
483
- const result = await (0, index_js_1.organize)(fiveP, [
484
- { type: 'insert', data: { pdf: aPdf, position: 5 } },
485
- { type: 'insert', data: { pdf: bPdf, position: 0 } },
486
- { type: 'move', data: { from: 2, to: 5 } },
487
- { type: 'move', data: { from: 3, to: 1 } },
488
- ]);
489
- const images = await pdfToImages(result);
490
- for (let i = 0; i < images.length; i++) {
491
- expect(images[i]).toMatchImageSnapshot({
492
- customSnapshotIdentifier: `organize-composite-3-result-page${i + 1}`,
493
- });
494
- }
495
- });
496
- test('organize: Rotate -> Rotate -> Remove', async () => {
497
- // Rotate the 2nd and 4th pages by 90 => 5 pages
498
- // Rotate the 3rd page by 270 => 5 pages
499
- // Remove the 1st page => 4 pages
500
- const result = await (0, index_js_1.organize)(fiveP, [
501
- { type: 'rotate', data: { position: 1, degrees: 90 } },
502
- { type: 'rotate', data: { position: 3, degrees: 90 } },
503
- { type: 'rotate', data: { position: 2, degrees: 270 } },
504
- { type: 'remove', data: { position: 0 } },
505
- ]);
506
- const images = await pdfToImages(result);
507
- for (let i = 0; i < images.length; i++) {
508
- expect(images[i]).toMatchImageSnapshot({
509
- customSnapshotIdentifier: `organize-composite-4-result-page${i + 1}`,
510
- });
511
- }
512
- });
513
- test('organize: Remove -> Insert -> Remove -> Insert (multiple times)', async () => {
514
- // Remove the 5th page (index:4) => 4 pages
515
- // Insert a.pdf at the 2nd page (index:1) => 5 pages
516
- // Remove the 1st page (index:0) => 4 pages
517
- // Insert b.pdf at the end (index:4) => 5 pages
518
- const result = await (0, index_js_1.organize)(fiveP, [
519
- { type: 'remove', data: { position: 4 } },
520
- { type: 'insert', data: { pdf: aPdf, position: 1 } },
521
- { type: 'remove', data: { position: 0 } },
522
- { type: 'insert', data: { pdf: bPdf, position: 4 } },
523
- ]);
524
- const images = await pdfToImages(result);
525
- for (let i = 0; i < images.length; i++) {
526
- expect(images[i]).toMatchImageSnapshot({
527
- customSnapshotIdentifier: `organize-composite-5-result-page${i + 1}`,
528
- });
529
- }
530
- });
531
- });
532
- //# sourceMappingURL=index.test.js.map