Skip to content

Commit c964480

Browse files
Fix broken links after addPage with different page size (#2943)
1 parent fe706dc commit c964480

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

src/modules/annotations.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ import { jsPDF } from "../jspdf.js";
216216

217217
rect =
218218
"/Rect [" +
219-
getHorizontalCoordinateString(anno.x) +
219+
anno.finalBounds.x +
220220
" " +
221-
getVerticalCoordinateString(anno.y) +
221+
anno.finalBounds.y +
222222
" " +
223-
getHorizontalCoordinateString(anno.x + anno.w) +
223+
anno.finalBounds.w +
224224
" " +
225-
getVerticalCoordinateString(anno.y + anno.h) +
225+
anno.finalBounds.h +
226226
"] ";
227227

228228
line = "";
@@ -325,11 +325,16 @@ import { jsPDF } from "../jspdf.js";
325325
*/
326326
jsPDFAPI.link = function(x, y, w, h, options) {
327327
var pageInfo = this.internal.getCurrentPageInfo();
328+
var getHorizontalCoordinateString = this.internal.getCoordinateString;
329+
var getVerticalCoordinateString = this.internal.getVerticalCoordinateString;
330+
328331
pageInfo.pageContext.annotations.push({
329-
x: x,
330-
y: y,
331-
w: w,
332-
h: h,
332+
finalBounds: {
333+
x: getHorizontalCoordinateString(x),
334+
y: getVerticalCoordinateString(y),
335+
w: getHorizontalCoordinateString(x + w),
336+
h: getVerticalCoordinateString(y + h)
337+
},
333338
options: options,
334339
type: "link"
335340
});

test/reference/insertLinkAddPage.pdf

3.43 KB
Binary file not shown.

test/specs/annotations.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,21 @@ describe("Module: Annotations", () => {
5555
});
5656
comparePdf(doc.output(), "freetext.pdf", "annotations");
5757
});
58+
it("should draw a link on the text with link after add page", () => {
59+
const doc = new jsPDF({
60+
unit: "px",
61+
format: [200, 300],
62+
floatPrecision: 2
63+
});
64+
65+
doc.textWithLink("Click me!", 10, 10, {
66+
url: "https://parall.ax/",
67+
});
68+
69+
doc.addPage("a4");
70+
71+
doc.text("New page with difference size", 10, 10);
72+
73+
comparePdf(doc.output(), "insertLinkAddPage.pdf", "annotations");
74+
});
5875
});

0 commit comments

Comments
 (0)