vue项目使用Print.js插件实现PDF文件打印

简介: vue项目使用Print.js插件实现PDF文件打印

一,Print.js介绍

Print.js主要是为了帮助我们在应用程序中直接打印PDF文件,而不需要离开界面,也不需要使用嵌入。对于用户不需要打开或下载PDF文件的特殊情况,只需要打印即可。

例如,当用户请求打印在服务器端生成的报告时,这是有用的一种情况。这些报告以PDF文件的形式发回。打印这些文件之前无需打开这些文件。Print.js提供了一种在我们的应用程序中快速打印这些文件的方法。

PDF文件必须与您的应用程序所在的域相同。Print.js在打印文件之前使用iframe加载文件,因此,它受到同源策略的限制。这有助于防止跨站点脚本(XSS)攻击。

  • 原生js,不依赖其它库
  • 可指定打印(或不打印)区域
  • 支持css样式(内联、外联、嵌入)
  • 支持input(radio/checkbox/text)、select、textarea值获取

二,下载安装

Print.js下载地址:https://download.csdn.net/download/weixin_43025151/87606898

或者使用npm安装

npm install print-js --save

引入到自己的项目中:

import print from 'print-js'

三,配置项

Print.js将接受一个对象作为参数,您可以在其中配置一些选项:

四,实现思路与项目示例

实现思路:将目标区域的dom/css添加到空iframe中,打印该iframe。

1) pdf的打印
<button type="button" onclick="printJS('docs/printjs.pdf')">
        Print PDF
     </button>
2)base64
<button type="button" onclick="printJS({printable: base64, type: 'pdf', base64: true})">
    Print PDF with Message
  </button>
3)html的打印
<form method="post" action="#" id="printJS-form">
     ...
   </form>
   <button type="button" onclick="printJS('printJS-form', 'html')">
     Print Form
   </button>
4)图片的打印
<img src="images/print-01.jpg" />
   printJS('images/print-01-highres.jpg', 'image')
   
   //打印多张图片
    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;'
     })
5)打印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>

整个vue文件:

<template>
  <div id="app">
    <div id="divPrint" class="mainbox print">
      <!-- <div style="font-size: 25px; margin-top: 80px" align="center">
        整改通知单
      </div> -->
      <div align="center" style="margin: 0; padding: 0; width: 100%">
        <table>
          <tr style="border: none">
            <td colspan="2">工程项目:xx</td>
            <td style="text-align: right; padding-left: 10px;" colspan="2">编号:xx</td>
          </tr>
          <tr style="border: none">
            <td colspan="2">整改单位:xx</td>
            <td style="text-align: right; padding-left: 10px;" colspan="2">编号:xx</td>
          </tr>
          <tr style="border: none">
            <td colspan="2">整改内容:xx</td>
            <td style="text-align: right; padding-left: 10px;" colspan="2">编号:xx</td>
          </tr>
        </table>
      </div>
    </div>
    <div style="margin-top: 20px; text-align: center">
      <button @click="print()">pdf打印</button>
    </div>
  </div>
</template>
<script>
import printJS from "print-js";
export default {
  name: "printing",
  data() {
    return {};
  },
  methods: {
    print() {
      printJS({
        printable: "divPrint", // 标签元素id
        type: "html", // 打印类型
        header: "整改通知单", // '表单名称',
        targetStyles: ["*"],
        style: "@page {margin:0 10mm};", // 可选-打印时去掉眉页眉尾
        ignoreElements: ["no-print"], // 接受打印父 html 元素时应忽略的 html id 数组。
        properties: null,
      });
    },
  },
};
</script>
<style lang="scss">
#app {
  width: 100%;
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  .mainbox {
    width: 100%;
    margin: 0 auto;
  }
}
</style>

点击打印按钮后,出现PDF预览。

五,分析:

经过多次实战演练:

如果要打印的样式比较复杂,并且有字体图标等,建议选择第一种;

如果打印表格数据,比如element表格,还可以按需勾选进行打印,选择第二种打印方式比较好。

再分享一个强制分页打印,每页只打印固定的内容:

只需要在要分页打印的末尾加上:

<div style="page-break-after:always"></div>

即可

代码如下:

<div id="printTable"
                   v-for="(arr, i) in saveListArray"
                   :key="i"
                   style="position: relative; background-color:rgb(236,230,232);margin-top:5px;"
                   :class="printWay == 3 ? 'printTableActive' : ''"
                   :style="{
                    width: containerWidth + 'mm',
                    height: containerHeight + 'mm',
                   }">
                <el-container v-for="(item, index) in arr"
                              :key="index">
                  <div id="printColor"
                       v-bind:style="{
                    width: Number(item.GoodInvoiceDetailWidth) + 'mm',
                    height: item.GoodInvoiceDetailHeight + 'mm',
                    'line-height': item.GoodInvoiceDetailHeight + 'mm',
                    left: Number(item.GoodInvoiceDetailCoordinateX) + Number(x) + 'mm',
                    top: Number(item.GoodInvoiceDetailCoordinateY) + Number(y) + 'mm',
                    color: item.GoodInvoiceDetailColour + '',
                    'font-size': item.GoodInvoiceDetailSize + 'mm',
                    'font-family': item.GoodInvoiceDetailFont + '',
                    'text-align': item.GoodInvoiceDetailPosition + '',
                    'border-top': item.GoodFramSize*0.1+0.1 + 'mm' + item.GoodFramSolidlineIf + item.borderTcolor,
                    'border-bottom': item.GoodFramSize*0.1+0.1 + 'mm' + item.GoodFramSolidlineIf + item.borderBcolor,
                    'border-left': item.GoodFramSize*0.1+0.1 + 'mm' + item.GoodFramSolidlineIf + item.borderLcolor,
                    'border-right': item.GoodFramSize*0.1+0.1 + 'mm' + item.GoodFramSolidlineIf + item.borderRcolor,
                    overflow: item.overflow,
                    }"
                       style=" display: inline-block;
                    text-align:left;
                    position: absolute;
                    cursor: move;">
                    <div v-if="item.GraphContent"><img :src="item.GraphContent"
                           style="width:100%;height:100%;margin-top:4px;"></div>
                    <div v-else
                         style="margin-right:3px;margin-left:3px;">{{ item.GoodInvoiceDetailName }}</div>
                  </div>
                </el-container>
                <div style="page-break-after:always"></div>
              </div>

下面展示两种图片:(一张没有加分页的,和加了分页的区别)

六,注意

不支持background-color背景色打印,试试用background-image代替,只在浏览器中预览打印,未实际打印过, 公司无公用打印机,低级浏览器兼容性待验证。

目录
相关文章
|
8月前
|
前端开发 JavaScript
个人征信电子版无痕修改, 个人信用报告pdf修改,js+html+css即可实现【仅供学习用途】
本代码展示了一个信用知识学习系统的前端实现,包含评分计算、因素分析和建议生成功能。所有数据均为模拟生成
|
5月前
|
存储 数据处理 数据库
公募REITs公告PDF文档处理项目
本项目是一个专门用于处理基础设施公募REITs(Real Estate Investment Trusts)公告PDF文件的完整RAG数据处理管道,也适用于其他公告PDF文件,应用多模态大模型,可高效提升文本提取内容。系统能够自动化地将PDF公告文档转换为结构化数据,能够检测表格、实现跨页表格拼接,并将表格内容还原为便于检索的文本信息。并构建向量数据库和 Elasticsearch 以支持智能检索与问答系统。
公募REITs公告PDF文档处理项目
|
7月前
|
C#
【PDF提取内容改名】批量提取PDF指定区域内容重命名PDF文件,PDF自动提取内容命名的方案和详细步骤
本工具可批量提取PDF中的合同编号、日期、发票号等关键信息,支持PDF自定义区域提取并自动重命名文件,适用于合同管理、发票处理、文档归档和数据录入场景。基于iTextSharp库实现,提供完整代码示例与百度、腾讯网盘下载链接,助力高效处理PDF文档。
899 40
|
7月前
|
缓存 测试技术 网络安全
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
131 0
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
|
7月前
|
编译器 Python
如何利用Python批量重命名PDF文件
本文介绍了如何使用Python提取PDF内容并用于文件重命名。通过安装Python环境、PyCharm编译器及Jupyter Notebook,结合tabula库实现PDF数据读取与处理,并提供代码示例与参考文献。
|
9月前
|
人工智能 算法 安全
使用CodeBuddy实现批量转换PPT、Excel、Word为PDF文件工具
通过 CodeBuddy 实现本地批量转换工具,让复杂的文档处理需求转化为 “需求描述→代码生成→一键运行” 的极简流程,真正实现 “技术为效率服务” 的目标。感兴趣的快来体验下把
496 10
|
8月前
|
数据采集 存储 API
Python爬虫结合API接口批量获取PDF文件
Python爬虫结合API接口批量获取PDF文件
|
8月前
|
前端开发
个人征信PDF无痕修改软件,个人征信模板可编辑,个人征信报告p图神器【js+html+css仅供学习用途】
这是一款信用知识学习系统,旨在帮助用户了解征信基本概念、信用评分计算原理及信用行为影响。系统通过模拟数据生成信用报告,涵盖还款记录
|
10月前
|
存储 JSON API
如何将 Swagger 文档导出为 PDF 文件
你会发现自己可能需要将 Swagger 文档导出为 PDF 或文件,以便于共享和存档。在这篇博文中,我们将指导你完成将 Swagger 文档导出为 PDF 格式的过程。
|
人工智能 编解码 文字识别
OCRmyPDF:16.5K Star!快速将 PDF 文件转换为可搜索、可复制的文档的命令行工具
OCRmyPDF 是一款开源命令行工具,专为将扫描的 PDF 文件转换为可搜索、可复制的文档。支持多语言、图像优化和多核处理。
1259 17
OCRmyPDF:16.5K Star!快速将 PDF 文件转换为可搜索、可复制的文档的命令行工具

热门文章

最新文章