refactoring, linting, formatting
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
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);
|
||||
}
|
||||
|
||||
function pdfBytesToArrayBuffer(bytes: Uint8Array): ArrayBuffer {
|
||||
const buffer = new ArrayBuffer(bytes.byteLength);
|
||||
new Uint8Array(buffer).set(bytes);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function pdfBytesToBlob(bytes: Uint8Array): Blob {
|
||||
return new Blob([pdfBytesToArrayBuffer(bytes)], { type: "application/pdf" });
|
||||
}
|
||||
|
||||
export async function loadPdfFromFile(file: File): Promise<PdfFile> {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const doc = await PDFDocument.load(arrayBuffer);
|
||||
@@ -21,10 +31,10 @@ 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);
|
||||
const baseDoc = basePdf.doc ?? (await PDFDocument.load(basePdf.arrayBuffer));
|
||||
const newDoc = newPdf.doc ?? (await PDFDocument.load(newPdf.arrayBuffer));
|
||||
|
||||
const mergedDoc = await PDFDocument.create();
|
||||
|
||||
@@ -35,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) {
|
||||
@@ -53,11 +63,10 @@ export async function mergePdfFiles(
|
||||
}
|
||||
|
||||
const bytes = await mergedDoc.save();
|
||||
const buffer = new ArrayBuffer(bytes.byteLength);
|
||||
new Uint8Array(buffer).set(bytes);
|
||||
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(),
|
||||
@@ -69,7 +78,7 @@ export async function mergePdfFiles(
|
||||
}
|
||||
|
||||
export async function splitIntoSinglePages(
|
||||
pdf: PdfFile
|
||||
pdf: PdfFile,
|
||||
): Promise<SplitResult[]> {
|
||||
const { doc, name } = pdf;
|
||||
|
||||
@@ -99,10 +108,10 @@ export async function splitIntoSinglePages(
|
||||
if (modificationDate) newDoc.setModificationDate(modificationDate);
|
||||
|
||||
const bytes = await newDoc.save();
|
||||
const blob = new Blob([bytes], { type: 'application/pdf' });
|
||||
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,
|
||||
@@ -114,10 +123,7 @@ export async function splitIntoSinglePages(
|
||||
return results;
|
||||
}
|
||||
|
||||
export async function extractRange(
|
||||
pdf: PdfFile,
|
||||
range: Range
|
||||
): Promise<Blob> {
|
||||
export async function extractRange(pdf: PdfFile, range: Range): Promise<Blob> {
|
||||
const { doc } = pdf;
|
||||
const pageCount = doc.getPageCount();
|
||||
|
||||
@@ -125,7 +131,7 @@ export async function extractRange(
|
||||
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();
|
||||
@@ -136,7 +142,7 @@ export async function extractRange(
|
||||
copiedPages.forEach((p) => newDoc.addPage(p));
|
||||
|
||||
const bytes = await newDoc.save();
|
||||
return new Blob([bytes], { type: 'application/pdf' });
|
||||
return pdfBytesToBlob(bytes);
|
||||
}
|
||||
|
||||
export async function mergePdfs(pdfs: PdfFile[]): Promise<Blob> {
|
||||
@@ -150,26 +156,26 @@ export async function mergePdfs(pdfs: PdfFile[]): Promise<Blob> {
|
||||
}
|
||||
|
||||
const bytes = await newDoc.save();
|
||||
return new Blob([bytes], { type: 'application/pdf' });
|
||||
return pdfBytesToBlob(bytes);
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -180,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));
|
||||
}
|
||||
|
||||
@@ -188,13 +194,13 @@ export async function exportPages(
|
||||
});
|
||||
|
||||
const bytes = await newDoc.save();
|
||||
return new Blob([bytes], { type: 'application/pdf' });
|
||||
return pdfBytesToBlob(bytes);
|
||||
}
|
||||
|
||||
export async function exportReordered(
|
||||
pdf: PdfFile,
|
||||
order: number[],
|
||||
rotations?: Record<number, number>
|
||||
rotations?: Record<number, number>,
|
||||
): Promise<Blob> {
|
||||
return exportPages(
|
||||
pdf,
|
||||
@@ -202,6 +208,6 @@ export async function exportReordered(
|
||||
id: String(sourcePageIndex),
|
||||
sourcePageIndex,
|
||||
rotation: rotations?.[sourcePageIndex] ?? 0,
|
||||
}))
|
||||
})),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user