Skip to content

Commit 0e26c1e

Browse files
committed
Fix px unit page size measurement (#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
1 parent a504e97 commit 0e26c1e

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

HOTFIX_README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,39 @@ new jsPDF({
1616

1717
# Active Hotfixes
1818

19-
## px_scaling
19+
## px_scaling_legacy
2020

2121
### Applies To
2222

2323
jsPDF Core
2424

2525
### Description
2626

27-
When supplying 'px' as the unit for the PDF, the internal scaling factor was being miscalculated making drawn components
28-
larger than they should be. Enabling this hotfix will correct this scaling calculation and items will be drawn to the
29-
correct scale.
27+
For backward compatibility, this hotfix restores the old (incorrect) pixel scaling behavior where scaleFactor = 96/72.
28+
By default, jsPDF now uses the correct pixel scaling (scaleFactor = 72/96) which matches the CSS standard where
29+
1px = 1/96in and 1pt = 1/72in, resulting in 1px = 72/96 pt.
3030

3131
### To Enable
3232

33-
To enable this hotfix, supply a 'hotfixes' array to the options object in the jsPDF constructor function, and add the
34-
string 'px_scaling' to this array.
33+
To enable this hotfix (restore old behavior), supply a 'hotfixes' array to the options object in the jsPDF constructor function, and add the
34+
string 'px_scaling_legacy' to this array.
3535

3636
# Accepted Hotfixes
3737

38+
## px_scaling
39+
40+
### Applies To
41+
42+
jsPDF Core
43+
44+
### Description
45+
46+
When supplying 'px' as the unit for the PDF, the internal scaling factor was being miscalculated making drawn components
47+
larger than they should be. This hotfix corrected the scaling calculation so items are drawn to the correct scale.
48+
49+
This is now the default behavior as of the fix for issue #3921. The correct scaling (72/96) is used by default.
50+
For backward compatibility with the old incorrect scaling (96/72), use the 'px_scaling_legacy' hotfix.
51+
3852
## scale_text
3953

4054
### Applies To

src/jspdf.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ function TilingPattern(boundingBox, xStep, yStep, gState, matrix) {
179179
* @param {string} [options.orientation=portrait] - Orientation of the first page. Possible values are "portrait" or "landscape" (or shortcuts "p" or "l").<br />
180180
* @param {string} [options.unit=mm] Measurement unit (base unit) to be used when coordinates are specified.<br />
181181
* Possible values are "pt" (points), "mm", "cm", "in", "px", "pc", "em" or "ex". Note that in order to get the correct scaling for "px"
182-
* units, you need to enable the hotfix "px_scaling" by setting options.hotfixes = ["px_scaling"].
182+
* units, the correct scaling (72/96) is now the default. For backward compatibility with the old incorrect scaling (96/72),
183+
* you can enable the hotfix "px_scaling_legacy" by setting options.hotfixes = ["px_scaling_legacy"].
183184
* @param {string/Array} [options.format=a4] The format of the first page. Can be:<ul><li>a0 - a10</li><li>b0 - b10</li><li>c0 - c10</li><li>dl</li><li>letter</li><li>government-letter</li><li>legal</li><li>junior-legal</li><li>ledger</li><li>tabloid</li><li>credit-card</li></ul><br />
184185
* Default is "a4". If you want to use your own format just pass instead of one of the above predefined formats the size as an number-array, e.g. [595.28, 841.89]
185186
* @param {boolean} [options.putOnlyUsedFonts=false] Only put fonts into the PDF, which were used.
@@ -2713,12 +2714,15 @@ function jsPDF(options) {
27132714
if (typeof parmFormat === "string") {
27142715
dimensions = getPageFormat(parmFormat.toLowerCase());
27152716
if (Array.isArray(dimensions)) {
2717+
// Dimensions from getPageFormat are in points
2718+
// beginPage expects dimensions in points, so we store them as-is
27162719
width = dimensions[0];
27172720
height = dimensions[1];
27182721
}
27192722
}
27202723

27212724
if (Array.isArray(parmFormat)) {
2725+
// Array format values are in user units, convert to points for storage
27222726
width = parmFormat[0] * scaleFactor;
27232727
height = parmFormat[1] * scaleFactor;
27242728
}
@@ -3221,10 +3225,13 @@ function jsPDF(options) {
32213225
scaleFactor = 72;
32223226
break;
32233227
case "px":
3224-
if (hasHotfix("px_scaling") == true) {
3225-
scaleFactor = 72 / 96;
3226-
} else {
3228+
// Default to correct px scaling (72/96 = 0.75)
3229+
// This matches CSS standard: 1px = 1/96in, 1pt = 1/72in, so 1px = 72/96 pt
3230+
// The old incorrect scaling (96/72) can be enabled via hotfix "px_scaling_legacy" for backward compatibility
3231+
if (hasHotfix("px_scaling_legacy") == true) {
32273232
scaleFactor = 96 / 72;
3233+
} else {
3234+
scaleFactor = 72 / 96;
32283235
}
32293236
break;
32303237
case "pc":

0 commit comments

Comments
 (0)