mostly formatting, dependency fix

This commit is contained in:
2026-05-17 02:39:32 +02:00
parent a5dc70aabf
commit cf9a0dd0b7
32 changed files with 837 additions and 836 deletions

View File

@@ -1,5 +1,5 @@
import { PDFDocument, degrees } from "pdf-lib";
import type { PdfFile, PageRef, SplitResult, Range } from "./pdfTypes";
import { PDFDocument, degrees } from 'pdf-lib';
import type { PdfFile, PageRef, SplitResult, Range } from './pdfTypes';
function createId() {
return Math.random().toString(36).slice(2);
@@ -12,7 +12,7 @@ function pdfBytesToArrayBuffer(bytes: Uint8Array): ArrayBuffer {
}
function pdfBytesToBlob(bytes: Uint8Array): Blob {
return new Blob([pdfBytesToArrayBuffer(bytes)], { type: "application/pdf" });
return new Blob([pdfBytesToArrayBuffer(bytes)], { type: 'application/pdf' });
}
export async function loadPdfFromFile(file: File): Promise<PdfFile> {
@@ -31,7 +31,7 @@ export async function loadPdfFromFile(file: File): Promise<PdfFile> {
export async function mergePdfFiles(
basePdf: PdfFile,
newPdf: PdfFile,
insertAt: number,
insertAt: number
): Promise<PdfFile> {
const baseDoc = basePdf.doc ?? (await PDFDocument.load(basePdf.arrayBuffer));
const newDoc = newPdf.doc ?? (await PDFDocument.load(newPdf.arrayBuffer));
@@ -45,11 +45,11 @@ export async function mergePdfFiles(
const basePages = await mergedDoc.copyPages(
baseDoc,
Array.from({ length: basePageCount }, (_, i) => i),
Array.from({ length: basePageCount }, (_, i) => i)
);
const newPages = await mergedDoc.copyPages(
newDoc,
Array.from({ length: newPageCount }, (_, i) => i),
Array.from({ length: newPageCount }, (_, i) => i)
);
for (let i = 0; i < clampedInsertAt; i += 1) {
@@ -65,8 +65,8 @@ export async function mergePdfFiles(
const bytes = await mergedDoc.save();
const buffer = pdfBytesToArrayBuffer(bytes);
const baseName = basePdf.name.replace(/\.pdf$/i, "");
const newName = newPdf.name.replace(/\.pdf$/i, "");
const baseName = basePdf.name.replace(/\.pdf$/i, '');
const newName = newPdf.name.replace(/\.pdf$/i, '');
return {
id: createId(),
@@ -78,7 +78,7 @@ export async function mergePdfFiles(
}
export async function splitIntoSinglePages(
pdf: PdfFile,
pdf: PdfFile
): Promise<SplitResult[]> {
const { doc, name } = pdf;
@@ -110,8 +110,8 @@ export async function splitIntoSinglePages(
const bytes = await newDoc.save();
const blob = pdfBytesToBlob(bytes);
const base = name.replace(/\.pdf$/i, "");
const filename = `${base}_page_${String(i + 1).padStart(3, "0")}.pdf`;
const base = name.replace(/\.pdf$/i, '');
const filename = `${base}_page_${String(i + 1).padStart(3, '0')}.pdf`;
results.push({
pageIndex: i,
@@ -131,7 +131,7 @@ export async function extractRange(pdf: PdfFile, range: Range): Promise<Blob> {
const toIndex = Math.min(pageCount - 1, range.to - 1);
if (fromIndex > toIndex) {
throw new Error("Invalid range: from > to");
throw new Error('Invalid range: from > to');
}
const newDoc = await PDFDocument.create();
@@ -161,21 +161,21 @@ export async function mergePdfs(pdfs: PdfFile[]): Promise<Blob> {
export async function exportPages(
pdf: PdfFile,
pages: PageRef[],
pages: PageRef[]
): Promise<Blob> {
const { doc } = pdf;
const pageCount = doc.getPageCount();
if (pages.length === 0) {
throw new Error("Pages must contain at least one page");
throw new Error('Pages must contain at least one page');
}
if (
pages.some(
(page) => page.sourcePageIndex < 0 || page.sourcePageIndex >= pageCount,
(page) => page.sourcePageIndex < 0 || page.sourcePageIndex >= pageCount
)
) {
throw new Error("Pages contain invalid source page indices");
throw new Error('Pages contain invalid source page indices');
}
const newDoc = await PDFDocument.create();
@@ -186,7 +186,7 @@ export async function exportPages(
copiedPages.forEach((page, idx) => {
const angle = pages[idx].rotation;
if (typeof angle === "number" && angle % 360 !== 0) {
if (typeof angle === 'number' && angle % 360 !== 0) {
page.setRotation(degrees(angle));
}
@@ -200,7 +200,7 @@ export async function exportPages(
export async function exportReordered(
pdf: PdfFile,
order: number[],
rotations?: Record<number, number>,
rotations?: Record<number, number>
): Promise<Blob> {
return exportPages(
pdf,
@@ -208,6 +208,6 @@ export async function exportReordered(
id: String(sourcePageIndex),
sourcePageIndex,
rotation: rotations?.[sourcePageIndex] ?? 0,
})),
}))
);
}