From 8a63ebe6cf3fd040e2c758b5ac9d9e8ec40b4854 Mon Sep 17 00:00:00 2001 From: Felix Kaspar Date: Fri, 17 Nov 2023 20:38:45 +0100 Subject: [PATCH] Cleanup, Documented Impose --- client-tauri/src/utils/pdf-operations.ts | 3 +-- server-node/src/utils/pdf-operations.ts | 3 +-- shared-operations/declarations/Action.d.ts | 12 ++++++++++++ shared-operations/src/functions/extractPages.ts | 12 ++++++------ shared-operations/src/functions/impose.ts | 15 +++++++-------- shared-operations/src/index.ts | 4 ++-- .../src/workflow/traverseOperations.ts | 2 +- 7 files changed, 30 insertions(+), 21 deletions(-) diff --git a/client-tauri/src/utils/pdf-operations.ts b/client-tauri/src/utils/pdf-operations.ts index f943e978f..1cf6aec08 100644 --- a/client-tauri/src/utils/pdf-operations.ts +++ b/client-tauri/src/utils/pdf-operations.ts @@ -7,8 +7,7 @@ import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile" import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js"; async function impose(params: ImposeParamsType): Promise { - const paramsToUse = { ...params, pdfcpuWrapper: pdfcpuWrapper }; - return SharedOperations.impose(paramsToUse); + return SharedOperations.impose(params, pdfcpuWrapper); } const toExport: OperationsType = { diff --git a/server-node/src/utils/pdf-operations.ts b/server-node/src/utils/pdf-operations.ts index a1d0d8f84..48f58d10d 100644 --- a/server-node/src/utils/pdf-operations.ts +++ b/server-node/src/utils/pdf-operations.ts @@ -7,8 +7,7 @@ import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile" import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/src/wasm/pdfcpu/pdfcpu-wrapper-node.js"; async function impose(params: ImposeParamsType): Promise { - const paramsToUse = { ...params, pdfcpuWrapper: pdfcpuWrapper }; - return SharedOperations.impose(paramsToUse); + return SharedOperations.impose(params, pdfcpuWrapper); } const toExport: OperationsType = { diff --git a/shared-operations/declarations/Action.d.ts b/shared-operations/declarations/Action.d.ts index 822fc1630..2019e33d1 100644 --- a/shared-operations/declarations/Action.d.ts +++ b/shared-operations/declarations/Action.d.ts @@ -4,6 +4,18 @@ export interface Action { actions?: Action[]; } +export interface WaitAction extends Action { + values: { id: number } +} + +export interface ExtractAction extends Action { + values: { indecies: string | number[] } +} + +export interface ImposeAction extends Action { + values: { nup: number, format: string } +} + export interface WaitAction extends Action { values: { id: number } } \ No newline at end of file diff --git a/shared-operations/src/functions/extractPages.ts b/shared-operations/src/functions/extractPages.ts index c1206f965..46b69843d 100644 --- a/shared-operations/src/functions/extractPages.ts +++ b/shared-operations/src/functions/extractPages.ts @@ -5,19 +5,19 @@ import { parsePageIndexSpecification } from './common/pageIndexesUtils' export type ExtractPagesParamsType = { file: PdfFile; - pageIndexes: string | number[]; + pageIndecies: string | number[]; } export async function extractPages(params: ExtractPagesParamsType): Promise { - const { file, pageIndexes } = params; + const { file, pageIndecies: pageIndecies } = params; const pdfLibDocument = await file.pdfLibDocument; - var indexes = pageIndexes; + var indecies = pageIndecies; - if (!Array.isArray(indexes)) { - indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount()); + if (!Array.isArray(indecies)) { + indecies = parsePageIndexSpecification(indecies, pdfLibDocument.getPageCount()); } - const newFile = await getPages(file, indexes); + const newFile = await getPages(file, indecies); newFile.filename += "_extractedPages" return newFile; } diff --git a/shared-operations/src/functions/impose.ts b/shared-operations/src/functions/impose.ts index 00cddca8c..7bf3d25cb 100644 --- a/shared-operations/src/functions/impose.ts +++ b/shared-operations/src/functions/impose.ts @@ -2,17 +2,16 @@ import { PdfFile, RepresentationType } from "../wrappers/PdfFile"; export type ImposeParamsType = { file: PdfFile; + /** Accepted values are 2, 3, 4, 8, 9, 12, 16 - see: {@link https://pdfcpu.io/generate/nup.html#n-up-value} */ nup: number; + /** A0-A10, other formats available - see: {@link https://pdfcpu.io/paper.html} */ format: string; } -export type ImposeParamsBaseType = { - file: PdfFile; - nup: number; - format: string; - pdfcpuWrapper: any; -} -export async function impose(params: ImposeParamsBaseType): Promise { - const uint8Array = await params.pdfcpuWrapper.oneToOne( + +/** PDF-Imposition, PDF-N-Up: Put multiple pages of the input document into a single page of the output document. - see: {@link https://en.wikipedia.org/wiki/N-up} */ +export async function impose(params: ImposeParamsType, pdfcpuWrapper: any): Promise { + // https://pdfcpu.io/generate/nup.html + const uint8Array = await pdfcpuWrapper.oneToOne( [ "pdfcpu.wasm", "nup", diff --git a/shared-operations/src/index.ts b/shared-operations/src/index.ts index f9a7f45a0..13fe808c7 100644 --- a/shared-operations/src/index.ts +++ b/shared-operations/src/index.ts @@ -1,7 +1,7 @@ import { arrangePages, ArrangePagesParamsType } from './functions/arrangePages' import { extractPages, ExtractPagesParamsType } from "./functions/extractPages"; -import { impose, ImposeParamsBaseType, ImposeParamsType } from "./functions/impose"; +import { impose, ImposeParamsType } from "./functions/impose"; import { mergePDFs, MergeParamsType } from './functions/mergePDFs'; import { removeBlankPages, RemoveBlankPagesParamsType } from "./functions/removeBlankPages"; import { rotatePages, RotateParamsType } from './functions/rotatePages'; @@ -34,7 +34,7 @@ export default toExport; export type OperationsParametersBaseType = { arrangePages: ArrangePagesParamsType extractPages: ExtractPagesParamsType; - impose: ImposeParamsBaseType; + impose: ImposeParamsType; mergePDFs: MergeParamsType; removeBlankPages: RemoveBlankPagesParamsType; rotatePages: RotateParamsType; diff --git a/shared-operations/src/workflow/traverseOperations.ts b/shared-operations/src/workflow/traverseOperations.ts index 5e7f579a5..7f3e18d35 100644 --- a/shared-operations/src/workflow/traverseOperations.ts +++ b/shared-operations/src/workflow/traverseOperations.ts @@ -54,7 +54,7 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[ break; case "extract": yield* nToN(input, action, async (input) => { - const newPdf = await Operations.extractPages({file: input, pageIndexes: action.values["pageIndexes"]}); + const newPdf = await Operations.extractPages({file: input, pageIndecies: action.values["pageIndecies"]}); return newPdf; }); break;