Roadmap, robust page refs, copy behaviour

This commit is contained in:
2026-05-16 02:37:20 +02:00
parent a649ede010
commit 2461cf3d64
9 changed files with 1588 additions and 744 deletions

View File

@@ -1,5 +1,5 @@
import { PDFDocument, degrees } from 'pdf-lib';
import type { PdfFile, SplitResult, Range } from './pdfTypes';
import type { PdfFile, PageRef, SplitResult, Range } from './pdfTypes';
function createId() {
return Math.random().toString(36).slice(2);
@@ -53,19 +53,18 @@ export async function mergePdfFiles(
}
const bytes = await mergedDoc.save();
const buffer = bytes.buffer.slice(
bytes.byteOffset,
bytes.byteOffset + bytes.byteLength
);
const buffer = new ArrayBuffer(bytes.byteLength);
new Uint8Array(buffer).set(bytes);
const baseName = basePdf.name.replace(/\.pdf$/i, '');
const newName = newPdf.name.replace(/\.pdf$/i, '');
return {
id: createId(),
name: `${baseName}_plus_${newName}.pdf`,
arrayBuffer: buffer,
pageCount: mergedDoc.getPageCount(),
doc: mergedDoc, // 👈 important
doc: mergedDoc,
};
}
@@ -93,7 +92,7 @@ export async function splitIntoSinglePages(
if (title) newDoc.setTitle(title);
if (author) newDoc.setAuthor(author);
if (subject) newDoc.setSubject(subject);
if (keywords) newDoc.setKeywords(keywords);
if (keywords) newDoc.setKeywords([keywords]);
if (producer) newDoc.setProducer(producer);
if (creator) newDoc.setCreator(creator);
if (creationDate) newDoc.setCreationDate(creationDate);
@@ -154,30 +153,32 @@ export async function mergePdfs(pdfs: PdfFile[]): Promise<Blob> {
return new Blob([bytes], { type: 'application/pdf' });
}
export async function exportReordered(
export async function exportPages(
pdf: PdfFile,
order: number[],
rotations?: Record<number, number>
pages: PageRef[]
): Promise<Blob> {
const { doc } = pdf;
const pageCount = doc.getPageCount();
if (order.length === 0) {
throw new Error('Order must contain at least one page');
if (pages.length === 0) {
throw new Error('Pages must contain at least one page');
}
if (order.some((i) => i < 0 || i >= pageCount)) {
throw new Error('Order contains invalid page indices');
if (
pages.some(
(page) => page.sourcePageIndex < 0 || page.sourcePageIndex >= pageCount
)
) {
throw new Error('Pages contain invalid source page indices');
}
const newDoc = await PDFDocument.create();
const indices = [...order];
const indices = pages.map((page) => page.sourcePageIndex);
const copiedPages = await newDoc.copyPages(doc, indices);
copiedPages.forEach((page, idx) => {
const originalIndex = indices[idx];
const angle = rotations?.[originalIndex];
const angle = pages[idx].rotation;
if (typeof angle === 'number' && angle % 360 !== 0) {
page.setRotation(degrees(angle));
@@ -189,3 +190,18 @@ export async function exportReordered(
const bytes = await newDoc.save();
return new Blob([bytes], { type: 'application/pdf' });
}
export async function exportReordered(
pdf: PdfFile,
order: number[],
rotations?: Record<number, number>
): Promise<Blob> {
return exportPages(
pdf,
order.map((sourcePageIndex) => ({
id: String(sourcePageIndex),
sourcePageIndex,
rotation: rotations?.[sourcePageIndex] ?? 0,
}))
);
}