在我的應用程式中,我想以更時尚的方式生成最終報告,目前我習慣于在 HTML/CSS 視圖中顯示報告。
然后我想嘗試列印它,但是當它列印時,它帶有基本視圖(例如沒有表格,資料排列不當等)。所以我想知道是否可以將此視圖發送到視圖中顯示的列印件?
我找到了一個用于列印操作的 javascript,這是我用于列印的。
<script type="text/javascript">
function PrintElem(elem) {
Popup($(elem).html());
}
function Popup(data) {
var mywindow = window.open('', 'divprint', 'height=400,width=600');
mywindow.document.write('<html><head><title></title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
return true;
}
</script>
或者當用戶單擊列印然后打開該pdf進行列印時,有沒有辦法將其轉換為pdf?我還想在生成它時將此視圖設定為 A4 大小。
這就是我現在的看法。謝謝

uj5u.com熱心網友回復:
將此庫添加到您的專案中,下載鏈接:https : //ekoopmans.github.io/html2pdf.js/
HTML代碼:
<div id="dPDF">
Your all html here which you want to print
</div>
<div class="generate-pdf" onclick="downloadPDF()">
Generate PDF (Button for generating pdf)
</div>
生成PDF的函式:
function downloadPDF() {
const element = document.getElementById("dPDF");
var opt = {
margin: 0.5,
filename: 'your_filename.pdf',
image: { type: 'jpeg', quality: 1 },
html2canvas: { scale: 4, logging: true },
jsPDF: { unit: 'in', format: 'a4', orientation: 'portrait' }
};
html2pdf().set(opt).from(element).save();
}
注意:通過生成和下載 pdf,您可以列印您想要的確切視圖。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/343187.html
