Prompt
My PDF generation is slow and timing out, how do I fix it?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
PDF generation slowdowns and timeouts usually come from one of a few bottlenecks:
Common causes
- Large or complex HTML/CSS
- Heavy tables, lots of images, complex layouts, web fonts, or CSS effects can make rendering much slower.
- External assets
- Fonts, images, JS, or CSS loaded from remote URLs can delay rendering or hang on network calls.
- Synchronous generation in the request path
- If you generate the PDF during a web request, the request may time out before the PDF is ready.
- High-resolution images
- Large images can dominate memory and render time.
- Browser-based renderers
- Tools like Puppeteer/Playwright can be slow if a new browser instance is launched for every PDF.
- Server resource limits
- Low CPU/memory or container limits can slow rendering significantly.
- Unoptimized template logic
- Loops, repeated calculations, or fetching data during template rendering can add latency.
Best fixes
1) Move generation out of the request
- Generate PDFs asynchronously in a background job/queue.
- Return a job ID immediately, then let the user download when ready.
2) Reuse the rendering process
- Don’t start a new browser/process for every PDF.
- Keep a warm browser instance or reuse a worker process.
3) Reduce document complexity
- Flatten CSS, simplify layout, reduce nested tables/divs.
- Remove unnecessary animations, shadows, filters, and custom fonts.
- Avoid client-side JS for critical layout if possible.
4) Optimize images and fonts
- Compress images, resize them to actual display size.
- Prefer modern compressed formats where supported.
- Subset fonts and avoid loading many font families.
5) Inline or cache assets
- Serve assets locally or inline small ones.
- Cache fonts, CSS, and images where possible.
- Avoid fetching anything from the public internet during generation.
6) Tune timeouts and memory
- Increase web server/request timeout only if needed.
- Increase memory/CPU limits if the renderer is starved.
- Set renderer-specific timeouts to fail fast and retry gracefully.
7) Profile the pipeline
Break the process into stages and time each one:
- data fetch
- template render
- asset loading
- HTML-to-PDF conversion
- file write/upload
This will tell you whether the bottleneck is data, rendering, or I/O.
If you use Puppeteer/Playwright
- Launch Chrome once and reuse it.
- Use
page.setContent()with local assets where possible. - Wait only for the necessary network activity.
- Block unnecessary requests (analytics, ads, third-party scripts).
- Set a sane timeout for navigation and PDF creation.
If you use wkhtmltopdf or a similar engine
- Avoid heavy JavaScript-dependent pages.
- Keep CSS simpler.
- Use local file paths for assets where possible.
- Ensure the executable has enough memory and access to fonts.
Quick wins
- Compress images
- Remove remote asset dependencies
- Reuse browser/process
- Generate asynchronously
- Simplify templates
If you want, I can help you troubleshoot this more specifically if you tell me:
- what library/tool you use to generate PDFs,
- how big/complex the document is,
- whether it times out in a web request or in a background job,
- your current server/runtime setup.