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();
}

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+)
Ensure proper lighting
Maintain clear text
Keep original aspect ratio

Upload Strategy

Batch similar documents
Validate before upload
Monitor progress
Handle errors gracefully

Error Prevention

Verify file formats
Check size limits
Implement retries
Log all failures

Performance

Use batch uploads
Optimize file size
Cache when possible
Monitor rate limits

Need help? Contact our support team: