-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Description
I was using doc.textWithLink(x,y,options)
and the link-width was only fitting when x=0, while the link-height was the entire pageHeight. I'm talking about the area that triggers the link when clicked (which is highlighted shortly in most viewers).
I took a look into https://github.com/MrRio/jsPDF/blob/master/plugins/annotations.js and found line 161:
var rect = "/Rect [" + f2(anno.x * k) + " " +
f2((pageHeight - anno.y) * k) + " " +
f2(anno.x + anno.w * k) + " " +
f2(pageHeight - (anno.y + anno.h) * k) + "] ";
And here are the two issues:
for x-coordinate you have to give anno.x
unscaled as k
is the scaleFactor (e.g. 72 for inch units). however, in width, anno.w
is apparently also given unscaled while after scaling with k
, suddenly anno.x
is added unscaled.
sloppy Fix: instead of width
use offset + width - offset/k
proposed Code-Fix: f2((anno.x + anno.w) * k) + " " +
The height has a different issue:
anno.h
is apparently not an offset from the y-position or from the top of the page, but from the bottom of the page.
sloppy Fix: instead of height
use 2 * height - ScaledPageHeight
proposed Code-Fix: f2( (anno.y + anno.h) * k) + "] "
In case you use A4 and inch, ScaledPageHeight is 11.7
Code Example:
var doc = new jsPDF('p','in','a4', true);
var size=12, verticalOffset=4, margin=0.5, pageHeight=11.7, pageWidth=8.3;
doc.setFont('serif');
doc.setFontSize(size);
var LText="Text mit mehr Text?";
var Lwidth=doc.getStringUnitWidth(LText) * size/72;
doc.text(LText, margin, verticalOffset);
doc.link(margin, verticalOffset-size/72, -margin/72 + margin+Lwidth, size/36-pageHeight, {pageNumber:1, top:3.2});
So, is this really an error in the code or did I miss some setting