JSPDF无法正确打印,显示空白pdf

时间:2019-03-18 14:16:57

标签: javascript jquery asp.net-core jspdf

我们在aspnet核心应用中使用jsPDF。这是脚本部分:

@section Scripts{

    <script src="~/lib/jsPDF/jspdf.min.js" type="text/javascript" asp-append-version="true"></script>
    <script src="~/lib/jsPDF/split_text_to_size.js" type="text/javascript" asp-append-version="true"></script>
    <script src="~/lib/jsPDF/standard_fonts_metrics.js" type="text/javascript" asp-append-version="true"></script>

    <script src="~/lib/jsPDF/html2canvas.min.js" asp-append-version="true"></script>
    <script src="~/lib/jsPDF/html.js" type="text/javascript" asp-append-version="true"></script>

    <script src="~/js/dailyjobreport.js" type="text/javascript" asp-append-version="true"></script>

}

从AJAX调用中接收到html,并将其用于pdf渲染

function onSuccess(data) {
        try {
            var pdf = new jsPDF({
                orientation: 'portrait',
                format: 'a4'
            });

            pdf.html(data);
            pdf.save('report.pdf');
        } catch (e) {
            console.error(e);
        }
}

呈现的pdf是空白页面。任何人都可以帮助找到我们代码的问题或我们缺少什么

1 个答案:

答案 0 :(得分:0)

尝试使用回调函数保存为pdf:

<script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
<script src="https://html2canvas.hertzen.com/dist/html2canvas.min.js"></script>

Js功能:

$(function () {
    var pdf = new jsPDF({
        orientation: 'portrait',
        format: 'a4'
    });


    pdf.html("<html><head></head><body><h2>2asdasda123dsdasd</h2></body></html>", {
        callback: function (pdf) {
            pdf.save('test.pdf');
        }
    });

})