Jspdf-autotable: How to set margin only for first page

Created on 12 Apr 2018  路  7Comments  路  Source: simonbengtsson/jsPDF-AutoTable

Hi, i exporting pdf from an API and i draw, insert and set tables an images. In header i put an image and a title, set a margin top so i can see these elements and in details have a page breaks. In this case in second page the margin top persist and draw me a white space. I want to reset this margin but only if page if not the first.

`
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.setFont('DINBold');
pdf.setFontSize(22);
pdf.setTextColor(26, 40, 88);
pdf.text("Tabla ",350, 80);
pdf.addImage(imgData, 'JPEG', 40, 40, 200, 50);

            pdf.setFont('Times New Roman');
            pdf.setFontSize(10);
            pdf.setTextColor(0, 0, 0);
            pdf.setDrawColor(104, 176, 224);
            pdf.setFillColor(104, 176, 224);

            pdf.rect(40, 136, 510, 20, 'FD');
            pdf.rect(40, 256, 510, 20, 'FD');
            pdf.rect(40, 356, 510, 20, 'FD');

            pdf.setDrawColor(120, 197, 49);
            pdf.setFillColor(120, 197, 49);

            pdf.rect(550, 136, 20, 20, 'FD');
            pdf.rect(550, 256, 20, 20, 'FD');
            pdf.rect(550, 356, 20, 20, 'FD');

            pdf.setDrawColor(221, 221, 221);
            pdf.setFillColor(221, 221, 221);
            //pdf.rect(40, 170, 530, 70, 'FD');

            pdf.setTextColor(255, 255, 255);
            pdf.text("Tabla",43, 150);

            pdf.setTextColor(27, 40, 88);
            pdf.text("N煤mero:  ",50, 186);
            pdf.text("Fecha:  ",350, 186);

            pdf.text(datos.numeroCuenta,150, 186);
            pdf.text(datos.fecha,450, 186);                              

            pdf.autoTable(movimientos.columns, movimientos.rows, {
              styles: {overflow: 'linebreak'},
              columnStyles: { 
                  fechaMovimiento: {columnWidth: 120}, 
                  descripcionMovimiento:{columnWidth:200}, 

              },
              margin: {top: 210},
              addPageContent: function(data) {
                  pdf.setTextColor(255, 255, 255);
                  pdf.text("Tabla "+ datos.fecha + " "+ datos.fecha +" N掳 "+ datos.numeroCuenta);
                }
            });

pdf.save("tabla.pdf");
`
Thanks in advance!
screenshot13

Most helpful comment

Try the following code

        let doc = new jsPDF();
        doc.autoTable({
                margin: {top: 50}, // Seting top margin for First Page.
                didDrawPage: function (data) {
                    // Reseting top margin. The change will be reflected only after print the first page.
                    data.settings.margin.top = 10; 
        });

All 7 comments

Check the startY for margin top on only the first page.

Thanks for the answer, i try this solution without results. Can you explain me the way to implement correctly? I prove conditioning pageNumber this.internal.getCurrentPageInfo().pageNumber and set startY but no good results.

Unable to provide more than short pointers here. Please post questions about how to use the plugin on stackoverflow with the jspdf and jspdf-stackoverflow tags.

I know this is already closed but you can try this:
Instead of margin, you can use startY.
startY: pdf.pageCount > 1? pdf.autoTableEndPosY() + 20 : 210

Enjoy!

Thanks for the answer, i try this solution without results. Can you explain me the way to implement correctly? I prove conditioning pageNumber this.internal.getCurrentPageInfo().pageNumber and set startY but no good results.

I know this is already closed but you can try this:
Instead of margin, you can use startY.
startY: pdf.pageCount > 1? pdf.autoTableEndPosY() + 20 : 210

Enjoy!

You are saviour Man.

Try the following code

        let doc = new jsPDF();
        doc.autoTable({
                margin: {top: 50}, // Seting top margin for First Page.
                didDrawPage: function (data) {
                    // Reseting top margin. The change will be reflected only after print the first page.
                    data.settings.margin.top = 10; 
        });

Try the following code

        let doc = new jsPDF();
        doc.autoTable({
                margin: {top: 50}, // Seting top margin for First Page.
                didDrawPage: function (data) {
                    // Reseting top margin. The change will be reflected only after print the first page.
                    data.settings.margin.top = 10; 
        });

Thank you so much for your code!! It works perfectly

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrico1234 picture andrico1234  路  5Comments

ultra2 picture ultra2  路  6Comments

cwolcott picture cwolcott  路  4Comments

cplaiu picture cplaiu  路  3Comments

prakashsam picture prakashsam  路  4Comments