From 333b88a8a76801e03545a2404a15a1c2ed5fcb4f Mon Sep 17 00:00:00 2001 From: Lukas Hollaender Date: Tue, 16 Sep 2025 13:55:11 +0200 Subject: [PATCH] fix scaling of form object bounding boxes --- src/jspdf.js | 50 ++++++++++++------- test/reference/form-objects-scale-factor.pdf | Bin 0 -> 3419 bytes test/specs/form-objects.spec.js | 25 ++++++++++ 3 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 test/reference/form-objects-scale-factor.pdf create mode 100644 test/specs/form-objects.spec.js diff --git a/src/jspdf.js b/src/jspdf.js index ac63154c5..f91dd396f 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -5711,8 +5711,8 @@ function jsPDF(options) { this.x = pageX; this.y = pageY; this.matrix = pageMatrix; - this.width = getPageWidth(currentPage); - this.height = getPageHeight(currentPage); + this.width = getUnscaledPageWidth(currentPage); + this.height = getUnscaledPageHeight(currentPage); this.outputDestination = outputDestination; this.id = ""; // set by endFormObject() @@ -5727,8 +5727,8 @@ function jsPDF(options) { pageX = this.x; pageY = this.y; pageMatrix = this.matrix; - setPageWidth(currentPage, this.width); - setPageHeight(currentPage, this.height); + setPageWidthWithoutScaling(currentPage, this.width); + setPageHeightWithoutScaling(currentPage, this.height); outputDestination = this.outputDestination; }; @@ -5953,32 +5953,46 @@ function jsPDF(options) { } } - var getPageWidth = (API.getPageWidth = function(pageNumber) { - pageNumber = pageNumber || currentPage; + function getUnscaledPageWidth(pageNumber) { + return ( + pagesContext[pageNumber].mediaBox.topRightX - + pagesContext[pageNumber].mediaBox.bottomLeftX + ); + } + + function setPageWidthWithoutScaling(pageNumber, value) { + pagesContext[pageNumber].mediaBox.topRightX = + value + pagesContext[pageNumber].mediaBox.bottomLeftX; + } + + function getUnscaledPageHeight(pageNumber) { return ( - (pagesContext[pageNumber].mediaBox.topRightX - - pagesContext[pageNumber].mediaBox.bottomLeftX) / - scaleFactor + pagesContext[pageNumber].mediaBox.topRightY - + pagesContext[pageNumber].mediaBox.bottomLeftY ); + } + + function setPageHeightWithoutScaling(pageNumber, value) { + pagesContext[pageNumber].mediaBox.topRightY = + value + pagesContext[pageNumber].mediaBox.bottomLeftY; + } + + var getPageWidth = (API.getPageWidth = function(pageNumber) { + pageNumber = pageNumber || currentPage; + return getUnscaledPageWidth(pageNumber) / scaleFactor; }); var setPageWidth = (API.setPageWidth = function(pageNumber, value) { - pagesContext[pageNumber].mediaBox.topRightX = - value * scaleFactor + pagesContext[pageNumber].mediaBox.bottomLeftX; + setPageWidthWithoutScaling(pageNumber, value * scaleFactor); }); var getPageHeight = (API.getPageHeight = function(pageNumber) { pageNumber = pageNumber || currentPage; - return ( - (pagesContext[pageNumber].mediaBox.topRightY - - pagesContext[pageNumber].mediaBox.bottomLeftY) / - scaleFactor - ); + return getUnscaledPageHeight(pageNumber) / scaleFactor; }); var setPageHeight = (API.setPageHeight = function(pageNumber, value) { - pagesContext[pageNumber].mediaBox.topRightY = - value * scaleFactor + pagesContext[pageNumber].mediaBox.bottomLeftY; + setPageHeightWithoutScaling(pageNumber, value * scaleFactor); }); /** diff --git a/test/reference/form-objects-scale-factor.pdf b/test/reference/form-objects-scale-factor.pdf new file mode 100644 index 0000000000000000000000000000000000000000..497dd9bff3c195ac1e0db7726891341e292d73cf GIT binary patch literal 3419 zcmcIm-EP`S5We51n474rj= zlTJ^W%I9PN{oWYDoHr_8n$N&vkD)+m3ME&}ZTms!)*cT3Fc)62FHE!}uM>38j>?P} z+%|n$hgS|7oUHI9Znxo1B)Ci8^ui_QgXS>M?A=ae@{0MmNdG(0La`~DO3DRowJ*zj zM>nnRQr(%{+i-r#oWIj~2^SQa30R-4DHi05YNLO;=FEASCwiXdOK{$&`HQ?vw+!YC)1ox}&$0kdU>IceL->A8dGK)Mt)9;2 zk8eTv2NsNHS^8y-DgD@Pi2lF`a>LOK!DPR!oJ|eyCqo~rH&w;s`+!sVZ>#pVx{s6z zd{;uhs)XwDt)uy$Q&W7n6OL!cu9yGH7w73pm92?h$@~b(P;cRflVPL(I8A}y-xN3M zM@D98a)kTe-xzoIKbjHu_r{O%YB9k6Ka&P$&3;>a&O3~A96wy$a=jPpfvZr2O` ztkB6+8Mtr|Sb=k{u5tQ~R*0f{)px)89-&@4oV#5lI8eSKxEcze#39N9iXKrNAP*De z1Nnle0AWT&hF}{XaU-z(hsYSX#SBQIB;Oz`q&e8yAQ6=1TWu)JxB5`n#&kmda4KH| z8A`Z^Z%n|Y9Rv~e){jn@uM<_k)60_F;I{Ga8KONzZYI_FKpF*42hVvNg{dX2#t=c=!> z^(wFFxh~WK!J`mt2Ph%%eb0vlYEl+qM2LCS4b;-SZJnlE;IzEPHPtFf9 z(0jblZStZHUVKk3ZtmsWZOC!qVXW@%m?@h^!25 { + beforeAll(loadGlobals); + + it("should use correct bounding box for scale factors other than 1", () => { + const doc = new jsPDF({ unit: "mm", format: [200, 200], orientation: "p" }); + + doc.advancedAPI(); + + doc.beginFormObject(-50, -50, 100, 100, doc.unitMatrix); + doc.rect(-50, -50, 100, 100).fill(); + doc.endFormObject("0"); + + doc.doFormObject("0", new doc.Matrix(1, 0, 0, 1, 100, 100)); + + doc.setDrawColor(255, 0, 0); + doc.rect(50, 50, 100, 100).stroke(); + doc.rect(0, 0, 200, 200).stroke(); + + doc.compatAPI(); + + comparePdf(doc.output(), "form-objects-scale-factor.pdf", "form-objects"); + }); +});