当前位置:网站首页>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>
边栏推荐
- Redis:Redis的数据结构、key的操作命令
- Uniapp tips - scrolling components
- Go language unit test 3: go language uses gocovey library to do unit test
- MIL-100( Fe) 包裹小分子阿司匹林形成[email protected](Fe)|甘草次酸修饰金属有机框架材料UiO-66-NH2(简称UiO-66-NH2-GA)
- Current situation, analysis and prediction of information and innovation industry
- There is nothing new under the sun. Can the meta universe go higher?
- 28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
- [acnoi2022] guess numbers
- 【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
- windos 创建cordova 提示 因为在此系统上禁止运行脚本
猜你喜欢
Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
Message subscription and publishing
Solve MySQL 1045 access denied for user 'root' @ 'localhost' (using password: yes)
Uniapp tips - scrolling components
[développement technologique - 24]: caractéristiques des technologies de communication Internet des objets existantes
Why are grass-roots colleges and universities with "soil and poverty" called "Northeast small Tsinghua"?
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
如何使用lxml判断网站公告是否更新
JVM family - overview, program counter day1-1
解决MySql 1045 Access denied for user ‘root‘@‘localhost‘ (using password: YES)
随机推荐
28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;
[ACNOI2022]猜数
Leetcode-1175.Prime Arrangements
交联环糊精金属有机骨架负载甲氨蝶呤缓释微粒|金属-有机多孔材料UiO-66负载黄酮苷类药物|齐岳
JS new challenges
金属有机骨架MOFs装载非甾体类抗炎药物|ZIF-8包裹普鲁士蓝负载槲皮素(制备方法)
怎样删除对象的某个属性或⽅法
[556. Next larger element III]
MySQL data processing value addition, deletion and modification
[机缘参悟-37]:人感官系统的结构决定了人类是以自我为中心
Mastering the cypress command line options is the basis for truly mastering cypress
Selenium browser (1)
GoLand 2021.2 configure go (go1.17.6)
[understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
Thrift threadmanager and three monitors
Vite project commissioning
Depth and breadth first traversal of tree (regardless of binary tree)
使用vscode查看Hex或UTF-8编码
Current situation, analysis and prediction of information and innovation industry
Go 1.16.4: purpose of go mod tidy