mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-04-08 01:16:26 +02: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 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';
|
import express, { Request, Response } from 'express';
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
@ -8,6 +9,36 @@ import multer from 'multer';
|
|||||||
const upload = multer();
|
const upload = multer();
|
||||||
import Joi from 'joi';
|
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) {
|
router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
||||||
const schema = Joi.object({
|
const schema = Joi.object({
|
||||||
angle: Joi.number().required()
|
angle: Joi.number().required()
|
||||||
@ -22,9 +53,10 @@ router.post('/rotate-pdf', upload.single("pdfFile"), async function(req: Request
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const rotated = await Operations.rotatePages(req.file.buffer, value.angle)
|
const arrayFile = fromMulterFile(req.file);
|
||||||
const newFilename = appendToFilename(req.file.originalname, '_rotated');
|
const rotated = await Operations.rotatePages(arrayFile, value.angle)
|
||||||
respondWithBinaryPdf(res, rotated, newFilename);
|
rotated.filename = appendToFilename(arrayFile.filename, '_rotated');
|
||||||
|
respondWithPdfFile(res, rotated);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/update-metadata', upload.single("pdfFile"), async function(req: Request, res: Response) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const processed = await Operations.updateMetadata(req.file.buffer, value)
|
const arrayFile = fromMulterFile(req.file);
|
||||||
const newFilename = appendToFilename(req.file.originalname, '_edited-metadata');
|
const processed = await Operations.updateMetadata(arrayFile, value)
|
||||||
respondWithBinaryPdf(res, processed, newFilename);
|
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, {
|
res.writeHead(200, {
|
||||||
'Content-Type': "application/pdf",
|
'Content-Type': "application/pdf",
|
||||||
'Content-disposition': 'attachment;filename=' + filename,
|
'Content-disposition': 'attachment;filename=' + byteFile.filename,
|
||||||
'Content-Length': buffer.length
|
'Content-Length': byteFile.byteArray?.length
|
||||||
});
|
});
|
||||||
res.end(buffer)
|
res.end(byteFile.byteArray)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function response_mustHaveExactlyOneFile(res: Response): void {
|
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));
|
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';
|
import { degrees } from 'pdf-lib';
|
||||||
|
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||||
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,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
export async function rotatePages(file: PdfFile, rotation: number|number[]): Promise<PdfFile> {
|
||||||
|
const pdfDoc = await file.getAsPdfLib();
|
||||||
const pages = pdfDoc.getPages();
|
const pages = pdfDoc.getPages();
|
||||||
|
|
||||||
pages.forEach(page => {
|
if (Array.isArray(rotation)) {
|
||||||
// Change page size
|
if (rotation.length != pages.length) {
|
||||||
page.setRotation(degrees(rotation))
|
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 fromPdfLib(pdfDoc, file.filename);
|
||||||
return pdfDoc.save();
|
|
||||||
};
|
};
|
@ -1,30 +1,36 @@
|
|||||||
|
|
||||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
import { PDFPage } from 'pdf-lib';
|
||||||
|
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||||
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,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
export async function scaleContent(file: PdfFile, scaleFactor: number|number[]): Promise<PdfFile> {
|
||||||
|
const pdfDoc = await file.getAsPdfLib();
|
||||||
const pages = pdfDoc.getPages();
|
const pages = pdfDoc.getPages();
|
||||||
|
|
||||||
pages.forEach(page => {
|
if (Array.isArray(scaleFactor)) {
|
||||||
const width = page.getWidth();
|
if (scaleFactor.length != pages.length) {
|
||||||
const height = page.getHeight();
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
// Scale content
|
return fromPdfLib(pdfDoc, file.filename);
|
||||||
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));
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
// Serialize the modified document
|
|
||||||
return pdfDoc.save();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function scalePage(page: PDFPage, scaleFactor: number) {
|
||||||
|
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),
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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';
|
import { PDFPage } from 'pdf-lib';
|
||||||
|
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||||
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;
|
|
||||||
|
|
||||||
|
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();
|
const pages = pdfDoc.getPages();
|
||||||
|
|
||||||
pages.forEach(page => {
|
if (Array.isArray(pageSize)) {
|
||||||
// Change page size
|
if (pageSize.length != pages.length) {
|
||||||
page.setSize(new_size.width, new_size.height);
|
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 fromPdfLib(pdfDoc, file.filename);
|
||||||
return pdfDoc.save();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
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: {
|
a4: {
|
||||||
width: 594.96,
|
width: 594.96,
|
||||||
height: 841.92
|
height: 841.92
|
||||||
@ -29,4 +53,4 @@ export const PageSize = {
|
|||||||
width: 612,
|
width: 612,
|
||||||
height: 792
|
height: 792
|
||||||
}
|
}
|
||||||
};
|
});
|
@ -1,12 +1,9 @@
|
|||||||
|
|
||||||
import { PDFDocument } from 'pdf-lib';
|
|
||||||
import * as PDFJS from 'pdfjs-dist';
|
|
||||||
import jsQR from "jsqr";
|
import jsQR from "jsqr";
|
||||||
|
|
||||||
import { detectEmptyPages } from "./common/detectEmptyPages.js";
|
import { detectEmptyPages } from "./common/detectEmptyPages.js";
|
||||||
import { getImagesOnPage } from "./common/getImagesOnPage.js";
|
import { getImagesOnPage } from "./common/getImagesOnPage.js";
|
||||||
import { selectPages } from "./subDocumentFunctions";
|
import { selectPages } from "./subDocumentFunctions";
|
||||||
import { TypedArray, DocumentInitParameters } from 'pdfjs-dist/types/src/display/api.js';
|
|
||||||
import { PdfFile } from '../wrappers/PdfFile.js';
|
import { PdfFile } from '../wrappers/PdfFile.js';
|
||||||
|
|
||||||
export async function splitOn(
|
export async function splitOn(
|
||||||
|
@ -48,7 +48,7 @@ export async function selectPages(file: PdfFile, pagesToExtractArray: number[]):
|
|||||||
subDocument.addPage(copiedPages[i]);
|
subDocument.addPage(copiedPages[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fromPdfLib(subDocument);
|
return fromPdfLib(subDocument, file.filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removePages(file: PdfFile, pagesToRemoveArray: number[]): Promise<PdfFile> {
|
export async function removePages(file: PdfFile, pagesToRemoveArray: number[]): Promise<PdfFile> {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
||||||
|
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
|
||||||
|
|
||||||
export type Metadata = {
|
export type Metadata = {
|
||||||
deleteAll?: boolean, // Delete all metadata if set to true
|
deleteAll?: boolean, // Delete all metadata if set to true
|
||||||
@ -13,21 +13,11 @@ export type Metadata = {
|
|||||||
subject?: string, // The subject of the document
|
subject?: string, // The subject of the document
|
||||||
title?: string, // The title of the document
|
title?: string, // The title of the document
|
||||||
//trapped?: string, // The trapped status 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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export async function updateMetadata(file: PdfFile, metadata: Metadata|null): Promise<PdfFile> {
|
||||||
*
|
const pdfDoc = await file.getAsPdfLib();
|
||||||
* @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,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!metadata || metadata.deleteAll) {
|
if (!metadata || metadata.deleteAll) {
|
||||||
pdfDoc.setAuthor("");
|
pdfDoc.setAuthor("");
|
||||||
@ -40,7 +30,7 @@ export async function updateMetadata(snapshot: string | Uint8Array | ArrayBuffer
|
|||||||
pdfDoc.setTitle("")
|
pdfDoc.setTitle("")
|
||||||
}
|
}
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
return pdfDoc.save();
|
return fromPdfLib(pdfDoc, file.filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(metadata.author)
|
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
|
// TODO add trapped and custom metadata. May need another library
|
||||||
|
|
||||||
// Serialize the modified document
|
return fromPdfLib(pdfDoc, file.filename);
|
||||||
return pdfDoc.save();
|
|
||||||
};
|
};
|
||||||
|
@ -7,12 +7,13 @@ export class PdfFile {
|
|||||||
byteArray: Uint8Array | null;
|
byteArray: Uint8Array | null;
|
||||||
pdfLib: PDFDocument | null;
|
pdfLib: PDFDocument | null;
|
||||||
pdfJs: PDFDocumentProxy | null;
|
pdfJs: PDFDocumentProxy | null;
|
||||||
filename?: string;
|
filename: string;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.byteArray = null;
|
this.byteArray = null;
|
||||||
this.pdfLib = null;
|
this.pdfLib = null;
|
||||||
this.pdfJs = null;
|
this.pdfJs = null;
|
||||||
|
this.filename = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
async convertToByteArrayFile(): Promise<PdfFile> {
|
async convertToByteArrayFile(): Promise<PdfFile> {
|
||||||
@ -57,22 +58,22 @@ export class PdfFile {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fromMulterFile(value: Express.Multer.File, filename?: string): PdfFile {
|
export function fromMulterFile(value: Express.Multer.File): PdfFile {
|
||||||
return fromUint8Array(value.buffer, filename)
|
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();
|
const out = new PdfFile();
|
||||||
out.byteArray = value;
|
out.byteArray = value;
|
||||||
out.filename = filename;
|
out.filename = filename;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
export function fromPdfLib(value: PDFDocument, filename?: string): PdfFile {
|
export function fromPdfLib(value: PDFDocument, filename: string): PdfFile {
|
||||||
const out = new PdfFile();
|
const out = new PdfFile();
|
||||||
out.pdfLib = value;
|
out.pdfLib = value;
|
||||||
out.filename = filename;
|
out.filename = filename;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
export function fromPdfJs(value: PDFDocumentProxy, filename?: string): PdfFile {
|
export function fromPdfJs(value: PDFDocumentProxy, filename: string): PdfFile {
|
||||||
const out = new PdfFile();
|
const out = new PdfFile();
|
||||||
out.pdfJs = value;
|
out.pdfJs = value;
|
||||||
out.filename = filename;
|
out.filename = filename;
|
||||||
|
Loading…
Reference in New Issue
Block a user