Document Stack
Document Stack
Docs

Performance Optimization

Speed up PDF generation with template design choices, caching, and API usage patterns.

Generation Speed

PDF generation speed depends on template complexity, image count, page count, and data size. Most simple documents generate in under 500ms.

Template TypeTypical Generation Time
Simple invoice (1 page, no images)100–300ms
Invoice with logo and table200–500ms
Multi-page report (5 pages)500ms–1s
Image-heavy certificate300–800ms
Large data table (50+ rows)500ms–2s

Template Optimization

  • Minimize images — Each image requires a network fetch; use fewer, smaller images
  • Optimize image sizes — Don't use a 2000px image for a 200px element
  • Limit custom fonts — Each font weight adds processing time
  • Reduce page count — Fewer pages = faster generation
  • Simplify complex layouts — Fewer elements means less rendering work

API Usage Patterns

  • Reuse the SDK client — Create one instance and reuse it; don't create a new client per request
  • Use connection keep-alive — The SDK handles this automatically
  • Batch wisely — Process concurrently but respect rate limits
  • Use webhooks for async — Don't poll for results; let the API notify you

Caching Strategies

If you generate the same document repeatedly with the same data, cache the result:

  • URL caching — Store the PDF URL and reuse it until the template or data changes
  • Content hashing — Hash the template ID + data to create a cache key
  • CDN delivery — Serve cached PDFs through a CDN for fast downloads
  • TTL-based — Set cache expiry based on how often data changes

Cache Key Pattern

A good cache key is: pdf:{templateId}:{hash(data)}. Use MD5 or SHA-256 of the JSON data for the hash.

Image Performance Tips

  • Host images on a fast CDN close to Document Stack servers
  • Use JPEG for photos (smaller than PNG)
  • Pre-compress all images before uploading
  • Use image URLs that respond quickly (avoid redirects)

Data Optimization

  • Only send data fields that the template actually uses
  • Pre-format numbers and dates on the client side
  • Keep table data rows to a reasonable count (under 100 per page)
  • Avoid deeply nested data structures

Monitoring

  • Track generation times in your application logs
  • Set alerts for generation times exceeding your SLA
  • Monitor API response headers for server-side timing
  • Use Usage & Quotas to track overall consumption

Next Steps