当前位置:网站首页>Print. JS -- web page file printing
Print. JS -- web page file printing
2022-07-03 14:06:00 【Cupid510】
Print.js
There are four types of printed documents available :’ pdf ‘,’ html ',' Images ’ and json.
The default type is ’ pdf '.
Its basic usage is to call printJS() Just enter one PDF Document URL :printJS('docs/PrintJS.pdf ').
For image files , The idea is the same , But you need to pass the second parameter :printJS('images/PrintJS.jpg ‘,’ image ‘).
To print HTML Elements , In a similar way , Incoming elements id And type :printJS(’ myelementtid ‘,’ html ‘).
Print JSON Data time , Pass in the data to be printed 、 Type and data attributes : printJS({ Printable :myData, type :’ json ', attribute :[‘prop1
‘,’ prop2 ‘,’ prop3’]});
Download and install
npm/yarn install
npm install print-js --save
yarn add print-js
Introduce in the project
import print from ‘print-js’
use cdn It can also be introduced directly
https://printjs-4de6.kxcdn.com/print.min.js
https://printjs-4de6.kxcdn.com/print.min.css
Getting started
Introduce <script src="print.js"></script>
If you use modal Need to introduce css file <link rel="stylesheet" type="text/css" href="print.css">
Document printing
Add a button to print PDF file :
<button type="button" onclick="printJS('docs/printjs.pdf')">
Print PDF
</button>

For large documents , You can display a message to the user when the file is loaded
<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">
Print PDF with Message
</button>
Support base64 PDF Print :
<button type="button" onclick="printJS({printable: base64, type: 'pdf', base64: true})">
Print PDF with Message
</button>
HTML Print
Sometimes we just want to print HTML Selected parts of the page , It can be tricky . Use Print.js, We can easily pass the elements we want to print id. The element can be any label , As long as it has a unique id. The library will try to print it very close to what it looks like on the screen , meanwhile , It will create a printer friendly format for it .
Form printing


Printing with parameters , Such as the title
<button type="button" onclick="printJS({ printable: 'printJS-form', type: 'html', header: 'PrintJS - Form Element Selection' })">
Print Form with Header
</button>
Image printing
Picture printing
printJS('images/print-01-highres.jpg', 'image')
add headers
printJS({
printable: 'images/print-01-highres.jpg', type: 'image', header: 'My cool image header'})
Print multiple pictures
printJS({
printable: ['images/print-01-highres.jpg', 'images/print-02-highres.jpg', 'images/print-03-highres.jpg'],
type: 'image',
header: 'Multiple Images',
imageStyle: 'width:50%;margin-bottom:20px;'
})
JSON Print
someJSONdata = [
{
name: 'John Doe',
email: '[email protected]',
phone: '111-111-1111'
},
{
name: 'Barry Allen',
email: '[email protected]',
phone: '222-222-2222'
},
{
name: 'Cool Dude',
email: '[email protected]',
phone: '333-333-3333'
}
]
basic
<button type="button" onclick="printJS({printable: someJSONdata, properties: ['name', 'email', 'phone'], type: 'json'})">
Print JSON Data
</button>
Custom style
<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;'
})">
Print JSON Data
</button>
Custom table title name
<button type="button" onclick="printJS({
printable: someJSONdata,
properties: [
{
field: 'name', displayName: 'Full Name'},
{
field: 'email', displayName: 'E-mail'},
{
field: 'phone', displayName: 'Phone'}
],
type: 'json'
})">
Print with custom table header text
</button>
Add table title text
<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>
边栏推荐
- 如何使用lxml判断网站公告是否更新
- 小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。
- Software testing is so hard to find, only outsourcing offers, should I go?
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- [how to earn a million passive income]
- JS download files through URL links
- 金属有机骨架MIL-88负载阿霉素DOX|叶酸修饰UiO-66-NH2负载阿霉素[email protected]纳米粒子
- 金属有机骨架(MOFs)抗肿瘤药载体|PCN-223装载甲硝唑|UiO-66包载盐酸环丙沙星([email protected])
- Golang - command line tool Cobra
- QT learning 17 dialog box and its types
猜你喜欢

Multi person collaborative data annotation based on Baidu brain easydata from scratch
![[技術發展-24]:現有物聯網通信技術特點](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技術發展-24]:現有物聯網通信技術特點

小项目(servelt+jsp+mysql+EL+JSTL)完成一个登录功能的Servlet,具有增删改查的操作。实现登录身份验证,防止非法登录,防止多点登录,记住用户名密码功能。

Qt学习22 布局管理器(一)

Redis:字符串类型数据的操作命令

GoLand 2021.2 configure go (go1.17.6)

Comprehensive case of MySQL data addition, deletion, modification and query

【吉林大学】考研初试复试资料分享

FPGA测试方法以Mentor工具为例

JVM family - overview, program counter day1-1
随机推荐
1px problem of mobile terminal
Use vscode to view hex or UTF-8 codes
Uniapp tips - set background music
Comprehensive case of MySQL data addition, deletion, modification and query
SQL Injection (POST/Select)
Richview trvstyle liststyle list style (bullet number)
MIL-100( Fe) 包裹小分子阿司匹林形成[email protected](Fe)|甘草次酸修饰金属有机框架材料UiO-66-NH2(简称UiO-66-NH2-GA)
Cross linked cyclodextrin metal organic framework loaded methotrexate slow-release particles | metal organic porous material uio-66 loaded with flavonoid glycosides | Qiyue
信创产业现状、分析与预测
Common plug-ins for vite project development
Conversion function and explicit
Qt学习17 对话框及其类型
[技术发展-24]:现有物联网通信技术特点
Article content typesetting and code highlighting
Uniapp skills - dom display and hiding
Mastering the cypress command line options is the basis for truly mastering cypress
使用vscode查看Hex或UTF-8编码
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
MySQL 数据增删改查综合案例
Similarities and differences of sessionstorage, localstorage and cookies