14

我可以使用下面的脚本从 html 表中生成 PDF 文件:但是我正在逐行获取所有列数据。

请帮助我以表格格式的方式生成 PDF 文件。(带有列边框、边距或填充、标题)在此脚本中

我使用 jsPDF lib 脚本生成一个 html 表格到 PDF 。

 var pdf = new jsPDF('p', 'pt', 'letter')   
    , source = $('#TableId')[0] 
    , specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function(element, renderer){           
            return true
        }
    }

    , margins = {
             top: 20,
             bottom: 20,
             left: 30,
             width: 922
         };


    pdf.fromHTML(
        source // HTML string or DOM elem ref.
        , margins.left // x coord
        , margins.top // y coord
        , {
            'width': margins.width // max width of content on PDF
            , 'elementHandlers': specialElementHandlers
        },
        function (dispose) {          
          pdf.save('Test.pdf');
        },
        margins
    )

编辑:

我也在下面的函数中尝试过这个示例,但我得到的只是空的 pdf 文件。

function exportTabletoPdf()
{
    var doc = new jsPDF('p','pt', 'a4', true);
    var header = [1,2,3,4];
    doc.table(10, 10, $('#test').get(0), header, {
    left:10,
    top:10,
    bottom: 10,
    width: 170,
    autoSize:false,
    printHeaders: true
    });
    doc.save('sample-file.pdf');
}
4

5 回答 5

55

我花了很多时间寻找我的表格的良好表示,然后我发现了这个插件(https://github.com/simonbengtsson/jsPDF-AutoTable),它工作得很好,包括主题、行跨度、列跨度、从html,与 json 一起使用,您还可以个性化您的标题并使它们成为水平的。下图是一个示例: jsPDF-AutoTable

于 2015-10-26T18:27:16.370 回答
2

你必须使用类似 -- doc.setLineWidth(2);

用于线条边框.. 请查看以下示例代码

如何设置使用jsPDF生成pdf的列宽

于 2014-04-30T10:35:58.903 回答
1

尝试从此方法中删除最后一个参数“true”:

var doc = new jsPDF('p','pt', 'a4', true);
于 2015-03-14T18:01:00.140 回答
1

使用 jspdf 包含脚本https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js导出 html div 内容,包括纯文本和表格数据

function download_DIVPdf(divid) {
    var pdf = new jsPDF('p', 'pt', 'letter');
    var pdf_name = 'PostMode-'+om+'.pdf';
    // source can be HTML-formatted string, or a reference
    // to an actual DOM element from which the text will be scraped.
    htmlsource = $('#'+divid)[0];

    specialElementHandlers = {
        // element with id of "bypass" - jQuery style selector
        '#bypassme': function (element, renderer) {
            // true = "handled elsewhere, bypass text extraction"
            return true
        }
    };
    margins = {
        top: 80,
        bottom: 60,
        left: 40,
        width: 522
    };

    pdf.fromHTML(
    htmlsource, // HTML string or DOM elem ref.
    margins.left, // x coord
    margins.top, { // y coord
        'width': margins.width, // max width of content on PDF
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {                

        pdf.save(pdf_name);
    }, margins);
}
于 2017-05-02T07:55:54.400 回答
0
$(".gridview td").each(function () {
      var value = $(this).html();
      doc.setFontSize(8);

      if (count == 1) {
          if (height > 278) {
              doc.rect(10, inc, 24, 8);
              doc.rect(34, inc, 111, 8);
              doc.rect(145, inc, 15, 8);
              doc.rect(160, inc, 20, 8);
              doc.rect(180, inc, 23, 8);

              doc.addPage(focus);
              doc.setLineWidth(0.5);
              inc = 15;
              height = 18;
          }


          doc.rect(10, inc, 24, 8);
          doc.text(value, 11, height);         
     }
     if (count == 2) {
          doc.rect(34, inc, 111, 8);
          var splitdesc = doc.splitTextToSize(value, 100);
          doc.text(splitdesc, 35, height);
     }
     if (count == 3) {
         doc.rect(145, inc, 15, 8);
         doc.text(value, 147, height);
         qty = value;
     }
     if (count == 4) {
        doc.rect(160, inc, 20, 8);
        doc.text(value, 163, height);

        amt = value;
     }
     if (count == 5) {
        doc.rect(180, inc, 23, 8);

        tot = parseInt(qty) * parseFloat(amt);
        doc.text("" + tot, 182, height);
        count = 0;

        height = height + 8;
        netamt = netamt + parseFloat(tot);

        inc = parseInt(inc) + 8;
        doc.rect(10, inc, 24, 8);
        doc.rect(34, inc, 111, 8);
        doc.rect(145, inc, 15, 8);
        doc.rect(160, inc, 20, 8);
        doc.rect(180, inc, 23, 8);
    }
    count = count + 1;
});
于 2015-05-20T06:33:13.423 回答