Follow these guidelines to ensure optimal document processing results and efficient API usage.
Document Requirements
Supported Formats
PDF (preferred for documents)
PNG (for scanned documents)
JPEG (for scanned documents)
File Specifications
Resolution: 300 DPI minimum
Max file size: 10MB per file
Max batch size: 50 files
Implementation Guide
1. File Optimization
const sharp = require ( 'sharp' );
async function optimizeImage ( inputBuffer ) {
return await sharp ( inputBuffer )
. resize ( 2000 , 2000 , {
fit: 'inside' ,
withoutEnlargement: true
})
. jpeg ({
quality: 85 ,
progressive: true
})
. toBuffer ();
}
const sharp = require ( 'sharp' );
async function optimizeImage ( inputBuffer ) {
return await sharp ( inputBuffer )
. resize ( 2000 , 2000 , {
fit: 'inside' ,
withoutEnlargement: true
})
. jpeg ({
quality: 85 ,
progressive: true
})
. toBuffer ();
}
from PIL import Image
import io
def optimize_image ( input_bytes ):
with Image.open(io.BytesIO(input_bytes)) as img:
# Convert to RGB if needed
if img.mode in ( 'RGBA' , 'P' ):
img = img.convert( 'RGB' )
# Resize if too large
max_size = 2000
ratio = min (max_size / img.width, max_size / img.height)
if ratio < 1 :
new_size = ( int (img.width * ratio), int (img.height * ratio))
img = img.resize(new_size, Image. LANCZOS )
# Save optimized image
output = io.BytesIO()
img.save(output, 'JPEG' , quality = 85 , optimize = True )
return output.getvalue()
2. Batch Processing
For multiple files, use batch uploading to reduce API calls and improve performance.
async function batchUpload ( files , batchSize = 10 ) {
const batches = [];
for ( let i = 0 ; i < files . length ; i += batchSize ) {
batches . push ( files . slice ( i , i + batchSize ));
}
const results = [];
for ( const batch of batches ) {
const batchResults = await Promise . all (
batch . map ( file => uploadFile ( file ))
);
results . push ( ... batchResults );
}
return results ;
}
3. Validation
Always validate files before uploading to prevent processing errors.
function validateFile ( file ) {
const validations = {
size: file . size <= 10 * 1024 * 1024 , // 10MB limit
type: [ 'application/pdf' , 'image/jpeg' , 'image/png' ]. includes ( file . type ),
name: / ^ [ a-zA-Z0-9-_ \. ] + $ / . test ( file . name )
};
const errors = Object . entries ( validations )
. filter (([ _ , valid ]) => ! valid )
. map (([ key ]) => `Invalid file ${ key } ` );
return {
valid: errors . length === 0 ,
errors
};
}
4. Error Handling
Implement retry logic to handle transient failures gracefully.
async function uploadWithRetry ( file , maxRetries = 3 ) {
for ( let attempt = 0 ; attempt < maxRetries ; attempt ++ ) {
try {
return await uploadFile ( file );
} catch ( error ) {
if ( ! isRetryableError ( error ) || attempt === maxRetries - 1 ) {
throw error ;
}
const delay = Math . pow ( 2 , attempt ) * 1000 ;
await new Promise ( resolve => setTimeout ( resolve , delay ));
}
}
}
Troubleshooting Guide
Best Practices Summary
Document Quality Use high resolution (300 DPI+)
Keep original aspect ratio
Need help? Contact our support team: