mirror of
https://github.com/alam00000/bentopdf.git
synced 2026-11-11 01:14:35 +01:00
Add visual workflow builder, fix critical bugs, and add Arabic i18n support
This commit is contained in:
1 parent
36ebb3b429
commit
5d8b83e105
118 files changed
+14151
-2357
No files matched your search
@@ -0,0 +1,70 @@
|
||||
import { ClassicPreset } from 'rete';
|
||||
import { BaseWorkflowNode } from './base-node';
|
||||
import { pdfSocket } from '../sockets';
|
||||
import type { SocketData } from '../types';
|
||||
import { requirePdfInput, extractAllPdfs } from '../types';
|
||||
import { downloadFile } from '../../utils/helpers.js';
|
||||
import { loadPyMuPDF } from '../../utils/pymupdf-loader.js';
|
||||
|
||||
export class ExtractImagesNode extends BaseWorkflowNode {
|
||||
readonly category = 'Output' as const;
|
||||
readonly icon = 'ph-download-simple';
|
||||
readonly description = 'Extract all images from PDF';
|
||||
|
||||
constructor() {
|
||||
super('Extract Images');
|
||||
this.addInput('pdf', new ClassicPreset.Input(pdfSocket, 'PDF'));
|
||||
}
|
||||
|
||||
async data(
|
||||
inputs: Record<string, SocketData[]>
|
||||
): Promise<Record<string, SocketData>> {
|
||||
const pdfInputs = requirePdfInput(inputs, 'Extract Images');
|
||||
const allPdfs = extractAllPdfs(pdfInputs);
|
||||
const pymupdf = await loadPyMuPDF();
|
||||
|
||||
const JSZip = (await import('jszip')).default;
|
||||
const zip = new JSZip();
|
||||
let totalImages = 0;
|
||||
|
||||
for (const pdf of allPdfs) {
|
||||
const blob = new Blob([new Uint8Array(pdf.bytes)], {
|
||||
type: 'application/pdf',
|
||||
});
|
||||
const doc = await pymupdf.open(blob);
|
||||
const pageCount = doc.pageCount;
|
||||
const prefix =
|
||||
allPdfs.length > 1 ? pdf.filename.replace(/\.pdf$/i, '') + '/' : '';
|
||||
|
||||
for (let pageIdx = 0; pageIdx < pageCount; pageIdx++) {
|
||||
const page = doc.getPage(pageIdx);
|
||||
const images = page.getImages();
|
||||
|
||||
for (const imgInfo of images) {
|
||||
try {
|
||||
const imgData = page.extractImage(imgInfo.xref);
|
||||
if (imgData && imgData.data) {
|
||||
totalImages++;
|
||||
zip.file(
|
||||
`${prefix}image_${totalImages}.${imgData.ext || 'png'}`,
|
||||
imgData.data
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
doc.close();
|
||||
}
|
||||
|
||||
if (totalImages === 0) {
|
||||
throw new Error('No images found in any of the connected PDFs');
|
||||
}
|
||||
|
||||
const zipBlob = await zip.generateAsync({ type: 'blob' });
|
||||
downloadFile(zipBlob, 'extracted_images.zip');
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user