Jspdf-autotable: jsPDF autoTable columnWidth wrap cuts table off

Created on 15 Dec 2017  Â·  13Comments  Â·  Source: simonbengtsson/jsPDF-AutoTable

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?

enhancement

Most helpful comment

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

All 13 comments

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

@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?

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

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

I also tested with custom columnWidth but it doesn't work.
(Last post -> https://github.com/simonbengtsson/jsPDF-AutoTable/issues/20 )

Unfortunately not :(

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

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

Can you share the full code?

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. ^^'

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

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

Should add a warning if a string is provided!

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

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simonbengtsson picture simonbengtsson  Â·  4Comments

simonbengtsson picture simonbengtsson  Â·  5Comments

ayozebarrera picture ayozebarrera  Â·  3Comments

dnlbaldwin picture dnlbaldwin  Â·  6Comments

ultra2 picture ultra2  Â·  6Comments