jQuery打印无法在嵌入src上工作

时间:2018-06-27 12:03:05

标签: jquery html

我想打印嵌入的内容(pdf),但是以下代码不起作用。

$("#printLabel").on('click', function () {
    $("#printContent").print({
        deferred: $.Deferred().done(function () {
            //$('#modal-addEdit').modal('hide');
        })
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.print/1.5.1/jQuery.print.js"></script>
<div>
<embed id="printContent" src="https://www.alejandrodelasota.org/wp-content/uploads/2013/03/demoform1.pdf">
</div>
<div>
	<button class="print-link no-print" id="printLabel">Print</button>
</div>

打印预览显示为空白。

1 个答案:

答案 0 :(得分:0)

我尝试打印一阵子,并且可以使用jquery使其工作,所以我最终使用了这个普通的js。或尝试使用此链接https://coderwall.com/p/c5xzpw/printing-the-contents-of-a-div-with-javascript

function printRequest(e) {      

    var content = document.getElementById("printerBox").innerHTML;
    var mywindow = window.open('', 'Print', 'height=600,width=800');   

    mywindow.document.write('<html><head><title>Print</title>');
    mywindow.document.write('</head><body >');
    mywindow.document.write(content);
    mywindow.document.write('</body></html>');

    mywindow.document.close();
    mywindow.focus()
    mywindow.print();
    mywindow.close();
    return true;
}

<div hidden id="printerBox">

</div>