diff --git a/src/jspdf.js b/src/jspdf.js index cf95600a4..b77515acc 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -187,6 +187,7 @@ function TilingPattern(boundingBox, xStep, yStep, gState, matrix) { * @param {number} [options.precision=16] Precision of the element-positions. * @param {number} [options.userUnit=1.0] Not to be confused with the base unit. Please inform yourself before you use it. * @param {string[]} [options.hotfixes] An array of strings to enable hotfixes such as correct pixel scaling. + * @param {boolean} [options.autoPageOrientation=true] When enabled, jsPDF swaps custom page dimensions to match the selected orientation ("portrait" keeps height >= width, "landscape" keeps width >= height). Disable to preserve custom dimensions as provided. * @param {Object} [options.encryption] * @param {string} [options.encryption.userPassword] Password for the user bound by the given permissions list. * @param {string} [options.encryption.ownerPassword] Both userPassword and ownerPassword should be set for proper authentication. @@ -216,6 +217,7 @@ function jsPDF(options) { var precision; var floatPrecision = 16; var defaultPathOperation = "S"; + var autoPageOrientation = true; var encryptionOptions = null; options = options || {}; @@ -240,6 +242,9 @@ function jsPDF(options) { if (typeof options.floatPrecision !== "undefined") { floatPrecision = options.floatPrecision; } + if (typeof options.autoPageOrientation === "boolean") { + autoPageOrientation = options.autoPageOrientation; + } defaultPathOperation = options.defaultPathOperation || "S"; } @@ -2738,17 +2743,19 @@ function jsPDF(options) { format = [width, height]; - switch (orientation.substr(0, 1)) { - case "l": - if (height > width) { - format = [height, width]; - } - break; - case "p": - if (width > height) { - format = [height, width]; - } - break; + if (autoPageOrientation) { + switch (orientation.substr(0, 1)) { + case "l": + if (height > width) { + format = [height, width]; + } + break; + case "p": + if (width > height) { + format = [height, width]; + } + break; + } } beginPage(format); diff --git a/test/specs/pages.spec.js b/test/specs/pages.spec.js index 51eaf87dc..742d9fdf6 100644 --- a/test/specs/pages.spec.js +++ b/test/specs/pages.spec.js @@ -86,4 +86,19 @@ describe("Core: Paging", () => { expect(doc.getPageWidth(0)).toEqual(doc.getPageWidth(1)); expect(doc.getPageHeight(0)).toEqual(doc.getPageHeight(1)); }); + it("should preserve custom dimensions when autoPageOrientation is disabled", () => { + const doc = new jsPDF({ + orientation: "portrait", + unit: "pt", + format: [600, 300], + autoPageOrientation: false + }); + + expect(doc.getPageWidth(1)).toEqual(600); + expect(doc.getPageHeight(1)).toEqual(300); + + doc.addPage([700, 200], "portrait"); + expect(doc.getPageWidth(2)).toEqual(700); + expect(doc.getPageHeight(2)).toEqual(200); + }); }); diff --git a/types/index.d.ts b/types/index.d.ts index fd21f959a..e7bf2b964 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -650,6 +650,8 @@ declare module "jspdf" { putOnlyUsedFonts?: boolean; hotfixes?: string[]; floatPrecision?: number | "smart"; + autoPageOrientation?: boolean; + } export interface Point {