Fix incorrect page size measurement for px unit (#3921) #3923
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When using
unit: 'px'with format strings (e.g.,format: 'a4'),doc.internal.pageSize.widthandheightreturned 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
scaleFactorfor px units was incorrectly set to96/72instead of72/96. According to CSS standards:The previous implementation used the inverse, causing all measurements to be incorrect.
Solution
scaleFactorfrom96/72to72/96for px unitspx_scaling_legacyhotfix to restore old behavior if neededHOTFIX_README.mdand code comments to reflect the changeTesting
✅ 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_legacyhotfix✅ 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