Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
};

Expand Down Expand Up @@ -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);
});

/**
Expand Down
Binary file added test/reference/form-objects-scale-factor.pdf
Binary file not shown.
25 changes: 25 additions & 0 deletions test/specs/form-objects.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* global jsPDF */

describe("Form objects", () => {
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");
});
});