Print.js javascript庫 實(shí)現(xiàn)頁面打印
官網(wǎng)地址
您可以從GitHub版本下載最新版本的Print.js
下載v1.0.61
npm 安裝
npm install print-js --save
npm安裝時(shí)將庫導(dǎo)入項(xiàng)目
import print from 'print-js'
首先,我們需要在頁面上包含Print.js庫和樣式
<script src="print.js"></script>
<link rel="stylesheet" type="text/css" href="print.css">
下面主要介紹一下我們要用的打印方法printJS();
printJS()將接受一個(gè)對(duì)象作為參數(shù),下面表格的參數(shù)則為該對(duì)象的屬性,通過控制屬性值來控制打印;
| 參數(shù) | 默認(rèn)值 | 說明 |
|---|---|---|
| printable: | null | 文檔來源:pdf或圖像的url,html元素的id或json數(shù)據(jù)的對(duì)象 |
| type: | 可打印類型??捎玫拇蛴∵x項(xiàng)包括:pdf,html,image,json和raw-html。 | |
| header: | null | 用于HTML,Image或JSON打印的可選標(biāo)頭。它將放在頁面頂部。此屬性將接受文本或原始HTML |
| headerStyle: | 'font-weight:300;' | 要應(yīng)用于標(biāo)題文本的可選標(biāo)題樣式 |
| maxWidth: | 800 | 最大文檔寬度(像素)。根據(jù)需要更改此項(xiàng)。在打印HTML,圖像或JSON時(shí)使用。 |
| css: | null | 這允許我們傳遞一個(gè)或多個(gè)應(yīng)該應(yīng)用于正在打印的html的css文件URL。值可以是包含單個(gè)URL的字符串,也可以是包含多個(gè)URL的數(shù)組。 |
| style: | null | 這允許我們傳遞一個(gè)字符串,該字符串應(yīng)該應(yīng)用于正在打印的html。 |
| scanStyles: | true | 設(shè)置為false時(shí),庫不會(huì)處理應(yīng)用于正在打印的html的樣式。使用css參數(shù)時(shí)很有用。 |
| targetStyle: | null | 默認(rèn)情況下,在打印HTML元素時(shí),庫僅處理某些樣式。此選項(xiàng)允許您傳遞要處理的樣式數(shù)組。例如:['padding-top','border-bottom'] |
| targetStyles: | null | 與targetStyle相同,這將處理任何一系列樣式。例如:['border','padding'],將包括'border-bottom','border-top','border-left','border-right','padding-top'等。你也可以傳遞['*']來處理所有樣式 |
| ignoreElements: | [] | 接受打印父html元素時(shí)應(yīng)忽略的html的id數(shù)組。 |
| properties: | null | 在打印JSON時(shí)使用。這些是對(duì)象屬性名稱。 |
| gridHeaderStyle: | 'font-weight:bold;' | 打印JSON數(shù)據(jù)時(shí)網(wǎng)格標(biāo)題的可選樣式。 |
| gridStyle: | 'border: 1px solid lightgray; margin-bottom: -1px;' | 打印JSON數(shù)據(jù)時(shí)網(wǎng)格行的可選樣式 |
| repeatTableHeader: | true | 在打印JSON數(shù)據(jù)時(shí)使用。設(shè)置為時(shí)false,數(shù)據(jù)表標(biāo)題僅顯示在第一頁中。 |
| showModal: | null | 啟用此選項(xiàng)可在檢索或處理大型PDF文件時(shí)顯示用戶反饋 |
| modalMessage: | 'Retrieving Document...' | 當(dāng)向用戶顯示的消息showModal被設(shè)定為true。 |
| onLoadingStart: | null | 加載PDF時(shí)要執(zhí)行的功能 |
| onLoadingEnd: | null | 加載PDF后要執(zhí)行的功能 |
| documentTitle: | 'Document' | 打印html,image或json時(shí),它將顯示為文檔標(biāo)題。如果用戶嘗試將打印作業(yè)保存為pdf文件,它也將是文檔的名稱。 |
| fallbackPrintable: | null | 打印pdf時(shí),如果瀏覽器不兼容(檢查瀏覽器兼容性表),庫將在新選項(xiàng)卡中打開pdf。這允許您傳遞要打開的不同pdf文檔,而不是傳遞給printable的原始文檔。如果您在備用pdf文件中注入javascript,這可能很有用。 |
| onPdfOpen : | null | 打印pdf時(shí),如果瀏覽器不兼容(檢查瀏覽器兼容性表),庫將在新選項(xiàng)卡中打開pdf??梢栽诖颂巶鬟f回調(diào)函數(shù),這將在發(fā)生這種情況時(shí)執(zhí)行。在您想要處理打印流程,更新用戶界面等的某些情況下,它可能很有用。 |
| onPrintDialogClose: | null | 關(guān)閉瀏覽器打印對(duì)話框后執(zhí)行回調(diào)功能 |
| onError: | error => throw error | 發(fā)生錯(cuò)誤時(shí)要執(zhí)行的回調(diào)函數(shù)。 |
| base64: | false | 在打印作為base64數(shù)據(jù)傳遞的PDF文檔時(shí)使用 |
| honorMarginPadding(不建議使用): | true | 這用于保留或刪除正在打印的元素的填充和邊距。有時(shí)這些樣式設(shè)置在屏幕上很棒,但在打印時(shí)看起來很糟糕。您可以通過將其設(shè)置為false來刪除它。 |
| honorColor(不建議使用) : | false | 要以彩色打印文本,請(qǐng)將此屬性設(shè)置為true。默認(rèn)情況下,所有文本都將以黑色打印。 |
| font(不建議使用): | 'TimesNewRoman' | 打印HTML或JSON時(shí)使用的字體 |
| font_size(不建議使用): | '12pt' | 打印HTML或JSON時(shí)使用的字體大小 |
| imageStyle (不建議使用): | 'width:100%;' | 打印圖像時(shí)使用。接受包含要應(yīng)用于每個(gè)圖像的自定義樣式的字符串。 |
| frameId: | null | print.js會(huì)將要打印的內(nèi)容復(fù)制到一個(gè)新的Frame中,此參數(shù)是frame的id值 |
示例
HTML打印
有時(shí)我們只想打印HTML頁面的選定部分,這可能很棘手。使用Print.js,我們可以輕松傳遞我們要打印的元素的id。該元素可以是任何標(biāo)記,只要它具有唯一ID即可。該庫將嘗試將其打印得非常接近它在屏幕上的外觀,同時(shí),它將為它創(chuàng)建一個(gè)打印機(jī)友好的格式。
<form method="post" action="#" id="printJS-form">
...
</form>
<button type="button" onclick="printJS('printJS-form', 'html')">
Print Form
</button>
向表單添加標(biāo)題
<button type="button" onclick="printJS({ printable: 'printJS-form', type: 'html', header: '這是標(biāo)題內(nèi)容' })">
打印
</button>
html時(shí)候需要注意的事項(xiàng):
直接采用默認(rèn)的參數(shù)打印html的時(shí)候如果打印內(nèi)容過多時(shí)候可能會(huì)非常慢,這時(shí)候需要設(shè)置scanStyles:false,然后把css抽取出來,這樣速度就會(huì)非常的快!
printJS({ printable: 'myHtmlElement', type: 'html', scanStyles: false, css: '/my_stylesheet.css' })
圖片的打印
通過傳遞圖像網(wǎng)址,Print.js可用于快速打印頁面上的任何圖像。當(dāng)您使用低分辨率版本的圖像在屏幕上顯示多個(gè)圖像時(shí),此功能非常有用。當(dāng)用戶嘗試打印所選圖像時(shí),您可以將高分辨率URL傳遞給Print.js。
<img src="images/print-01.jpg" />
printJS('圖片的url', 'image')
添加標(biāo)題
printJS({printable: 'images/print-01-highres.jpg', type: 'image', header: '這是標(biāo)題'})
打印多張圖片
printJS({
printable: ['images/print-01-highres.jpg', 'images/print-02-highres.jpg', 'images/print-03-highres.jpg'],
type: 'image',
header: '標(biāo)題僅顯示在第一張圖片',
imageStyle: 'width:50%;margin-bottom:20px;'
})
JSON的打印
一種簡(jiǎn)單快捷的方法來打印動(dòng)態(tài)數(shù)據(jù)或javascript對(duì)象數(shù)組。
var someJSONdata = [
{
name: 'John Doe',
email: 'john@doe.com',
phone: '111-111-1111'
},
{
name: 'Barry Allen',
email: 'barry@flash.com',
phone: '222-222-2222'
},
{
name: 'Cool Dude',
email: 'cool@dude.com',
phone: '333-333-3333'
}
]
<button type="button"
onclick="printJS({printable: someJSONdata, properties: ['name', 'email', 'phone'], type: 'json'})">
打印
</button>
自定義css來設(shè)置數(shù)據(jù)網(wǎng)格的樣式:
<button type="button" onclick="printJS({
printable: someJSONdata,
properties: ['name', 'email', 'phone'],
type: 'json',
gridHeaderStyle: 'color: red; border: 2px solid #3971A5;',
gridStyle: 'border: 2px solid #3971A5;'
})">
打印
</button>
自定義發(fā)送對(duì)象數(shù)組的表頭文本
<button type="button" onclick="printJS({
printable: someJSONdata,
properties: [
{ field: 'name', displayName: '自定義表頭'},
{ field: 'email', displayName: '自定義表頭'},
{ field: 'phone', displayName: '自定義表頭'}
],
type: 'json'
})">
打印
</button>
JSON,HTML和圖像打印可以接收原始HTML標(biāo)頭
<button type="button" onclick="printJS({
printable: someJSONdata,
type: 'json',
properties: ['name', 'email', 'phone'],
header: '<h3 class="custom-h3">My custom header</h3>',
style: '.custom-h3 { color: red; }'
})">
Print header raw html
</button>
感謝WwJoyous的分享
浪客行1213的簡(jiǎn)書

xhh