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

jsPDF autoTable columnWidth wrap cuts table off #306

Closed
jamessuske opened this issue Dec 15, 2017 · 13 comments
Closed

jsPDF autoTable columnWidth wrap cuts table off #306

jamessuske opened this issue Dec 15, 2017 · 13 comments

Comments

@jamessuske
Copy link

jamessuske commented Dec 15, 2017

I am using jsPDF to generate a PDF from an HTML table and jsPDF is great, expect I am having one issue, when I try to wrap the columnWidth, my table gets cut off :(

var doc = new jsPDF('l', 'mm', "a0");
var tbl = $('#cost-matrix-table').clone();
tbl.find('tr:nth-child(1)').remove();
tbl.find('tr:nth-child(1)').remove();
tbl.find('tr:nth-child(2)').remove();
var res = doc.autoTableHtmlToJson(tbl.get(0));

doc.autoTable(res.columns, res.data, {
    startY: 40,
    margin: {
        top: 40
    },
    addPageContent: function (data) {
        doc.setFontSize(28);
        doc.setTextColor(0);
        doc.setFontStyle('bold');
        doc.text("Cost Matrix " + $("#dropdown").val(), 500, 30);
    },
    styles: {
        fontSize: 20,
        overflow: 'linebreak',
        columnWidth: 'wrap',
    },
    theme: 'grid'
});

doc.save("Report.pdf");

Is there a solution for this?

@simonbengtsson
Copy link
Owner

I can't replicate the issue. Can you create a runnable example with data included?

@jamessuske
Copy link
Author

@jdmcd
Copy link

jdmcd commented Jan 22, 2018

@simonbengtsson Hi! Big fan of the library. I also am struggling with this, even when I copy your code directly from the "long" example. My text just continues off the screen and pushes the columns to the left of it off the page with it. Is there a solution?

@jdmcd
Copy link

jdmcd commented Jan 22, 2018

This is the code that I am attempting to execute:

function generatePdf(cols, rows, name, headerText) {
        // Only pt supported (not mm or in)
        var doc = new jsPDF('p', 'pt');
        var totalPagesExp = "{total_pages_count_string}";
        var leftMargin = 40;

        doc.setFontSize(10);

        doc.autoTable(cols, rows, {
            theme: "grid",
            margin: {horizontal: 7},
            bodyStyles: {valign: 'top'},
            styles: {overflow: 'linebreak', columnWidth: 'wrap'},
            addPageContent: function(data) {
                doc.text(headerText, leftMargin, 30);
                var str = "Page " + data.pageCount;
                // Total page number plugin only available in jspdf v1.0+
                if (typeof doc.putTotalPages === 'function') {
                    str = str + " of " + totalPagesExp;
                }

                str = str + ". Generated on " + Date();
                doc.text(str, leftMargin, doc.internal.pageSize.height - 10);
            }
        });

        // Total page number plugin only available in jspdf v1.0+
        if (typeof doc.putTotalPages === 'function') {
            doc.putTotalPages(totalPagesExp);
        }

        doc.save(name);
    }

This is a picture of the outputted PDF:

screen shot 2018-01-22 at 6 46 17 pm

@Tichju
Copy link

Tichju commented Feb 7, 2018

Hello,
Did you find a solution ?
I have the same problem.

I also tested with custom columnWidth but it doesn't work.
(Last post -> #20 )

@jdmcd
Copy link

jdmcd commented Feb 7, 2018

Unfortunately not :(

@Tichju
Copy link

Tichju commented Feb 8, 2018

The solution of custom widths it works, but you have to put the width between ''

columnStyles: {
description: {
columnWidth: '97'
},
featureName: {
columnWidth: '40'
}
}

@jdmcd
Copy link

jdmcd commented Feb 8, 2018

Can you share the full code?

@Tichju
Copy link

Tichju commented Feb 8, 2018

In theory..


    doc.autoTable(columns, JSON.parse(rows), {
        margin: {horizontal: 5},
        bodyStyles: {valign: 'middle'},
        styles: {overflow: 'linebreak', columnWidth:'wrap'},
        theme: 'plain',
        columnStyles: {
            text: {
                columnWidth: 'wrap'
            },
            columnName1: {
                columnWidth: '45',
                fontStyle: 'bold',
                textColor: 240                
            },
            longColumnName: {
                columnWidth: '107'
            },
            columnName2: {
                columnWidth: '45'
            },
            columnName3: {
                columnWidth: '45'
            },
            columnName4: {
                columnWidth: '45'
            }
        }
    });

But in real, longColumnName is very long, and the other columns are very thin (a few characters wide).
It does not correspond at all to the values ​​entered.
I'm still looking for why. ^^'

@jdmcd
Copy link

jdmcd commented Feb 8, 2018

Ah interesting. Ok, please post again if you find the reason :) Thanks for your work on this!

@Tichju
Copy link

Tichju commented Feb 12, 2018

doc.autoTable(columns, JSON.parse(rows), {
        margin: {horizontal: 5},
        bodyStyles: {valign: 'middle'},
        styles: {overflow: 'linebreak', columnWidth:'wrap'},
        theme: 'plain',
        columnStyles: {
            text: {
                columnWidth: 'wrap'
            },
            columnName1: {
                columnWidth: 45,
                fontStyle: 'bold',
                textColor: 240                
            },
            description: {
                columnWidth: 107
            },
            columnName2: {
                columnWidth: 45
            },
            columnName3: {
                columnWidth: 45
            },
            columnName4: {
                columnWidth: 45
            }
        }
    });

The code looks for numbers, it was enough to remove the ''.
'107' → 107

@simonbengtsson
Copy link
Owner

Should add a warning if a string is provided!

@lavirana
Copy link

lavirana commented Jul 6, 2020

how I can set the single row height according to the image height in the table?

image

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

No branches or pull requests

5 participants