Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to use pdfkit link method to generate hyperlinks inside the table? #18

Open
jayanth0907 opened this issue Apr 24, 2018 · 7 comments

Comments

@jayanth0907
Copy link

No description provided.

@jayanth0907 jayanth0907 changed the title Is it possible to user pdfkit link method to generate hyperlinks inside the table? Is it possible to use pdfkit link method to generate hyperlinks inside the table? Apr 24, 2018
@tafel
Copy link
Contributor

tafel commented Aug 14, 2018

Hi, you can add link: "https://mysite.com" in your column configuration. You can use all that pdfkit.text() accepts, like underline, link, strike, etc.

And you can change the text color using cache: false, renderer: Function in your column. For example:

{
    id: 'description',
    header: 'Product',
    cache: false,
    renderer(tb, data, draw) {
        if (draw) { 
            tb.pdf.fillColor('red'); return data.description
        }
        return ``;
    }
}

@iwanwmys
Copy link

iwanwmys commented Feb 3, 2021

Hi @tafel thanks so much for your description here, however, I am struggling a bit to get a link to actually work, can you perhaps show what the column configuration should look like for passing a dynamic link into a field? Thanks so much!

Here's what I have, I've also tried different variations, but I'm just not sure how to do that. Thanks

.addColumns([
            {
                id: 'fileName',
                header: 'File Name',
                align: 'left',
            },
            {
                id: 'folder',
                header: 'Folder',
                align: 'left',
                width: 80
            },
            {
                id: 'link',
                header: 'URL',
                align: 'left',
                width: 250,
                link: function (data) {
                    return data.link
                }
            }
        ])

@tafel
Copy link
Contributor

tafel commented Feb 3, 2021

When you have special needs, like dynamic links or images, you will need to manage yourself the drawing. Here's an example of how to add your dynamic link:

// column definition
{
    id: 'description',
    header: 'Product',
    align: 'left',
    link: 'https://duckduckgo.com/?q=xxx',
    cache: false,
    renderer(tb, data, draw, column, pos) {
        if (!draw) {
            // we are calculating cell height. We only need text now.
            return data.description;
        } else {
            // we are drawing content on the page. You probably will
            // need to adjust `pos.x` and `pos.y` as well as cell width,
            // depending on the cell padding you want.
            tb.pdf.text(data.description, pos.x, pos.y, {
                ...column,
                link: column.link.replace('xxx', data.id),
                height: data._renderedContent.height, // this value is automatically calculated
                width: column.width
            });
        }
    }
}

// sample data
{ id: 'abc', description: 'Product 1' }

@iwanwmys
Copy link

iwanwmys commented Feb 3, 2021

That is exactly what I needed, thank you very much!

@iwanwmys
Copy link

iwanwmys commented Feb 3, 2021

@tafel Just letting you know your solution worked, thanks :)

@bso-oo
Copy link

bso-oo commented Apr 14, 2021

Do you know how to apply font or change color?
Also, can you fill in the column only?

@bso-oo
Copy link

bso-oo commented Apr 14, 2021

When you have special needs, like dynamic links or images, you will need to manage yourself the drawing. Here's an example of how to add your dynamic link:

// column definition
{
    id: 'description',
    header: 'Product',
    align: 'left',
    link: 'https://duckduckgo.com/?q=xxx',
    cache: false,
    renderer(tb, data, draw, column, pos) {
        if (!draw) {
            // we are calculating cell height. We only need text now.
            return data.description;
        } else {
            // we are drawing content on the page. You probably will
            // need to adjust `pos.x` and `pos.y` as well as cell width,
            // depending on the cell padding you want.
            tb.pdf.text(data.description, pos.x, pos.y, {
                ...column,
                link: column.link.replace('xxx', data.id),
                height: data._renderedContent.height, // this value is automatically calculated
                width: column.width
            });
        }
    }
}

// sample data
{ id: 'abc', description: 'Product 1' }

Do you know how to apply font or change color?
Also, can you fill in the column only?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants