feat: integrate fix page size functionality into the workflow by extracting core logic to a utility and adding a new workflow node.

This commit is contained in:
alam00000
2026-03-07 14:33:33 +05:30
parent c2147a2f20
commit d32251014e
5 changed files with 390 additions and 188 deletions

View File

@@ -0,0 +1,104 @@
import { ClassicPreset } from 'rete';
import { BaseWorkflowNode } from './base-node';
import { pdfSocket } from '../sockets';
import type { SocketData } from '../types';
import { requirePdfInput, processBatch } from '../types';
import { fixPageSize } from '../../utils/pdf-operations';
import { PDFDocument } from 'pdf-lib';
import { hexToRgb } from '../../utils/helpers.js';
export class FixPageSizeNode extends BaseWorkflowNode {
readonly category = 'Organize & Manage' as const;
readonly icon = 'ph-frame-corners';
readonly description = 'Standardize all pages to a target size';
constructor() {
super('Fix Page Size');
this.addInput('pdf', new ClassicPreset.Input(pdfSocket, 'PDF'));
this.addOutput(
'pdf',
new ClassicPreset.Output(pdfSocket, 'Standardized PDF')
);
this.addControl(
'targetSize',
new ClassicPreset.InputControl('text', { initial: 'A4' })
);
this.addControl(
'orientation',
new ClassicPreset.InputControl('text', { initial: 'auto' })
);
this.addControl(
'scalingMode',
new ClassicPreset.InputControl('text', { initial: 'fit' })
);
this.addControl(
'backgroundColor',
new ClassicPreset.InputControl('text', { initial: '#ffffff' })
);
this.addControl(
'customWidth',
new ClassicPreset.InputControl('number', { initial: 210 })
);
this.addControl(
'customHeight',
new ClassicPreset.InputControl('number', { initial: 297 })
);
this.addControl(
'customUnits',
new ClassicPreset.InputControl('text', { initial: 'mm' })
);
}
async data(
inputs: Record<string, SocketData[]>
): Promise<Record<string, SocketData>> {
const pdfInputs = requirePdfInput(inputs, 'Fix Page Size');
const getText = (key: string, fallback: string) => {
const ctrl = this.controls[key] as
| ClassicPreset.InputControl<'text'>
| undefined;
return (ctrl?.value || fallback).trim();
};
const getNum = (key: string, fallback: number) => {
const ctrl = this.controls[key] as
| ClassicPreset.InputControl<'number'>
| undefined;
const value = ctrl?.value;
return Number.isFinite(value) ? (value as number) : fallback;
};
const targetSize = getText('targetSize', 'A4');
const orientation = getText('orientation', 'auto');
const scalingMode = getText('scalingMode', 'fit');
const backgroundHex = getText('backgroundColor', '#ffffff');
const customWidth = Math.max(1, getNum('customWidth', 210));
const customHeight = Math.max(1, getNum('customHeight', 297));
const customUnits = getText('customUnits', 'mm');
const backgroundColor = hexToRgb(backgroundHex);
return {
pdf: await processBatch(pdfInputs, async (input) => {
const resultBytes = await fixPageSize(input.bytes, {
targetSize,
orientation,
scalingMode,
backgroundColor,
customWidth,
customHeight,
customUnits,
});
const resultDoc = await PDFDocument.load(resultBytes);
return {
type: 'pdf',
document: resultDoc,
bytes: resultBytes,
filename: input.filename.replace(/\.pdf$/i, '_standardized.pdf'),
};
}),
};
}
}

View File

@@ -13,6 +13,7 @@ import { ReversePagesNode } from './reverse-pages-node';
import { AddBlankPageNode } from './add-blank-page-node';
import { DividePagesNode } from './divide-pages-node';
import { NUpNode } from './n-up-node';
import { FixPageSizeNode } from './fix-page-size-node';
import { CombineSinglePageNode } from './combine-single-page-node';
import { CropNode } from './crop-node';
import { GreyscaleNode } from './greyscale-node';
@@ -298,6 +299,13 @@ export const nodeRegistry: Record<string, NodeRegistryEntry> = {
description: 'Arrange multiple pages per sheet',
factory: () => new NUpNode(),
},
FixPageSizeNode: {
label: 'Fix Page Size',
category: 'Organize & Manage',
icon: 'ph-frame-corners',
description: 'Standardize all pages to a target size',
factory: () => new FixPageSizeNode(),
},
CombineSinglePageNode: {
label: 'Combine to Single Page',
category: 'Organize & Manage',