I questioned myself of how to get a code node / extrenal service that is free that prechecks my ocR ready pdf if an upload to mistral is being needed
1 Like
You can search for images inside a PDF using a Code Node if you prefer not to install extra dependencies. While this method is primitive, it will flag any graphical element, including logos. You could potentially extend the logic to analyze image metadata to filter for actual photographs.
// Quick PDF image detection - checks raw PDF structure for embedded images
const pdfBuffer = await this.helpers.getBinaryDataBuffer(0, 'data');
const content = pdfBuffer.toString('latin1');
// Look for /Subtype /Image markers - this is how PDF spec defines image XObjects
const imageSubtypeRegex = /\/Subtype\s*\/Image/gi;
const imageMatches = content.match(imageSubtypeRegex);
const imageCount = imageMatches ? imageMatches.length : 0;
// Sometimes images don't have explicit Subtype tags but use specific compression
// DCT = JPEG, JPX = JPEG2000, CCITT/JBIG2 = fax/scanned docs
const hasImageCompression =
content.includes('/DCTDecode') ||
content.includes('/JPXDecode') ||
content.includes('/CCITTFaxDecode') ||
content.includes('/JBIG2Decode');
const hasImages = imageCount > 0 || hasImageCompression;
return [{
json: {
hasImages: hasImages,
estimatedImageCount: imageCount,
debug: {
hasDCT: content.includes('/DCTDecode'),
hasJPX: content.includes('/JPXDecode'),
hasCCITT: content.includes('/CCITTFaxDecode'),
hasJBIG2: content.includes('/JBIG2Decode')
}
}
}];
Otherwise you could install for example poppler-utils on your n8n server to count the images inside the PDF.
what kind of utils are being used on self hosted servers?