簡體   English   中英

使用 print.js 自定義頁邊距

[英]Custom page margins using print.js

我正在嘗試使用 Print.js 庫使用熱敏 POS 打印機打印付款收據。 我可以使用 printjs 成功打印頁面。 但是按照 A4 頁面,打印件帶有額外的邊距。
有沒有辦法打印它以適應 58 毫米和頁邊距?
如果提供使用 JavaScript 進行打印的任何替代方法,這也將非常有幫助。 以下是我的作品。

HTML & JS

<button id="print-receipt">print to pos</button>
<div id="invoice-print-div" class="hidden">
    <div class="text-center" style="width:58mm;background-color:red;">
        <h4>alpha</h4>
        <table border="0">
            <tr>
                <th>Item</th>
                <th>Qty</th>
                <th>Price</th>
            </tr>
            <tr>
                <td>abc</td>
                <td>1</td>
                <td>120</td>
            </tr>
            <tr>
                <td>pqr</td>
                <td>2</td>
                <td>240</td>
            </tr>
            <tr>
                <td>xyz</td>
                <td>4</td>
            <td>360</td>
            </tr>
        </table>
    </div>
</div>
<script>
$("#print-receipt").on("click", function() {
    printJS("invoice-print-div", "html");
});
</script>

1-您可以將 object 作為參數傳遞,而不是將 printJS() 與默認的兩個 arguments 一起使用:

printJS('invoice-print-div', 'html'); 應替換為:

printJS({printable: 'invoice-print-div', type: 'html',style: ['@page { size: A4; margin: 0mm;} body {margin: 0;} h4 {margin:0}'], targetStyles: ['*']});

請注意,當涉及到實際打印時,參數 object 中的屬性樣式是您可以將您的自定義樣式傳遞給正在打印的 html 的地方。

我在stackblitz中做了一個演示,請檢查一下。

有關更多信息,您可以查看PrintJS 配置選項

2- 當您要求 PrintJS 的一些替代品時,您可以查看 jsPDF 庫在此處進行一些解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM