Changed PdfFile.filename to exclude file extensions. Other naming fixes

This commit is contained in:
Saud Fatayerji
2023-11-16 02:24:10 +03:00
parent 667583984f
commit 576b0e02f6
11 changed files with 108 additions and 83 deletions

View File

@@ -12,7 +12,8 @@ export type ImposeParamsBaseType = {
pdfcpuWrapper: any;
}
export async function impose(params: ImposeParamsBaseType): Promise<PdfFile> {
const result = new PdfFile(params.file.originalFilename, await params.pdfcpuWrapper.oneToOne([
const uint8Array = await params.pdfcpuWrapper.oneToOne(
[
"pdfcpu.wasm",
"nup",
"-c",
@@ -21,7 +22,17 @@ export async function impose(params: ImposeParamsBaseType): Promise<PdfFile> {
"/output.pdf",
String(params.nup),
"input.pdf",
], await params.file.uint8Array), RepresentationType.Uint8Array, params.file.filename + "_imposed");
],
await params.file.uint8Array
);
const result = new PdfFile(
params.file.originalFilename,
uint8Array,
RepresentationType.Uint8Array,
params.file.filename + "_imposed"
);
console.log("ImposeResult: ", result);
return result;
}

View File

@@ -15,5 +15,6 @@ export async function mergePDFs(params: MergeParamsType): Promise<PdfFile> {
copiedPages.forEach((page) => mergedPdf.addPage(page));
}
return new PdfFile("mergedPDF", mergedPdf, RepresentationType.PDFLibDocument);
const newName = "("+params.files.map(input => input.filename).join("_and_") + ")_merged"
return new PdfFile("mergedPDF", mergedPdf, RepresentationType.PDFLibDocument, newName);
};

View File

@@ -1,6 +1,6 @@
import { degrees } from 'pdf-lib';
import { PdfFile } from '../wrappers/PdfFile';
import { PdfFile, RepresentationType } from '../wrappers/PdfFile';
export type RotateParamsType = {
file: PdfFile;
@@ -10,7 +10,8 @@ export type RotateParamsType = {
export async function rotatePages(params: RotateParamsType): Promise<PdfFile> {
const { file, rotation } = params;
const pages = (await file.pdfLibDocument).getPages();
const pdfDoc = await file.pdfLibDocument;
const pages = pdfDoc.getPages();
if (Array.isArray(rotation)) {
if (rotation.length != pages.length) {
@@ -28,5 +29,5 @@ export async function rotatePages(params: RotateParamsType): Promise<PdfFile> {
});
}
return file;
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_rotated");
};

View File

@@ -1,6 +1,6 @@
import { PDFPage } from 'pdf-lib';
import { PdfFile } from '../wrappers/PdfFile';
import { PdfFile, RepresentationType } from '../wrappers/PdfFile';
export type ScaleContentParamsType = {
file: PdfFile;
@@ -24,7 +24,7 @@ export async function scaleContent(params: ScaleContentParamsType): Promise<PdfF
pages.forEach(page => scalePage(page, scaleFactor));
}
return file;
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_scaledContent");
};
function scalePage(page: PDFPage, scaleFactor: number) {

View File

@@ -1,6 +1,6 @@
import { PDFPage } from 'pdf-lib';
import { PdfFile } from '../wrappers/PdfFile';
import { PdfFile, RepresentationType } from '../wrappers/PdfFile';
export type ScalePageParamsType = {
file: PdfFile;
@@ -24,7 +24,7 @@ export async function scalePage(params: ScalePageParamsType): Promise<PdfFile> {
pages.forEach(page => resize(page, pageSize));
}
return file;
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_scaledPages");
};
function resize(page: PDFPage, newSize: {width?:number,height?:number}) {

View File

@@ -41,7 +41,7 @@ export async function splitOn(params: SplitOnParamsType) {
console.log("File: ", file);
// Remove detected Pages & Split
const pdfDoc = await file.pdfLibDocument;
const pdfDoc = await file.pdflibDocument;
const numberOfPages = pdfDoc.getPageCount();
let pagesArray: number[] = [];
@@ -71,7 +71,7 @@ export async function splitOn(params: SplitOnParamsType) {
async function getPagesWithQRCode(file: PdfFile) {
console.log("FileInQRPrev: ", file);
const pdfDoc = await file.pdfJsDocument;
const pdfDoc = await file.pdfjsDocument;
console.log("FileInQRAfter: ", file);
const pagesWithQR: number[] = [];

View File

@@ -21,7 +21,7 @@ export async function sortPagesWithPreset(params: SortPagesWithPresetParamsType)
throw new Error("Operation not supported");
}
const pdflibDocument = await file.pdfLibDocument;
const pdflibDocument = await file.pdflibDocument;
const pageCount = pdflibDocument.getPageCount();
const sortIndecies = sortFunction(pageCount);
@@ -36,7 +36,7 @@ export type RearrangePagesParamsType = {
export async function rearrangePages(params: RearrangePagesParamsType): Promise<PdfFile> {
const { file, fancyPageSelector } = params;
const pdflibDocument = await file.pdfLibDocument;
const pdflibDocument = await file.pdflibDocument;
const pagesToExtractArray = parseFancyPageSelector(fancyPageSelector, pdflibDocument.getPageCount());
const newDocument = selectPages({file: file, pagesToExtractArray});
@@ -50,7 +50,7 @@ export type SelectPagesParamsType = {
export async function selectPages(params: SelectPagesParamsType): Promise<PdfFile> {
const { file, pagesToExtractArray } = params;
const pdflibDocument = await file.pdfLibDocument;
const pdflibDocument = await file.pdflibDocument;
const subDocument = await PDFDocument.create();
@@ -75,7 +75,7 @@ export type RemovePagesParamsType = {
export async function removePages(params: RemovePagesParamsType): Promise<PdfFile> {
const { file, pagesToRemoveArray } = params;
const pdflibDocument = await file.pdfLibDocument;
const pdflibDocument = await file.pdflibDocument;
const pagesToExtractArray = invertSelection(pagesToRemoveArray, pdflibDocument.getPageIndices())
return selectPages({file: file, pagesToExtractArray});

View File

@@ -86,8 +86,13 @@ export class PdfFile {
}
constructor(originalFilename: string, representation: Uint8Array | PDFLibDocument | PDFJSDocument, representationType: RepresentationType, filename?: string) {
if (originalFilename.toLowerCase().endsWith(".pdf"))
originalFilename = originalFilename.slice(0, -4);
this.originalFilename = originalFilename;
this.filename = filename ? filename : originalFilename;
if (this.filename.toLowerCase().endsWith(".pdf"))
this.filename = this.filename.slice(0, -4);
this.representation = representation;
this.representationType = representationType;