mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2024-12-21 19:08:24 +01:00
Migrated more functions to use PdfFile
This commit is contained in:
parent
0d915fcc33
commit
8018947353
@ -1,6 +1,7 @@
|
||||
|
||||
import Operations from '../../utils/pdf-operations';
|
||||
import { respondWithBinaryPdf, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
|
||||
import { respondWithPdfFile, response_mustHaveExactlyOneFile } from '../../utils/endpoint-utils';
|
||||
import { PdfFile, fromMulterFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile'
|
||||
|
||||
import express, { Request, Response } from 'express';
|
||||
const router = express.Router();
|
||||
@ -8,6 +9,36 @@ import multer from 'multer';
|
||||
const upload = multer();
|
||||
import Joi from 'joi';
|
||||
|
||||
router.post('/merge-pdfs', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
||||
const schema = Joi.object({
|
||||
deleteAll: Joi.string(),
|
||||
author: Joi.string(),
|
||||
creationDate: Joi.string(),
|
||||
creator: Joi.string(),
|
||||
keywords: Joi.string(),
|
||||
modificationDate: Joi.string(),
|
||||
producer: Joi.string(),
|
||||
subject: Joi.string(),
|
||||
title: Joi.string(),
|
||||
trapped: Joi.string(),
|
||||
allRequestParams: Joi.object().pattern(Joi.string(), Joi.string()),
|
||||
}).required();
|
||||
const { error, value } = schema.validate(req.body);
|
||||
if (error) {
|
||||
res.status(400).send(error.details);
|
||||
return;
|
||||
}
|
||||
if (!req.file) {
|
||||
response_mustHaveExactlyOneFile(res);
|
||||
return;
|
||||
}
|
||||
|
||||
const arrayFile = fromMulterFile(req.file);
|
||||
const processed = await Operations.updateMetadata(arrayFile, value)
|
||||
const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
|
||||
respondWithPdfFile(res, processed);
|
||||
});
|
||||
|
||||
router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
||||
const schema = Joi.object({
|
||||
angle: Joi.number().required()
|
||||
@ -22,9 +53,10 @@ router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request
|
||||
return;
|
||||
}
|
||||
|
||||
const rotated = await Operations.rotatePages(req.file.buffer, value.angle)
|
||||
const newFilename = appendToFilename(req.file.originalname, '_rotated');
|
||||
respondWithBinaryPdf(res, rotated, newFilename);
|
||||
const arrayFile = fromMulterFile(req.file);
|
||||
const rotated = await Operations.rotatePages(arrayFile, value.angle)
|
||||
rotated.filename = appendToFilename(arrayFile.filename, '_rotated');
|
||||
respondWithPdfFile(res, rotated);
|
||||
});
|
||||
|
||||
router.post('/update-metadata', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
||||
@ -51,9 +83,10 @@ router.post('/update-metadata', upload.single("pdfFile"), async function(req: Re
|
||||
return;
|
||||
}
|
||||
|
||||
const processed = await Operations.updateMetadata(req.file.buffer, value)
|
||||
const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
|
||||
respondWithBinaryPdf(res, processed, newFilename);
|
||||
const arrayFile = fromMulterFile(req.file);
|
||||
const processed = await Operations.updateMetadata(arrayFile, value)
|
||||
processed.filename = appendToFilename(arrayFile.filename, '_edited-metadata');
|
||||
respondWithPdfFile(res, processed);
|
||||
});
|
||||
|
||||
/**
|
||||
|
@ -1,13 +1,15 @@
|
||||
|
||||
import express, { Request, Response } from 'express';
|
||||
import { Response } from 'express';
|
||||
import { PdfFile } from '@stirling-pdf/shared-operations/wrappers/PdfFile'
|
||||
|
||||
export function respondWithBinaryPdf(res: Response, buffer: Uint8Array, filename: string) {
|
||||
export async function respondWithPdfFile(res: Response, file: PdfFile): Promise<void> {
|
||||
const byteFile = await file.convertToByteArrayFile();
|
||||
res.writeHead(200, {
|
||||
'Content-Type': "application/pdf",
|
||||
'Content-disposition': 'attachment;filename=' + filename,
|
||||
'Content-Length': buffer.length
|
||||
'Content-disposition': 'attachment;filename=' + byteFile.filename,
|
||||
'Content-Length': byteFile.byteArray?.length
|
||||
});
|
||||
res.end(buffer)
|
||||
res.end(byteFile.byteArray)
|
||||
}
|
||||
|
||||
export function response_mustHaveExactlyOneFile(res: Response): void {
|
||||
|
@ -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();
|
||||
|
||||
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
|
||||
page.setRotation(degrees(rotation))
|
||||
const oldRotation = page.getRotation().angle
|
||||
page.setRotation(degrees(oldRotation + rotation))
|
||||
});
|
||||
}
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
};
|
@ -1,15 +1,26 @@
|
||||
|
||||
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 => {
|
||||
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));
|
||||
}
|
||||
|
||||
return fromPdfLib(pdfDoc, file.filename);
|
||||
};
|
||||
|
||||
function scalePage(page: PDFPage, scaleFactor: number) {
|
||||
const width = page.getWidth();
|
||||
const height = page.getHeight();
|
||||
|
||||
@ -22,9 +33,4 @@ export async function scaleContent(snapshot: string | Uint8Array | ArrayBuffer,
|
||||
|
||||
// Center content in new page format
|
||||
page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
|
||||
|
||||
});
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
};
|
||||
}
|
@ -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);
|
||||
});
|
||||
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));
|
||||
}
|
||||
|
||||
// Serialize the modified document
|
||||
return pdfDoc.save();
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user