【问题标题】:jsPDF, fromHTML plugin does not create PDF and says undefinedjsPDF,fromHTML 插件不创建 PDF 并说未定义
【发布时间】:2018-11-15 09:55:03
【问题描述】:

以下 HTML 和 JavaScript 用于创建收据 PDF。 PDF 将在收据打印机中打印,纸张尺寸为 80 毫米宽。并且打印高度将按三部分(页眉、页脚和购买项目)的长度计算。

我尝试使用此代码,但它没有创建要打印的 PDF。

JavaScript

$(document).ready(function()
{
    $(".btn").click(function()
    {
        var contentHeight; // header + footer + number of buying items
        var doc = new jsPDF("p", "mm", [80, contentHeight]),
            source = $("#template_invoice"),
            margins = {
                top: 10,
                //bottom: 60,
                left: 5,
                width: 80    // Receipt printer width
            };
        doc.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
            },
            function(dispose)
            {
                // dispose: object with X, Y of the last line add to the PDF
                // this allow the insertion of new lines after html
                doc.save("Test.pdf");
            },
            margins
        );
    });
});

HTML

<!DOCTYPE html>
<html lang="en" >
   <head>
      <meta charset="UTF-8">
      <title>Receipt PDF</title>
      <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
      <link rel="stylesheet" href="css/style.css">
      <style>
         table{
         font-family: 'Courier New', Courier, monospace;
         }
         td#aliCenter{
         text-align:center;
         }
         td#aliRight{
         text-align:right;
         }
      </style>
   </head>
   <body>
      <div class="container" id="template_invoice">
         <table>
            <!-- header -->
            <tr>
               <td colspan="5" id="aliCenter">ABC Restaurant<br>Sunny Road, Island<br>Contact : 123456789</td>
            </tr>
            <tr>
               <td colspan="5" id="aliCenter">15/11/2018 02:14:52&nbsp;&nbsp;&nbsp;&nbsp;IamCoder&nbsp;&nbsp;No: 150</td>
            </tr>
            <tr>
               <td colspan="5">
                  <hr>
               </td>
            </tr>
            <tr>
               <td id="aliCenter">NO</td>
               <td id="aliCenter">ITEM</td>
               <td id="aliCenter">QTY</td>
               <td id="aliCenter">PRICE</td>
               <td id="aliCenter">AMOUNT</td>
            </tr>
            <tr>
               <td colspan="5">
                  <hr>
               </td>
            </tr>
            <!-- buying items -->
            <tr>
               <td>1</td>
               <td colspan="4">The big tasty Pizza</td>
            </tr>
            <tr>
               <td></td>
               <td>PZ4545</td>
               <td>2.00</td>
               <td id="aliRight">150.00</td>
               <td id="aliRight">300.00</td>
            </tr>
            <tr>
               <td>2</td>
               <td colspan="4">Crunchy huge Buggers</td>
            </tr>
            <tr>
               <td></td>
               <td>BR7878</td>
               <td>5.00</td>
               <td id="aliRight">500.00</td>
               <td id="aliRight">2500.00</td>
            </tr>
            <tr>
               <td>3</td>
               <td colspan="4">1.5 l Coca-Cola pack</td>
            </tr>
            <tr>
               <td></td>
               <td>CC6523</td>
               <td>3.00</td>
               <td id="aliRight">180.00</td>
               <td id="aliRight">540.00</td>
            </tr>
            <!-- footer -->
            <tr>
               <td colspan="5">
                  <hr>
               </td>
            </tr>
            <tr>
               <td></td>
               <td colspan="3">Net Total</td>
               <td id="aliRight">3340.00</td>
            </tr>
            <tr></tr>
            <tr>
               <td></td>
               <td colspan="3">Cash</td>
               <td id="aliRight">3500.00</td>
            </tr>
            <tr>
               <td></td>
               <td colspan="3">Balance</td>
               <td id="aliRight">160.00</td>
            </tr>
            <tr></tr>
            <tr>
               <td colspan="5" id="aliCenter">-----------IMPORTANT NOTICE-----------</td>
            </tr>
            <tr>
               <td colspan="5" id="aliCenter">In case of a price discrepancy, <br>return the bill and item within <br> 2 days to refund the difference</td>
            </tr>
         </table>
         <div class="row">
            <div class="col-xs-4">
               <button class="btn btn-info pull-right">Download</button>
            </div>
         </div>
      </div>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
      <script src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js'></script>
      <script  src="js/index.js"></script>
   </body>
</html>

【问题讨论】:

  • 你的源是一个数组使用源[0]
  • 我应该在哪里使用?
  • source = $("#template_invoice")[0], 这是正确的吗?但不起作用
  • 你有另一个错误 Uncaught TypeError: Cannot read property 'name' of undefined github.com/MrRio/jsPDF/issues/516
  • @JibinMathews,那我该怎么用呢?

标签: javascript html jspdf


【解决方案1】:

你得到一个错误:

未捕获的类型错误:无法读取未定义的属性“名称”

因为colspans and rowspans are not supported。而且样式也不支持。

我用jsPDF插件为你写了解决方案。

解决方法/解决方案

function alignCol(cell, data, colCount)
{
    cell.styles.fillColor = false;

    var col = data.column.index;
    if(colCount == 1)
        cell.styles.halign = 'center';
    else
    {
        if(colCount == 2)
            cell.styles.halign = 'left';
        else
        {
           if(col < colCount - 1)
                cell.styles.halign = 'left';
            else cell.styles.halign = 'right'
        }
    }
}

$(document).ready(function()
{
    $(".btn").click(function()
    {
        var options =
            {
                margin: 0,
                colCount: 1,
                tableWidth: 226.772,
                drawHeaderRow: function(){return false},
                drawHeaderCell: function(){return false},
                createdCell: function(cell, data){alignCol(cell, data, options.colCount)}
            },
            tableWidths5 =
            {
                0: {columnWidth: 26},
                1: {columnWidth: 50},
                2: {columnWidth: 45},
                3: {columnWidth: 45},
                4: {columnWidth: 60}
            };

        // Only pt supported (not mm or in)
        var contentHeight = 400; // header + footer + number of buying items
        //80 mm = 226.772 pt
        var doc = new jsPDF('p', 'pt', [contentHeight, 226.772]);
        doc.setFontSize(3);
        doc.setFont('Courier New');
        doc.autoTable([0], [['ABC Restaurant\nSunny Road, Island\nContact : 123456789\n15/11/2018   02:14:52   IamCoder   No: 150']], options);

        doc.line(0, 60, 226, 60);
        options.columnStyles = tableWidths5;
        options.startY = 60;
        options.colCount = 5;
        doc.autoTable([0,0,0,0,0], [['NO','ITEM','QTY','PRICE','AMOUNT']], options);

        doc.line(0, 80, 226, 80);
        options.columnStyles = {0: {columnWidth: 26}};
        options.startY = 80;
        options.colCount = 2;
        doc.autoTable([0,0], [[1,'The big tasty Pizza']], options);

        options.columnStyles = tableWidths5;
        options.startY = 100;
        options.colCount = 5;
        doc.autoTable([0,0,0,0,0], [[' ','PZ4545','2.00','150.00','300.00']], options);

        options.columnStyles = {0: {columnWidth: 26}};
        options.startY = 120;
        options.colCount = 2;
        doc.autoTable([0,0], [[2,'Crunchy huge Buggers']], options);

        options.columnStyles = tableWidths5;
        options.startY = 140;
        options.colCount = 5;
        doc.autoTable([0,0,0,0,0], [[' ','BR7878','5.00','500.00','2500.00']], options);

        options.columnStyles = {0: {columnWidth: 26}};
        options.startY = 160;
        options.colCount = 2;
        doc.autoTable([0,0], [[3,'1.5 l Coca-Cola pack']], options);

        options.columnStyles = tableWidths5;
        options.startY = 180;
        options.colCount = 5;
        doc.autoTable([0,0,0,0,0], [[' ','CC6523','3.00','180.00','540.00']], options);

        doc.line(0, 210, 226, 210);
        options.columnStyles = {0: {columnWidth: 26}};
        options.startY = 220;
        options.colCount = 3;
        doc.autoTable([0,0,0], [
            ['','Net Total','3340.00'],
            ['','Cash','3500.00'],
            ['','Balance','160.00']
        ], options);

        options.columnStyles = void 0;

        options.startY = 295;
        options.colCount = 1;
        doc.autoTable([0], [['----------- IMPORTANT NOTICE -----------\n\nIn case of a price discrepancy, return\nthe bill and item within 2 days\nto refund the difference.']], options);
        
        doc.save('test.pdf');
    });
});
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<center><button class="btn btn-info">Download PDF</button></center>

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.5/jspdf.plugin.autotable.js"></script>

【讨论】:

  • 谢谢@Bharata。但是那个宽度是80厘米吗?因为热敏纸宽度是 80 厘米
  • @IamCoder,您必须将 80 毫米(不是厘米!!!)转换为 pt,因为 jsPDF-autoTable 插件仅支持 pt 大小。您可以在需要时将其转换为 here。我的示例中的 PDF 为 80 毫米宽。感谢 StackOverflow 是通过投票和接受回答来完成的。如果您对我上面的回答感到满意,请在我的回答左侧标记为已接受并点赞。
  • 谢谢。由于纸张尺寸,我告诉了 80 毫米。是否可以通过使用 JSON 数组在前端使用它们来更改项目(美味的大披萨、松脆的巨型臭虫......)和那些相关数据(QTY, AMOUNT, CODE, PRICE, NET TOTAL , CASH)?否则,它似乎是硬编码的。
  • @IamCoder,没有什么是硬编码的。您可以使用 JS 在前端更改所有尺寸。我的示例中的 PDF 为 80 毫米宽。是的,可以更改 JS 中的所有项目。
  • 好的,但是您的代码包含所有代码 我应该在哪个地方提供 json ?喜欢var data = ["items": [{ "no": "1", "code":"PZ4545", "item": "The big tasty Pizza", "price": "500.00", "qty":"2.00", "amount":"1000.00"}, {"no": "2", "code":"BR7878", "item": "Crunchy huge Buggers", "price": "250.00", "qty":"2.00", "amount":"500.00"}, { "no": "3", "code":"CC6523", "item": "1.5 l Coca-Cola pack", "price": "180.00", "qty":"3.00", "amount":"540.00" }]]
猜你喜欢
  • 2016-08-27
  • 2014-02-28
  • 2018-06-25
  • 2016-08-18
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-28
相关资源
最近更新 更多