Migrated more functions to use PdfFile
This commit is contained in:
@@ -14,5 +14,5 @@ export async function mergePDFs(files: PdfFile[]): Promise<PdfFile> {
|
||||
copiedPages.forEach((page) => mergedPdf.addPage(page));
|
||||
}
|
||||
|
||||
return fromPdfLib(mergedPdf);
|
||||
return fromPdfLib(mergedPdf, files[0].filename);
|
||||
};
|
||||
@@ -1,19 +1,26 @@
|
||||
|
||||
import { PDFDocument, ParseSpeeds, degrees } from 'pdf-lib';
|
||||
|
||||
export async function rotatePages(snapshot: string | Uint8Array | ArrayBuffer, rotation: number): Promise<Uint8Array> {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
import { degrees } from 'pdf-lib';
|
||||
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||
|
||||
export async function rotatePages(file: PdfFile, rotation: number|number[]): Promise<PdfFile> {
|
||||
const pdfDoc = await file.getAsPdfLib();
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
// Change page size
|
||||
page.setRotation(degrees(rotation))
|
||||
});
|
||||
if (Array.isArray(rotation)) {
|
||||
if (rotation.length != pages.length) {
|
||||
throw new Error(`Number of given rotations '${rotation.length}' is not the same as the number of pages '${pages.length}'`)
|
||||
}
|
||||
for (let i=0; i<rotation.length; i++) {
|
||||
const oldRotation = pages[i].getRotation().angle
|
||||
pages[i].setRotation(degrees(oldRotation + rotation[i]))
|
||||
}
|
||||
} else {
|
||||
pages.forEach(page => {
|
||||
// Change page size
|
||||
const oldRotation = page.getRotation().angle
|
||||
page.setRotation(degrees(oldRotation + rotation))
|
||||
});
|
||||
}
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
};
|
||||
@@ -1,30 +1,36 @@
|
||||
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
||||
|
||||
export async function scaleContent(snapshot: string | Uint8Array | ArrayBuffer, scaleFactor: number): Promise<Uint8Array> {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
import { PDFPage } from 'pdf-lib';
|
||||
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||
|
||||
export async function scaleContent(file: PdfFile, scaleFactor: number|number[]): Promise<PdfFile> {
|
||||
const pdfDoc = await file.getAsPdfLib();
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
const width = page.getWidth();
|
||||
const height = page.getHeight();
|
||||
|
||||
// Scale content
|
||||
page.scaleContent(scaleFactor, scaleFactor);
|
||||
const scaled_diff = {
|
||||
width: Math.round(width - scaleFactor * width),
|
||||
height: Math.round(height - scaleFactor * height),
|
||||
};
|
||||
if (Array.isArray(scaleFactor)) {
|
||||
if (scaleFactor.length != pages.length) {
|
||||
throw new Error(`Number of given scale factors '${scaleFactor.length}' is not the same as the number of pages '${pages.length}'`)
|
||||
}
|
||||
for (let i=0; i<scaleFactor.length; i++) {
|
||||
scalePage(pages[i], scaleFactor[i]);
|
||||
}
|
||||
} else {
|
||||
pages.forEach(page => scalePage(page, scaleFactor));
|
||||
}
|
||||
|
||||
// Center content in new page format
|
||||
page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
};
|
||||
|
||||
});
|
||||
function scalePage(page: PDFPage, scaleFactor: number) {
|
||||
const width = page.getWidth();
|
||||
const height = page.getHeight();
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
// Scale content
|
||||
page.scaleContent(scaleFactor, scaleFactor);
|
||||
const scaled_diff = {
|
||||
width: Math.round(width - scaleFactor * width),
|
||||
height: Math.round(height - scaleFactor * height),
|
||||
};
|
||||
|
||||
// Center content in new page format
|
||||
page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
|
||||
}
|
||||
@@ -1,26 +1,50 @@
|
||||
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
||||
|
||||
export async function scalePage(snapshot: string | Uint8Array | ArrayBuffer, pageSize: {width:number,height:number}): Promise<Uint8Array> {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Fastest,
|
||||
});
|
||||
|
||||
const new_size = pageSize;
|
||||
import { PDFPage } from 'pdf-lib';
|
||||
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||
|
||||
export async function scalePage(file: PdfFile, pageSize: {width?:number,height?:number}|{width?:number,height?:number}[]): Promise<PdfFile> {
|
||||
const pdfDoc = await file.getAsPdfLib();
|
||||
const pages = pdfDoc.getPages();
|
||||
|
||||
pages.forEach(page => {
|
||||
// Change page size
|
||||
page.setSize(new_size.width, new_size.height);
|
||||
});
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
if (Array.isArray(pageSize)) {
|
||||
if (pageSize.length != pages.length) {
|
||||
throw new Error(`Number of given sizes '${pageSize.length}' is not the same as the number of pages '${pages.length}'`)
|
||||
}
|
||||
for (let i=0; i<pageSize.length; i++) {
|
||||
resize(pages[i], pageSize[i]);
|
||||
}
|
||||
} else {
|
||||
pages.forEach(page => resize(page, pageSize));
|
||||
}
|
||||
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
};
|
||||
|
||||
export const PageSize = {
|
||||
function resize(page: PDFPage, newSize: {width?:number,height?:number}) {
|
||||
const calculatedSize = calculateSize(page, newSize);
|
||||
page.setSize(calculatedSize.width, calculatedSize.height);
|
||||
|
||||
const xRatio = calculatedSize.width / page.getWidth();
|
||||
const yRatio = calculatedSize.height / page.getHeight();
|
||||
page.scaleContent(xRatio, yRatio);
|
||||
}
|
||||
|
||||
function calculateSize(page: PDFPage, newSize: {width?:number,height?:number}): {width:number,height:number} {
|
||||
if (!newSize.width && !newSize.height){
|
||||
throw new Error(`Sizes '${newSize}' cannot have null width and null height`);
|
||||
} else if (!newSize.width && newSize.height) {
|
||||
const oldSize = page.getSize();
|
||||
const ratio = oldSize.width / oldSize.height;
|
||||
return { width: newSize.height * ratio, height: newSize.height };
|
||||
} else if (newSize.width && !newSize.height) {
|
||||
const oldSize = page.getSize();
|
||||
const ratio = oldSize.height / oldSize.width;
|
||||
return { width: newSize.width, height: newSize.width * ratio };
|
||||
}
|
||||
return { width: newSize.width!, height: newSize.height! };
|
||||
}
|
||||
|
||||
export const PageSize = Object.freeze({
|
||||
a4: {
|
||||
width: 594.96,
|
||||
height: 841.92
|
||||
@@ -29,4 +53,4 @@ export const PageSize = {
|
||||
width: 612,
|
||||
height: 792
|
||||
}
|
||||
};
|
||||
});
|
||||
@@ -1,12 +1,9 @@
|
||||
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import * as PDFJS from 'pdfjs-dist';
|
||||
import jsQR from "jsqr";
|
||||
|
||||
import { detectEmptyPages } from "./common/detectEmptyPages.js";
|
||||
import { getImagesOnPage } from "./common/getImagesOnPage.js";
|
||||
import { selectPages } from "./subDocumentFunctions";
|
||||
import { TypedArray, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api.js';
|
||||
import { PdfFile } from '../wrappers/PdfFile.js';
|
||||
|
||||
export async function splitOn(
|
||||
|
||||
@@ -48,7 +48,7 @@ export async function selectPages(file: PdfFile, pagesToExtractArray: number[]):
|
||||
subDocument.addPage(copiedPages[i]);
|
||||
}
|
||||
|
||||
return fromPdfLib(subDocument);
|
||||
return fromPdfLib(subDocument, file.filename);
|
||||
}
|
||||
|
||||
export async function removePages(file: PdfFile, pagesToRemoveArray: number[]): Promise<PdfFile> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
|
||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
||||
|
||||
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||
|
||||
export type Metadata = {
|
||||
deleteAll?: boolean, // Delete all metadata if set to true
|
||||
@@ -13,21 +13,11 @@ export type Metadata = {
|
||||
subject?: string, // The subject of the document
|
||||
title?: string, // The title of the document
|
||||
//trapped?: string, // The trapped status of the document
|
||||
//allRequestParams?: object, // Map list of key and value of custom parameters. Note these must start with customKey and customValue if they are non-standard
|
||||
//allRequestParams?: {[key: string]: [key: string]}, // Map list of key and value of custom parameters. Note these must start with customKey and customValue if they are non-standard
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Uint16Array} snapshot
|
||||
* @param {Metadata} metadata - Set property to null or "" to clear, undefined properties will be skipped.
|
||||
* @returns Promise<Uint8Array>
|
||||
*/
|
||||
export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer, metadata: Metadata): Promise<Uint8Array> {
|
||||
// Load the original PDF file
|
||||
const pdfDoc = await PDFDocument.load(snapshot, {
|
||||
parseSpeed: ParseSpeeds.Slow,
|
||||
updateMetadata: false,
|
||||
});
|
||||
export async function updateMetadata(file: PdfFile, metadata: Metadata|null): Promise<PdfFile> {
|
||||
const pdfDoc = await file.getAsPdfLib();
|
||||
|
||||
if (!metadata || metadata.deleteAll) {
|
||||
pdfDoc.setAuthor("");
|
||||
@@ -40,7 +30,7 @@ export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer
|
||||
pdfDoc.setTitle("")
|
||||
}
|
||||
if (!metadata) {
|
||||
return pdfDoc.save();
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
}
|
||||
|
||||
if(metadata.author)
|
||||
@@ -62,6 +52,5 @@ export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer
|
||||
|
||||
// TODO add trapped and custom metadata. May need another library
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
};
|
||||
|
||||
@@ -7,12 +7,13 @@ export class PdfFile {
|
||||
byteArray: Uint8Array | null;
|
||||
pdfLib: PDFDocument | null;
|
||||
pdfJs: PDFDocumentProxy | null;
|
||||
filename?: string;
|
||||
filename: string;
|
||||
|
||||
constructor() {
|
||||
this.byteArray = null;
|
||||
this.pdfLib = null;
|
||||
this.pdfJs = null;
|
||||
this.filename = "";
|
||||
}
|
||||
|
||||
async convertToByteArrayFile(): Promise<PdfFile> {
|
||||
@@ -57,22 +58,22 @@ export class PdfFile {
|
||||
}
|
||||
}
|
||||
|
||||
export function fromMulterFile(value: Express.Multer.File, filename?: string): PdfFile {
|
||||
return fromUint8Array(value.buffer, filename)
|
||||
export function fromMulterFile(value: Express.Multer.File): PdfFile {
|
||||
return fromUint8Array(value.buffer, value.originalname)
|
||||
}
|
||||
export function fromUint8Array(value: Uint8Array, filename?: string): PdfFile {
|
||||
export function fromUint8Array(value: Uint8Array, filename: string): PdfFile {
|
||||
const out = new PdfFile();
|
||||
out.byteArray = value;
|
||||
out.filename = filename;
|
||||
return out;
|
||||
}
|
||||
export function fromPdfLib(value: PDFDocument, filename?: string): PdfFile {
|
||||
export function fromPdfLib(value: PDFDocument, filename: string): PdfFile {
|
||||
const out = new PdfFile();
|
||||
out.pdfLib = value;
|
||||
out.filename = filename;
|
||||
return out;
|
||||
}
|
||||
export function fromPdfJs(value: PDFDocumentProxy, filename?: string): PdfFile {
|
||||
export function fromPdfJs(value: PDFDocumentProxy, filename: string): PdfFile {
|
||||
const out = new PdfFile();
|
||||
out.pdfJs = value;
|
||||
out.filename = filename;
|
||||
|
||||
Reference in New Issue
Block a user