#184·zerox

node-zerox: Tall pdfs causing performance issues

Author: TimothyIpCreated May 29, 2025Updated Jun 19, 2025

I noticed that with some specific pdf files. It would take longer and consume more resources. I noticed this error in my logs Image.

This led me to dig into the code a bit. It seems that convertPdfToImage function grabs the aspect ratio via getPdfAspectRatio. getPdfAspectRatio calculates the aspect ratio as Height / Width.

This leads to a situation where if we have an aspect ratio >5. It will multiply the height by the aspect ratio amount.

javascript
/** 
The ASPECT_RATIO_THRESHOLD value is set to 5. 
Whenever you have a tall pdf - 500px x 5000px (WxH), the aspect ratio is 10 (5000px/500px)
**/
const aspectRatio = (await getPdfAspectRatio(pdfPath)) || 1; 

// Our tall pdf has an aspect ratio of 10, so shouldAdjustHeight is true
const shouldAdjustHeight = aspectRatio > ASPECT_RATIO_THRESHOLD; 

// We take the max of imageHeight (default 2048px) OR (10 * 2048px = 20480px).
const adjustedHeight = shouldAdjustHeight 
 ? Math.max(imageHeight, Math.round(aspectRatio * imageHeight)) 
 : imageHeight; 

const options: ConvertPdfOptions = {
  density: imageDensity,
  format: "png",

  // The adjustHeight value is now 20480px
  height: adjustedHeight,
  preserveAspectRatio: true,
  saveFilename: path.basename(pdfPath, path.extname(pdfPath)),
  savePath: tempDir,
};

Do we need this step? The .jpg and .png path do not follow this height adjustment logic.

For a PDF with 5 pages having an aspect ratio of 10 Process took: Current logic: ~28095ms Removed height adjustment logic: ~6971ms

PDFs to reproduce

PDF that is 500px x 2500px. (Aspect Ratio of 5) multi-page-ratio-5.pdf

PDF that is 500px x 5000px (Aspect Ratio of 10) multi-page-ratio-10.pdf