Refactor color input fields and enhance watermark functionality

- Updated color input fields in various HTML pages to remove unnecessary classes for improved styling consistency.
- Modified the watermark node to include options for positioning and flattening watermarks.
- Enhanced the addTextWatermark function to support customizable positioning and page selection for watermarks.
- Added new controls for text and image watermarks in the UI, allowing users to specify text, font size, color, opacity, angle, and image scaling.
- Updated the WASM provider to use the latest version of pymupdf-wasm.
This commit is contained in:
alam00000
2026-02-26 19:40:08 +05:30
parent 2e64e8e8d4
commit 88260c26ab
41 changed files with 1512 additions and 383 deletions

View File

@@ -3,9 +3,10 @@ import { BaseWorkflowNode } from './base-node';
import { pdfSocket } from '../sockets';
import type { SocketData } from '../types';
import { requirePdfInput, processBatch } from '../types';
import { addTextWatermark } from '../../utils/pdf-operations';
import { addTextWatermark, parsePageRange } from '../../utils/pdf-operations';
import { PDFDocument } from 'pdf-lib';
import { hexToRgb } from '../../utils/helpers.js';
import * as pdfjsLib from 'pdfjs-dist';
export class WatermarkNode extends BaseWorkflowNode {
readonly category = 'Edit & Annotate' as const;
@@ -39,6 +40,18 @@ export class WatermarkNode extends BaseWorkflowNode {
'angle',
new ClassicPreset.InputControl('number', { initial: -45 })
);
this.addControl(
'position',
new ClassicPreset.InputControl('text', { initial: 'center' })
);
this.addControl(
'pages',
new ClassicPreset.InputControl('text', { initial: 'all' })
);
this.addControl(
'flatten',
new ClassicPreset.InputControl('text', { initial: 'no' })
);
}
async data(
@@ -67,16 +80,89 @@ export class WatermarkNode extends BaseWorkflowNode {
const opacity = getNum('opacity', 30) / 100;
const angle = getNum('angle', -45);
const positionPresets: Record<string, { x: number; y: number }> = {
'top-left': { x: 0.15, y: 0.15 },
top: { x: 0.5, y: 0.15 },
'top-right': { x: 0.85, y: 0.15 },
left: { x: 0.15, y: 0.5 },
center: { x: 0.5, y: 0.5 },
right: { x: 0.85, y: 0.5 },
'bottom-left': { x: 0.15, y: 0.85 },
bottom: { x: 0.5, y: 0.85 },
'bottom-right': { x: 0.85, y: 0.85 },
};
const posKey = getText('position', 'center').trim().toLowerCase();
const { x, y } = positionPresets[posKey] ?? positionPresets['center'];
const pagesStr = getText('pages', 'all').trim().toLowerCase();
const shouldFlatten =
getText('flatten', 'no').trim().toLowerCase() === 'yes';
return {
pdf: await processBatch(pdfInputs, async (input) => {
const resultBytes = await addTextWatermark(input.bytes, {
const srcDoc = await PDFDocument.load(input.bytes);
const totalPages = srcDoc.getPageCount();
const pageIndices =
pagesStr === 'all' ? undefined : parsePageRange(pagesStr, totalPages);
let resultBytes = await addTextWatermark(input.bytes, {
text: watermarkText,
fontSize,
color: { r: c.r, g: c.g, b: c.b },
opacity,
angle,
x,
y: 1 - y,
pageIndices,
});
if (shouldFlatten) {
const watermarkedPdf = await pdfjsLib.getDocument({
data: resultBytes.slice(),
}).promise;
const flattenedDoc = await PDFDocument.create();
const renderScale = 2.5;
for (let i = 1; i <= watermarkedPdf.numPages; i++) {
const page = await watermarkedPdf.getPage(i);
const unscaledVP = page.getViewport({ scale: 1 });
const viewport = page.getViewport({ scale: renderScale });
const canvas = document.createElement('canvas');
canvas.width = viewport.width;
canvas.height = viewport.height;
const ctx = canvas.getContext('2d')!;
await page.render({ canvasContext: ctx, canvas, viewport }).promise;
const jpegBytes = await new Promise<ArrayBuffer>(
(resolve, reject) =>
canvas.toBlob(
(blob) =>
blob
? blob.arrayBuffer().then(resolve)
: reject(new Error(`Failed to rasterize page ${i}`)),
'image/jpeg',
0.92
)
);
const image = await flattenedDoc.embedJpg(jpegBytes);
const newPage = flattenedDoc.addPage([
unscaledVP.width,
unscaledVP.height,
]);
newPage.drawImage(image, {
x: 0,
y: 0,
width: unscaledVP.width,
height: unscaledVP.height,
});
}
resultBytes = new Uint8Array(await flattenedDoc.save());
}
const resultDoc = await PDFDocument.load(resultBytes);
return {