jspdf:将图像添加到pdf但是abobe reader无法读取文件?

时间:2016-12-27 05:45:38

标签: javascript angularjs jspdf

我在我的项目中使用jspdf将一些图表转换为pdf。我的框架是 angularjs 1.5.6 ,图表由 chart.js 生成。 html片段如下:

<div name="charts" class="charts">

    <div class="ft" style="width:45%; height:550px; margin-top: 20px;margin-left:2%;">
        <canvas id="biChart" class="chart chart-bar" chart-data="biData" chart-labels="biLabels" chart-options="biOptions" chart-series="series" chart-colors="biColors" chart-dataset-override="biDatasetOverride"></canvas>
    </div>

    <div class="fr" style="width:45%; height:550px; margin-top: 20px;margin-right:2%;">
        <canvas id="qzsChart" class="chart chart-bar" chart-data="qzsData" chart-labels="qzsLabels" chart-options="qzsOptions" chart-series="series" chart-colors="qzsColors" chart-dataset-override="qzsDatasetOverride"></canvas>
    </div>

</div>

导出功能如下:

$scope.exportChart = function() {
        var biChart = document.getElementById("biChart");
        var qzsChart = document.getElementById("qzsChart");
        var doc = new jsPDF('p', 'mm');
        var text = "Sample Charts";
        var xOffset = (doc.internal.pageSize.width / 2) - (doc.getStringUnitWidth(text) * doc.internal.getFontSize() / 2);
        doc.text(text, xOffset, 10);
        doc.addImage(biChart.toDataURL('image/png'), 'PNG', 10, 10);
        doc.addPage();
        doc.addImage(qzsChart.toDataURL('image/png'), 'PNG', 10, 10);
        doc.save('sample-chart.pdf');
    }

现在结果是,我可以在浏览器中看到生成的pdf。但是,当我使用adobe reader打开文件时,它说:

open wrong message

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

You can give image in your html part.

JS代码

var pdf = new jsPDF('l','pt','a4');
                pdf.addHTML($('#invoicearea'),function() {
                    var courseenrollmentid = $(".courseenrollmentid").text();
                    pdf.output("save", "InvoiceReceipt"+courseenrollmentid+".pdf");
                });    

HTML CODE

 <div id="invoicearea" style="background-color: white; padding: 20px;">
        <div class="row-fluid">
            <div class="span6">
                <img style="width:20%;" alt=""
                    src="<%=request.getContextPath()%>/assets/app/images/invoicelogo.png">
            </div>
            <div class="span6">
                <address class="invoiceheader">
                    <h4 class="righttext martop20">Invoice Number : {{#if orderno}}<span class="courseenrollmentid">{{orderno}}</span>
                        {{else}}<span class="courseenrollmentid">-</span>{{/if}}</h4>
                    <h4 class="righttext martop20">Invoice Date: {{paymentdate}}</h4>
                </address>
            </div>
        </div>
        <h3 class="invoicereceipt">Invoice Receipt</h3>
        <div class="invoicepart">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>Course Title</th>
                        <th>Course Author</th>
                        <th>Price<span class="rubee" style='font-family;'>(&#8377;)</span></th>

                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>{{coursetitle}}</td>
                        <td>{{firstname}}</td>
                        <td>{{price}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="row-fluid">
            <address class="invoiceaddress">
                <h4>For Details Contact</h4>
                <h4><s:text name="label.productname"></s:text></h4>      
            </address>
        </div>
    </div>