jsPdf add margins to pdf page

后端 未结 1 1788
清酒与你
清酒与你 2020-12-18 14:01

I use jsPdf for creating pdf from html. How can I add margings (top, left, right) to my pdf page?

     var doc = new jsPDF(\'p\', \'pt\', \'letter\');
    d         


        
1条回答
  •  旧时难觅i
    2020-12-18 14:34

    JSPdf allows you to make a margin hash and apply it in your download i.e.

    margins = {
      top: 40,
      bottom: 60,
      left: 40,
      width: 522
    };
    

    Try this snippet below or this CodePen:

    $(document).ready(function() {
      $(".btn").click(function() {
        var doc = new jsPDF("p", "pt", "letter"),
        source = $("#template_invoice")[0],
        margins = {
          top: 40,
          bottom: 60,
          left: 40,
          width: 522
        };
        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
        );
      });
    });
    .btn-info,
    .lead {
      margin-top: 20px;
    }
    
    
    

    Invoice

    Order # 12345


    Billed To:
    John Smith
    1234 Main
    Apt. 4B
    Springfield, ST 54321
    Shipped To:
    Jane Smith
    1234 Main
    Apt. 4B
    Springfield, ST 54321
    Payment Method:
    Visa ending **** 4242
    jsmith@email.com
    Order Date:
    March 7, 2014

    Order summary

    Item Price Quantity Totals
    BS-200 $10.99 1 $10.99
    BS-400 $20.00 3 $60.00
    BS-1000 $600.00 1 $600.00
    Subtotal $670.99
    Shipping $15
    Total $685.99

    0 讨论(0)
提交回复
热议问题