Skip to content

Conversation

@AyushCoder9
Copy link

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

- Make correct px scaling (72/96) the default behavior
- Fixes incorrect page size measurements when using px unit with format strings
- A4 format now correctly measures as 793.71 x 1122.52 px instead of 446.46 x 631.42 px
- Add px_scaling_legacy hotfix for backward compatibility with old (incorrect) behavior
- Update documentation to reflect the change
@HackbrettXXX
Copy link
Collaborator

Thank you for the PR. I'm hesitant to merge it at this time because it would require a major release. If we decide to fix this issue at some point, I would just make the current hotfix the default and don't offer a new hotfix for backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug in measure the width for px unit

2 participants