0.1.0 • Published 1 year ago

@browser-print/core v0.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

@browser-print/core

原生js打印工具

Usage

  1. local print
var handleBeforePrint = function() {
  console.log('before print')
}
var handleAfterPrint = function() {
  console.log('after print')
}

new Printer({
  id: 'print-content',
  iframeTitle: 'print',
  extraHead: [
    '<meta http-equiv="Access-Control-Allow-Origin" content="*">'
  ],
  onBeforePrint: handleBeforePrint,
  onAfterPrint: handleAfterPrint,
})
  1. url print
new Printer({
  url: 'http://localhost:8897/vite-react.html',
  onBeforePrint: handleBeforePrint,
  onAfterPrint: handleAfterPrint,
})
  1. async url print
new Printer({
  getUrlAsync: function(resolve) {
    setTimeout(function() {
      resolve('http://localhost:8897/vite-react.html')
    }, 2000)
  },
  onBeforePrint: handleBeforePrint,
  onAfterPrint: handleAfterPrint,
})

Options

enum HTMLStandards {
  Strict = 'strict',
  Loose = 'loose',
  HTML5 = 'html5',
}

interface PrinterOptions {
  id?: string;
  standard?: HTMLStandards;
  extraHead?: string[];
  extraCss?: string[];
  iframeTitle?: string;
  url?: string;
  onBeforePrint?: () => void;
  onAfterPrint?: () => void;
  getUrlAsync?: (resolve: (url: string) => void) => void;
}
OptionDescription
id需要被打印的元素id
standard设置 iframe html 标准
extraHead添加额外的元素到 iframe head 标签下
extraCss添加额外的 css url 地址
iframeTitle设置 iframe title
url用于 url 打印
onBeforePrint执行打印之前的回调
onAfterPrint执行打印之后的回调
getUrlAsync用于 async url 打印