【问题标题】:Create pdf using jsPDF with formatted Table data使用带有格式化表格数据的 jsPDF 创建 pdf
【发布时间】:2014-04-11 16:42:21
【问题描述】:

我可以使用以下脚本从 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
    )

编辑:

我也试过这个 sample 下面的函数,但我得到的只是空的 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');
}

【问题讨论】:

标签: jquery html html-table jspdf export-to-pdf


【解决方案1】:

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

【讨论】:

  • 这很好!感谢您的贡献!
  • 非常感谢!它现在正在使用这个插件!
  • 非常可爱的插件。感谢分享。
  • 感谢您的贡献
【解决方案2】:

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

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

How to set column width for generating pdf using jsPDF

【讨论】:

    【解决方案3】:

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

    var doc = new jsPDF('p','pt', 'a4', true);
    

    【讨论】:

    • 文档说 boolean 是 putOnlyusedFonts 选项,它只包括 PDF 中使用的字体。
    【解决方案4】:

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

    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);
    }
    

    【讨论】:

    【解决方案5】:
    $(".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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 2022-10-05
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      相关资源
      最近更新 更多