Skip to content

TextWithLink & Link rectangle wrong width and height #638

@Dediggefedde

Description

@Dediggefedde

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions