JsPDF在每个新页面上重复第一行

时间:2019-05-08 12:52:02

标签: javascript arrays jspdf jspdf-autotable

我正在使用jspdf保存PDF。我有javascript数组。但是不了解JsPDF的功能是自动在每页上重复pdf的第一行行并且它们是重叠的。此外,每页生成的最后一张图像被截断并仅显示一半,有没有办法解决这两个问题。所以。附加页面的屏幕快照。谢谢!!!

function jsGeneratePDF2(){
        console.log('myImageArray105: '+myImageArray);
        doc.filename = 'Test.pdf';
        doc.page = 1;
        doc.setFontSize(10);// optional
        doc.autoTable({
            head: headRows(),
            body: bodyRows(10),
            theme: 'plain',//'grid',//'plain',
            startY: 50, 
            showHead: 'everyPage',
            styles: {overflow: 'linebreak', cellWidth: 'wrap'},
            bodyStyles :{valign: 'middle', minCellHeight: dim},
            columnStyles: {ProdInfo:{cellWidth: dim},
                           UOM:{cellWidth: dim/2},
                           Barcode:{cellWidth: dim},
                          },
            didDrawCell: function() {
                    for (var i = 0; i < gtinStrings.length; i++) {
                        doc.addImage(myImageArray[i], 'PNG', dim*3, 70+dim*i, dim*2, dim*2, i,'FAST');

                    }
                }

           });
       [![enter image description here][1]][1]
         doc.fromHTML($('#contentForPdf').html().replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
                         , margins.left,    margins.top, 
                         {  
                             'width': margins.width //// max width of content on PDF
                             ,'elementHandlers': specialElementHandlers
                         },

                         //Add new page and increase page count             
                         function(bla){


                             var string = doc.output('datauristring');
                             var iframe = "<iframe width='100%' height='100%' src='" + string + "'></iframe>";
                             var x = window.open();
                             x.document.open();
                             x.document.write(iframe);
                             x.document.close();


                         },margins); 

        }

1 个答案:

答案 0 :(得分:0)

我自己修复了以下问题。数据来自$ Ajax结果。

data =(77)[{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{ …},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…} ,{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{ …},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…} ,{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{ …},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…},{…} ,{…},{…},{…},{…}];

$.ajax({
                url: "{{ route('yourUrl') }}",
                method :"POST",
                dataType:"json",
                data:{"_token": "{{ csrf_token() }}"},
                success:function(data)
                {    
                    console.log(data);

                    $.each(data, function(i, value)
                    {
                        fileName = value.Date + ' - ' + 'Sales Report';

                        var itemNew = [{ no:i+1, code:value.RepCode, rep:value.RepName, inC:value.Count, tot:value.TotalAmt }];

                        itemNew.forEach(element => {      
                            var temp = [element.no,element.code, element.rep,element.inC, element.tot];                       
                            rows.push(temp);
                        });

                    });

                    doc.autoTable(col, rows, {

                            theme: "grid",
                            tableWidth: 'auto',
                            margin: {right:30,left:30},
                            startY: 0.05*doc.internal.pageSize.height,
                            styles: {font: 'arial', fontSize: 8, cellPadding: 1, valign: 'middle', halign: 'center'}
                          });

                    doc.save(fileName+'.pdf');
                }                       
            });

因此,请检查您的'jsGeneratePDF2()'函数是否正确调用。它应该在循环外调用。