Coder Social home page Coder Social logo

printtemplate's Introduction

print-template

模板打印

安装

npm install print-template
#
pnpm install print-template

使用

import printTemplate from 'print-template'

let  template  = new printTemplate()

//添加模板
template.push({
  name:'template1', // 模板名称
  unit:'mm',        // 尺寸  mm / px
  size:'a4',        // a4 或 [221,291]
  fixed:[           // 固定的数据
    {
      type:'line' ,   // 类型 线条  line / text / image / barcode / qrcode
      x:10,           // x
      y:20,           // y
      length:10,      // 长度
      orientation:'l',// 方向 默认 l 横向  / p 竖向
      width:0.3,      // 线段宽度 (1.2.8版本新增)
      color:'blue'    // 线段颜色 (1.2.8版本新增)
    },
    {
      type:'text' ,          // 类型  文本 line / text / image / barcode / qrcode
      default:'张三',        // 默认值  \n 换行  或设置maxWidth大小
      x:10,                  // x
      y:20,                  // y
      fontSize:'20',         // 字体大小
      fontFamily:'微软雅黑',  // 字体类型   默认 '微软雅黑'
      fontWeight:null,       // 字体宽度
      fontStyle:'normal',    // 字体样式 normal / 斜体
      maxWidth:null,         // 最大换行宽度
      orientation:'l',       // 方向 默认 l 横向  / p 竖向
      color:'red'            // 字体颜色 (1.2.8版本新增)
    },
     {
      type:'image' ,        // 图片  文本 line / text / image / barcode / qrcode
      default:'图片地址',    // *默认值  图片地址 或 base64
      x:10,                 // *x
      y:20,                 // *y
      width: 25,            // *宽度
      height: 25,           // *高度
    },
    {
      type:'barcode' ,      // 条形码  文本 line / text / image / barcode / qrcode
      default:'OK111111',   // *默认值  条形码值
      x:10,                 // *x
      y:20,                 // *y
      width: 3,             // *线宽
      height: 25,           // *高度
      format: 'CODE128A',   // 条形码格式
      fontOptions: 'bold'   // 粗体   bold / italic / bold  italic
    },
    {
      type:'qrcode' ,      // *条形码  文本 line / text / image / barcode / qrcode
      default:'二维码文本',  // *默认值
      x:10,                 // *x
      y:20,                 // *y
      width: 25,            // *宽度
    }

  ],
  data:{                    // 动态数据
    code1: {                // 传入数据的属性名称
      type:'barcode' ,      // 条形码  文本 line / text / image / barcode / qrcode
      x:10,                 // *x
      y:20,                 // *y
      width: 3,            // *线宽度
      height: 25,           // *高度
      format: 'CODE128A',   // 条形码格式
    },
    code2:{                 // 传入数据的属性名称
      type:'qrcode' ,       // *条形码  文本 line / text / image / barcode / qrcode
      x:10,                 // *x
      y:20,                 // *y
      width: 25,            // *宽度
    }

  }

})

// 打印模板
// 异步方法 返回 jspdf
let pdf = await template.print(
  'template1',          // 刚才添加的模板名称
  [                     // 打印数据  {} 或 [{},{},{}]
    {
      code:'YOO11111111',  // 条形码 CODE128A 类型 大写字母加数字
      code2:'记得start',   // 二维码
    },
     {
      code:'T00222222',  // 条形码 CODE128A 类型 大写字母加数字
      code2:'记得start',   // 二维码
    }
  ])


// 下载 / 预览

// blob 地址
let uri = pdf.output('bloburi', { filename: '打印文件' });

// 下载
let fileName = 'test'
pdf.save( fileName ||'打印文件' );

多页模版

{

  name:'template1',
  unit:'mm',
  size:'a4',
  multiPage: true,  // treu :  多页模板  false : 默认 单页
  // 分页方式  传入打印每行的数据 返回要打印的信息
  // - 返回 null 继续打印当前模版 
  // - 返回 [] 空数组什么也不打印,当前页也不打印
  // - 返回 [{name:'模版名称',data:{打印对象}},{name:'模版名称',data:{打印对象}] 打印多页信息
  pageMode: function (data) {
    // 判断 存在 phone 属性 打印 当前模板 和 template1 模板
    if (data.phone) {
      return [{ name: this.name, data }, { name: 'template1', data: { text: data.phone } }]
    } 
    return null
  },
  // 关联 模板 : 打印时验证存不存在模板 如果为空或模板为空 不生成pdf页
  linkTemplate: ['template1','template1'],
  ... // 其他属性
}

例子

例子

下载

printtemplate's People

Stargazers

Talent.Miao avatar  avatar guixue avatar  avatar  avatar Huang Q avatar  avatar  avatar  avatar  avatar caicy avatar  avatar Kaer avatar 黄亮 avatar  avatar Hulk Liu avatar ride avatar  avatar  avatar 张志华 avatar

Watchers

James Cloos avatar Hulk Liu avatar  avatar  avatar

printtemplate's Issues

字体不清晰

你好,大神 利用模板生成的PDF 出来后字体不清晰 打印出来很模糊 ,有没有好的办法啊, 宋体、黑体效果都不是很理想

字体支持

你好,大神,现在的打印字体能支持其他fontFamily么?最近好像'微软雅黑'不让用了,谢谢

模板页面尺寸设置无效

name: 'expressDelivery1',
unit: 'mm',
size: [76, 130],

name: 'expressDelivery1',
unit: 'mm',
size: [130, 76],

生成的pdf,页面大小一致

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.