-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpdfTools.ts
More file actions
456 lines (412 loc) · 14.1 KB
/
pdfTools.ts
File metadata and controls
456 lines (412 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import { PDFDocument, degrees, rgb, StandardFonts, PageSizes } from 'pdf-lib';
import { PDF } from '@libpdf/core';
async function loadPdf(file: File): Promise<PDFDocument> {
const data = await file.arrayBuffer();
return PDFDocument.load(data, { ignoreEncryption: true });
}
function toBlob(bytes: Uint8Array): Blob {
return new Blob([bytes as any], { type: 'application/pdf' });
}
export async function mergePdfs(files: File[]): Promise<Blob> {
const merged = await PDFDocument.create();
for (const file of files) {
const src = await loadPdf(file);
const pages = await merged.copyPages(src, src.getPageIndices());
pages.forEach(p => merged.addPage(p));
}
return toBlob(await merged.save());
}
export async function splitPdf(file: File, ranges: [number, number][]): Promise<Blob[]> {
const src = await loadPdf(file);
const blobs: Blob[] = [];
for (const [start, end] of ranges) {
const doc = await PDFDocument.create();
const indices = Array.from({ length: end - start + 1 }, (_, i) => start - 1 + i);
const pages = await doc.copyPages(src, indices);
pages.forEach(p => doc.addPage(p));
blobs.push(toBlob(await doc.save()));
}
return blobs;
}
export async function rotatePdf(file: File, pageRotations: Record<number, number>): Promise<Blob> {
const doc = await loadPdf(file);
const pages = doc.getPages();
for (const [pageIdx, deg] of Object.entries(pageRotations)) {
const page = pages[Number(pageIdx)];
if (page) {
const current = page.getRotation().angle;
page.setRotation(degrees(current + deg));
}
}
return toBlob(await doc.save());
}
export async function removePages(file: File, pagesToRemove: number[]): Promise<Blob> {
const src = await loadPdf(file);
const doc = await PDFDocument.create();
const keepIndices = src.getPageIndices().filter(i => !pagesToRemove.includes(i));
const pages = await doc.copyPages(src, keepIndices);
pages.forEach(p => doc.addPage(p));
return toBlob(await doc.save());
}
export async function reorderPages(file: File, newOrder: number[]): Promise<Blob> {
const src = await loadPdf(file);
const doc = await PDFDocument.create();
const pages = await doc.copyPages(src, newOrder);
pages.forEach(p => doc.addPage(p));
return toBlob(await doc.save());
}
export type CompressionLevel = 'lossless' | 'balanced' | 'max';
export async function compressPdf(
file: File,
level: CompressionLevel = 'balanced',
onProgress?: (message: string) => void
): Promise<{
blob: Blob;
originalSize: number;
compressedSize: number;
savedPercent: number;
}> {
const originalSize = file.size;
const fileBuffer = await file.arrayBuffer();
return new Promise((resolve, reject) => {
const worker = new Worker(
new URL('../workers/ghostscript.worker.ts', import.meta.url),
{ type: 'module' }
);
worker.onmessage = (e: MessageEvent) => {
const { type, message, output } = e.data;
if (type === 'progress') {
onProgress?.(message);
} else if (type === 'done') {
worker.terminate();
const blob = new Blob([output], { type: 'application/pdf' });
const compressedSize = blob.size;
resolve({
blob,
originalSize,
compressedSize,
savedPercent: Math.max(0, ((originalSize - compressedSize) / originalSize) * 100),
});
} else if (type === 'error') {
worker.terminate();
reject(new Error(message));
}
};
worker.onerror = (err) => {
worker.terminate();
reject(new Error(err.message));
};
worker.postMessage({ fileBuffer, level }, [fileBuffer]);
});
}
export async function repairPdf(file: File): Promise<Blob> {
const doc = await loadPdf(file);
return toBlob(await doc.save());
}
export async function protectPdf(
file: File,
userPassword: string,
ownerPassword: string
): Promise<Blob> {
const data = await file.arrayBuffer();
const pdf = await PDF.load(new Uint8Array(data));
pdf.setProtection({
userPassword,
ownerPassword: ownerPassword || userPassword,
algorithm: 'AES-256' as any,
});
const encrypted = await pdf.save();
return new Blob([encrypted as any], { type: 'application/pdf' });
}
export async function unlockPdf(
file: File,
password: string
): Promise<Blob> {
const data = await file.arrayBuffer();
try {
const pdf = await PDF.load(new Uint8Array(data), { credentials: password } as any);
(pdf as any).removeProtection();
const unlocked = await pdf.save();
return new Blob([unlocked as any], { type: 'application/pdf' });
} catch {
throw new Error('Incorrect password or unsupported encryption.');
}
}
export async function addWatermark(file: File, text: string, opacity: number): Promise<Blob> {
const doc = await loadPdf(file);
const font = await doc.embedFont(StandardFonts.HelveticaBold);
const pages = doc.getPages();
for (const page of pages) {
const { width, height } = page.getSize();
const fontSize = Math.min(width, height) * 0.08;
page.drawText(text, {
x: width / 2 - (font.widthOfTextAtSize(text, fontSize) / 2),
y: height / 2,
size: fontSize,
font,
color: rgb(0.5, 0.5, 0.5),
opacity,
rotate: degrees(45),
});
}
return toBlob(await doc.save());
}
export async function addPageNumbers(file: File): Promise<Blob> {
const doc = await loadPdf(file);
const font = await doc.embedFont(StandardFonts.Helvetica);
const pages = doc.getPages();
pages.forEach((page, i) => {
const { width } = page.getSize();
const text = `${i + 1}`;
const fontSize = 10;
const textWidth = font.widthOfTextAtSize(text, fontSize);
page.drawText(text, {
x: width / 2 - textWidth / 2,
y: 20,
size: fontSize,
font,
color: rgb(0.3, 0.3, 0.3),
});
});
return toBlob(await doc.save());
}
export async function imagesToPdf(files: File[]): Promise<Blob> {
const doc = await PDFDocument.create();
for (const file of files) {
const data = await file.arrayBuffer();
const bytes = new Uint8Array(data);
let image;
if (file.type === 'image/png') {
image = await doc.embedPng(bytes);
} else {
image = await doc.embedJpg(bytes);
}
const page = doc.addPage([image.width, image.height]);
page.drawImage(image, { x: 0, y: 0, width: image.width, height: image.height });
}
return toBlob(await doc.save());
}
export async function addBlankPages(file: File, afterPages: number[]): Promise<Blob> {
const src = await loadPdf(file);
const doc = await PDFDocument.create();
const allPages = await doc.copyPages(src, src.getPageIndices());
for (let i = 0; i < allPages.length; i++) {
doc.addPage(allPages[i]);
if (afterPages.includes(i)) {
doc.addPage(PageSizes.A4);
}
}
return toBlob(await doc.save());
}
export async function cropPdf(file: File, margins: { top: number; right: number; bottom: number; left: number }): Promise<Blob> {
const doc = await loadPdf(file);
const pages = doc.getPages();
for (const page of pages) {
const { width, height } = page.getSize();
page.setCropBox(
margins.left,
margins.bottom,
width - margins.left - margins.right,
height - margins.top - margins.bottom
);
}
return toBlob(await doc.save());
}
async function getPdfjsLib() {
const pdfjsLib = await import('pdfjs-dist');
if (!pdfjsLib.GlobalWorkerOptions.workerSrc) {
try {
pdfjsLib.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.mjs', import.meta.url).href;
} catch {
pdfjsLib.GlobalWorkerOptions.workerSrc = `https://unpkg.com/pdfjs-dist@${pdfjsLib.version}/build/pdf.worker.min.mjs`;
}
}
return pdfjsLib;
}
export async function pdfToText(file: File): Promise<string> {
const pdfjsLib = await getPdfjsLib();
const data = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data }).promise;
let fullText = '';
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const content = await page.getTextContent();
const strings = content.items.map((item: any) => item.str).join(' ');
fullText += `--- Page ${i} ---\n${strings}\n\n`;
}
return fullText;
}
export async function pdfToImages(file: File): Promise<Blob[]> {
const pdfjsLib = await getPdfjsLib();
const data = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data }).promise;
const blobs: Blob[] = [];
for (let i = 1; i <= pdf.numPages; i++) {
const page = await pdf.getPage(i);
const viewport = page.getViewport({ scale: 2 });
const canvas = document.createElement('canvas');
canvas.width = viewport.width;
canvas.height = viewport.height;
const ctx = canvas.getContext('2d')!;
const renderTask = page.render({ canvasContext: ctx, viewport } as any);
await (renderTask.promise ?? renderTask);
const blob = await new Promise<Blob>((resolve, reject) => {
canvas.toBlob(b => {
if (b) resolve(b);
else reject(new Error('Failed to create image blob'));
}, 'image/png');
});
blobs.push(blob);
}
return blobs;
}
export async function removeMetadata(file: File): Promise<{ blob: Blob; removed: string[] }> {
const doc = await loadPdf(file);
const removed: string[] = [];
if (doc.getTitle()) { removed.push(`Title: "${doc.getTitle()}"`); }
if (doc.getAuthor()) { removed.push(`Author: "${doc.getAuthor()}"`); }
if (doc.getSubject()) { removed.push(`Subject: "${doc.getSubject()}"`); }
if (doc.getCreator()) { removed.push(`Creator: "${doc.getCreator()}"`); }
if (doc.getProducer()) { removed.push(`Producer: "${doc.getProducer()}"`); }
if (doc.getKeywords()) { removed.push(`Keywords: "${doc.getKeywords()}"`); }
if (doc.getCreationDate()) { removed.push(`Created: "${doc.getCreationDate()}"`); }
if (doc.getModificationDate()) { removed.push(`Modified: "${doc.getModificationDate()}"`); }
doc.setTitle('');
doc.setAuthor('');
doc.setSubject('');
doc.setCreator('');
doc.setProducer('');
doc.setKeywords([]);
doc.setCreationDate(new Date(0));
doc.setModificationDate(new Date(0));
const blob = toBlob(await doc.save());
return { blob, removed };
}
export async function pdfToZip(file: File): Promise<Blob> {
const imageBlobs = await pdfToImages(file);
const JSZip = (await import('jszip')).default;
const zip = new JSZip();
imageBlobs.forEach((blob, i) => {
const pageNum = String(i + 1).padStart(3, '0');
zip.file(`page-${pageNum}.png`, blob);
});
return await zip.generateAsync({ type: 'blob' });
}
export async function signPdf(
file: File,
signatureDataUrl: string,
pageIndex: number,
position: { x: number; y: number; width: number; height: number }
): Promise<Blob> {
const doc = await loadPdf(file);
const pages = doc.getPages();
const page = pages[pageIndex];
const base64 = signatureDataUrl.split(',')[1];
const pngBytes = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
const image = await doc.embedPng(pngBytes);
page.drawImage(image, {
x: position.x,
y: position.y,
width: position.width,
height: position.height,
});
return toBlob(await doc.save());
}
export async function fillAndFlattenForm(
file: File,
fieldValues: Record<string, string>
): Promise<Blob> {
const doc = await loadPdf(file);
const form = doc.getForm();
const fields = form.getFields();
for (const field of fields) {
const name = field.getName();
const value = fieldValues[name];
if (value === undefined) continue;
try {
if (field.constructor.name === 'PDFTextField') {
(field as any).setText(value);
} else if (field.constructor.name === 'PDFCheckBox') {
if (value === 'true') (field as any).check();
else (field as any).uncheck();
} else if (field.constructor.name === 'PDFDropdown') {
(field as any).select(value);
} else if (field.constructor.name === 'PDFRadioGroup') {
(field as any).select(value);
}
} catch (e) {
console.warn('Could not set field:', name, e);
}
}
form.flatten();
return toBlob(await doc.save());
}
export async function detectFormFields(
file: File
): Promise<Array<{ name: string; type: string; options?: string[] }>> {
const doc = await loadPdf(file);
const form = doc.getForm();
const fields = form.getFields();
return fields.map(field => {
const type = field.constructor.name
.replace('PDF', '')
.replace('Field', '')
.replace('Group', '');
const result: { name: string; type: string; options?: string[] } = {
name: field.getName(),
type,
};
if (type === 'Dropdown' || type === 'RadioGroup') {
result.options = (field as any).getOptions?.() ?? [];
}
return result;
});
}
export type RedactionArea = {
page: number;
x: number;
y: number;
width: number;
height: number;
};
export async function redactPdf(
file: File,
redactions: RedactionArea[],
onProgress?: (message: string) => void
): Promise<Blob> {
const fileBuffer = await file.arrayBuffer();
return new Promise((resolve, reject) => {
const worker = new Worker(
new URL('../workers/mupdf.worker.ts', import.meta.url),
{ type: 'module' }
);
worker.onmessage = (e: MessageEvent) => {
const { type, message, output } = e.data;
if (type === 'progress') {
onProgress?.(message);
} else if (type === 'done') {
worker.terminate();
resolve(new Blob([output], { type: 'application/pdf' }));
} else if (type === 'error') {
worker.terminate();
reject(new Error(message));
}
};
worker.onerror = (err) => {
worker.terminate();
reject(new Error(err.message));
};
worker.postMessage({ fileBuffer, redactions }, [fileBuffer]);
});
}
export async function getPageCount(file: File): Promise<number> {
const doc = await loadPdf(file);
return doc.getPageCount();
}
export function parseSplitRanges(input: string): [number, number][] {
return input.split(',').map(r => {
const parts = r.trim().split('-').map(Number);
if (parts.length === 1) return [parts[0], parts[0]] as [number, number];
return [parts[0], parts[1]] as [number, number];
}).filter(([a, b]) => !isNaN(a) && !isNaN(b) && a > 0 && b >= a);
}