Print.js--网页文件打印

Print.js
有四种打印文档类型可用:’ pdf ‘,’ html ','图像’和json。

默认类型是’ pdf '。
它的基本用法是呼叫printJS()只需输入一个PDF文档网址:printJS('docs/PrintJS.pdf ')。
对于图像文件,想法是一样的,但是您需要传递第二个参数:printJS('images/PrintJS.jpg ‘,’ image ‘)。
要打印HTML元素,以类似的方式,传入元素id并键入:printJS(’ myelementtid ‘,’ html ‘)。
打印JSON数据时,传入要打印的数据、类型和数据属性: printJS({可打印:myData,类型:’ json ',属性:[‘prop1
‘,’ prop2 ‘,’ prop3’]});

下载并安装

npm/yarn安装

npm install print-js --save
yarn add print-js

在项目中引入

import print from ‘print-js’

用cdn也可以直接引入

https://printjs-4de6.kxcdn.com/print.min.js
https://printjs-4de6.kxcdn.com/print.min.css

入门指南

在需要的文件内引入 <script src="print.js"></script>
如果你使用了modal 需要引入css文件<link rel="stylesheet" type="text/css" href="print.css">

文件打印

添加一个按钮来打印位于您的托管服务器上的PDF文件:

 <button type="button" onclick="printJS('docs/printjs.pdf')">
    Print PDF
 </button>

在这里插入图片描述

对于大文件,您可以在加载文件时向用户显示消息
<button type="button" onclick="printJS({printable:'docs/xx_large_printjs.pdf', type:'pdf', showModal:true})">
    Print PDF with Message
 </button>
支持base64 PDF打印:

 <button type="button" onclick="printJS({printable: base64, type: 'pdf', base64: true})">
    Print PDF with Message
 </button>

HTML打印

有时我们只想打印HTML页面的选定部分,这可能很棘手。使用Print.js,我们可以轻松地传递想要打印的元素的id。元素可以是任何标签,只要它有唯一的id。该库将尝试打印它非常接近它在屏幕上的样子,同时,它将为它创建一个打印机友好的格式。

表单打印

在这里插入图片描述
在这里插入图片描述

带有参数的打印,比如标题
 <button type="button" onclick="printJS({ printable: 'printJS-form', type: 'html', header: 'PrintJS - Form Element Selection' })">
    Print Form with Header
 </button>

图像打印

图片打印
 printJS('images/print-01-highres.jpg', 'image')
添加页眉
 printJS({printable: 'images/print-01-highres.jpg', type: 'image', header: 'My cool image header'})
打印多个图片
 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打印

 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'})">
    Print JSON Data
 </button>
自定义样式
 <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>
自定义表格标题名称
 <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>
添加表格标题文本
<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>
posted @ 2022-03-03 08:42  Cupid05  阅读(3826)  评论(0编辑  收藏  举报