Выравнивание текста справа не работает после загрузки PDF с помощью jsPDF - PullRequest
0 голосов
/ 30 января 2020

У меня есть счет-фактура, и я хочу загрузить его в формате PDF, но после загрузки PDF в текстовом формате не работает выравнивание текста.

Мой код: -

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

    // we support special element handlers. Register them with jQuery-style 
    // ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
    // There is no support for any other type of selectors 
    // (class, of compound) at this time.
    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
    };
    // all coords and widths are in jsPDF instance's declared units
    // 'inches' in this case
    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) {
        // dispose: object with X, Y of the last line add to the PDF 
        //          this allow the insertion of new lines after html
        pdf.save('invoice.pdf');
    }, margins);
}
  .invoice-container {
            width: 100%;
            height: auto;
            background: #fff;
            padding: 20px;
            box-shadow: 2px 4px 10px rgba(39, 59, 69, 0.2);
        }

        .invoive-table thead {
            background: #93dbf7;
        }
        .fl-left{float: left;}
        .fl-right{float: right;}
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"
        integrity="sha256-gJWdmuCRBovJMD9D/TVdo4TIK8u5Sti11764sZT1DhI=" crossorigin="anonymous"></script>
<div class="container" id="customers">
        <div class="col-md-10 col-md-offset-1">
            <div class="invoice-container">
                <div class="row">
                    <div class="col-md-12">
                        <button class="pull-right btn btn-primary" onclick="javascript:demoFromHTML();">Download PDF</button>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                    <div class="fl-left">
                        <h3>Invoice</h3>
                        <p>Bill To</p>
                        <p>Company Name</p>
                        <p>New Delhi</p>
                        <p>India</p>
                    </div>
                    <div class="fl-right">
                        <h3>&nbsp;</h3>
                        <p>Invoice: 12345</p>
                        <p>Terms : Net30</p>
                        <p>Date : 29/01/2020</p>
                        <p>Due Date : 29/01/2020</p>
                    </div>
                </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="table-responsive">
                            <table class="table table-bordered invoive-table">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Activity</th>
                                        <th>Qty</th>
                                        <th>Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        <td>29/01/2020</td>
                                        <td>School Work</td>
                                        <td>2</td>
                                        <td>500 /-</td>
                                    </tr>
                                    <tr>
                                        <td>29/01/2020</td>
                                        <td>School Work</td>
                                        <td>2</td>
                                        <td>1000 /-</td>
                                    </tr>
                                    <tr>
                                        <td>29/01/2020</td>
                                        <td>School Work</td>
                                        <td>2</td>
                                        <td>1500 /-</td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td></td>
                                        <td>Subtotal</td>
                                        <td>3000 /- </td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td></td>
                                        <td>GST (18%)</td>
                                        <td>540 /-</td>
                                    </tr>
                                    <tr>
                                        <td></td>
                                        <td></td>
                                        <td>Total</td>
                                        <td>3540 /-</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>


            </div>
        </div>
    </div>
...