Fix incorrect page size measurement for px unit (#3921)
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
-
Made correct px scaling the default: Changed default
scaleFactorfrom96/72to72/96for px units -
Backward compatibility: Added
px_scaling_legacyhotfix to restore old behavior if needed -
Updated documentation: Updated
HOTFIX_README.mdand 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