Pdfkit: support for internal links

Created on 8 Sep 2015  路  4Comments  路  Source: foliojs/pdfkit

I couldn't find a way to insert a link to a page in the same PDF using a document.link call. Are internal links supported?

Most helpful comment

No internal links, I add my own function linkToPage :

/**
 * Add a link to a page by number.
 *
 * @param x
 * @param y
 * @param w
 * @param h
 * @param number
 * @param options
 */
PDFDocument.prototype.linkToPage = function(x, y, w, h, number, options) {
    if (!options || options === null) {
        options = {};
    }
    options.Subtype = 'Link';
    options.Dest = ['/Dest[',(number - 1),' /XYZ null null null]'].join('');
    options.noConvertDest = true;
    return this.annotate(x, y, w, h, options);
};
PDFDocument.prototype.annotate = function(x, y, w, h, options) {
    var key, ref, val;
    options.Type = 'Annot';
    options.Rect = this._convertRect(x, y, w, h);
    options.Border = [0, 0, 0];
    if (options.Subtype !== 'Link') {
        if (options.C === null) {
            options.C = this._normalizeColor(options.color || [0, 0, 0]);
        }
    }
    delete options.color;
    if (typeof options.Dest === 'string' && !options.noConvertDest) {
        options.Dest = PDFObject.s(options.Dest);
    }
    for (key in options) {
        val = options[key];
        options[key[0].toUpperCase() + key.slice(1)] = val;
    }
    ref = this.ref(options);
    this.page.annotations.push(ref);
    ref.end();
    return this;
};

All 4 comments

No internal links, I add my own function linkToPage :

/**
 * Add a link to a page by number.
 *
 * @param x
 * @param y
 * @param w
 * @param h
 * @param number
 * @param options
 */
PDFDocument.prototype.linkToPage = function(x, y, w, h, number, options) {
    if (!options || options === null) {
        options = {};
    }
    options.Subtype = 'Link';
    options.Dest = ['/Dest[',(number - 1),' /XYZ null null null]'].join('');
    options.noConvertDest = true;
    return this.annotate(x, y, w, h, options);
};
PDFDocument.prototype.annotate = function(x, y, w, h, options) {
    var key, ref, val;
    options.Type = 'Annot';
    options.Rect = this._convertRect(x, y, w, h);
    options.Border = [0, 0, 0];
    if (options.Subtype !== 'Link') {
        if (options.C === null) {
            options.C = this._normalizeColor(options.color || [0, 0, 0]);
        }
    }
    delete options.color;
    if (typeof options.Dest === 'string' && !options.noConvertDest) {
        options.Dest = PDFObject.s(options.Dest);
    }
    for (key in options) {
        val = options[key];
        options[key[0].toUpperCase() + key.slice(1)] = val;
    }
    ref = this.ref(options);
    this.page.annotations.push(ref);
    ref.end();
    return this;
};

Hi, thank you for the snippet, I changed (number-1) to number but I suppose that this is a matter of convention on page numbering: it worked like a charm!

Hi YoannB,
Thanks for the code snippet, it was very useful. Could you help in creating named destinations. (internal link to a section within the document.)

I read the pdf specification and got this info,
Definition of named destination
[/Dest /MyNamedDest
/Page 1
/View [/FitH 5]
/DEST pdfmark

Link to a named destination
[/Rect [70 650 210 675]
/Border [16 16 1 [3 10]]
/Color [0 .7 1]
/Dest /MyNamedDest
/Subtype /Link
/ANN pdfmark

But struggling the implement this within pdfmake.

Thanks in advance.
Regards
Raja

Couldn't quite get the version above to work, but I didn't want to monkeypatch the core logic anyway.

So my (CS) link function is below, and this doesn't need patching to annotate, at least not in the most recent version.

PDFDocument::linkToPage = (x, y, w, h, number, options) ->
  options = {} if !options or options == null
  options.Subtype = 'Link'
  options.Dest = [number, 'XYZ', null, null, null]
  @annotate(x, y, w, h, options)

This certainly works on OSX Preview, not tested on Adobe (since I refuse to install it) and I'll happily make a PR for this if needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ehaubold picture ehaubold  路  5Comments

cam-intel picture cam-intel  路  6Comments

tm2josep picture tm2josep  路  5Comments

leore picture leore  路  3Comments

durango picture durango  路  4Comments