首页 > 其他分享 >print-js打印

print-js打印

时间:2024-05-26 09:01:45浏览次数:17  
标签:printJS images 打印 js Print print

Print.js--网页文件打印

 官网地址是:https://printjs.crabbly.com/

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: '[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'
    }
 ]
基本
 <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>
请用今天的努力,让明天没有遗憾。

标签:printJS,images,打印,js,Print,print
From: https://www.cnblogs.com/tomcat2022/p/18213340

相关文章

  • (免费领取源码)计算机毕业设计项目:07558基于Python的校园宿舍(开题答辩+程序定制+全套文
    摘要本论文主要论述了如何使用django开发一个校园宿舍管理系统,本系统将严格按照软件开发流程进行各个阶段的工作,采用B/S架构,面向对象编程思想进行项目开发。在引言中,作者将论述校园宿舍管理系统的当前背景以及系统开发的目的,后续章节将严格按照软件开发流程,对系统进行各......
  • THREE.JS中 CubeTextureLoader 使用避坑
    最近在跟着教程学THREE.JS,毕竟在现在的前端开发市场上,THREE.JS太火爆了。今天学到“纹理”这一块的时候,跟着教程敲代码,发现自己的没有正确显示,百思不得其解,打开控制台发现,如下WARNING:localhost/:1[.WebGL-000060380A191C00]GL_INVALID_VALUE:Eachcubemapfacemusthavee......
  • python爬虫js逆向(标准算法加密)
    在爬取某些网站时,可能会遇到一些参数或头部信息进行字符串加密、数据加密或字符串编码等问题,导致不能正确获取到我们想要的数据。step1观察是否有加密通过正常方法请求网页,并且也加了请求头和请求参数伪装浏览器,发现返回的状态码还是4xx,那么就要考虑到是否是请求头或者请求......
  • formilyjs/element-plus 设置中文无效的原因
    工作中用到了这个库,但是使用国际化设置中文却不行,所以查看源码发现了不行的原因直接说结论,不浪费大家时间,不是使用方法有问题,就是这个库自身的bug,想要设置中文,要么改源码要么直接使用原生element原理分析先看看element是如何设置成中文的这是官网的写法,那么看看这个组件内......
  • @JsonFormat注解出现日期序列化以及反序列化问题(日期比实际日期少一天)
    文章目录前言一、场景如下所示二、问题分析三、@JsonFormat注解是什么以下是@JsonFormat注解的一些常用属性:四、解决问题解决方式:只需要指定对应的时区就好效果如下:五、@JsonFormat注解时出现日期问题总结前言  在一次的偶然机会下发现了一个小问题,场景是这......
  • js实现歌词自动轮转及播放时间跳转
    需求实现歌词滚动提示和歌词点击跳转功能思路歌词滚动:将歌词解析成两个数组,一个用于存放时间,一个用于存放歌词,下标一致对应,利用定时器对每毫秒轮询时间数组,若一致,显示歌词变红。歌词点击播放:判断点击歌词在数组中的下标,读取时间数组中的时间,赋值播放时间代码歌词滚动.html<......
  • Content-Type 'application/json;charset=UTF-8' is not supported异常解决
    Content-Type'application/json;charset=UTF-8'isnotsupported异常解决前提:确定不是因为Content-Type导致的异常,controller层有注解@RequestBody。报错详情:确定不是因为缺少Jackson依赖或者版本过低:注意到报错信息上边有一条警告日志:.c.j.MappingJackson2HttpMessageCo......
  • 只打印一次(状态改变时)
    为了防止日志一直打印输出,只在状态改变时打印一次#include<iostream>intmain(){/////////////////////////////////////////对于两种状态切换情况{boolm_bOnline=true;//默认在线boolm_bFlag1=true;//默认都是“开”bo......
  • js的闭包原理——通过引擎的堆栈解析
    有段代码如下:functioncreateCounter(){leti=0;functionincrement(){i++;}functiongetValue(){returni;}return{increment,getValue}}constcounter=createCounter();在这段代码中,运用了函数的3个特点:在函......
  • Nodejs安装及配置,包含Windows和Linux两种平台
    目录1.下载安装包2.Windows下安装3.Linux下安装4.使用Vite创建Vue项目5.结语Node.js是前端开发的必备工具,特别是在使用Vue或React开发项目时,需要npm安装依赖、运行开发环境以及项目打包,这里就分别介绍一下在Windows和Linux平台如何安装Nodejs。文章结尾,笔者还会......