jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Fix incorrect page size measurement for px unit (#3921)

Open AyushCoder9 opened this issue 1 month ago • 1 comments

Problem

When using unit: 'px' with format strings (e.g., format: 'a4'), doc.internal.pageSize.width and height returned incorrect values due to an inverted scaling factor.

Before (incorrect): const doc = new jsPDF({ unit: 'px', format: 'a4' }); console.log(doc.internal.pageSize.width); // 446.46 (wrong) console.log(doc.internal.pageSize.height); // 631.42 (wrong)After (correct): const doc = new jsPDF({ unit: 'px', format: 'a4' }); console.log(doc.internal.pageSize.width); // 793.71 ✓ console.log(doc.internal.pageSize.height); // 1122.52 ✓## Root Cause

The default scaleFactor for px units was incorrectly set to 96/72 instead of 72/96. According to CSS standards:

  • 1px = 1/96in
  • 1pt = 1/72in
  • Therefore: 1px = 72/96 pt

The previous implementation used the inverse, causing all measurements to be incorrect.

Solution

  1. Made correct px scaling the default: Changed default scaleFactor from 96/72 to 72/96 for px units
  2. Backward compatibility: Added px_scaling_legacy hotfix to restore old behavior if needed
  3. Updated documentation: Updated HOTFIX_README.md and code comments to reflect the change

Testing

✅ A4 format now correctly measures as 793.71 x 1122.52 px
✅ All other units (mm, cm, in, pt) continue to work correctly
✅ Backward compatibility maintained via px_scaling_legacy hotfix
✅ Verified with multiple format sizes

Breaking Changes

None - The fix corrects a bug. However, code that was relying on the incorrect behavior will now get correct values. If you need the old (incorrect) behavior for backward compatibility, you can use:

const doc = new jsPDF({ unit: 'px', format: 'a4', hotfixes: ['px_scaling_legacy'] });## Related Issue

Fixes #3921

AyushCoder9 avatar Nov 23 '25 09:11 AyushCoder9