打印源码封装及调用

// {
//   /** Class to pass to the print window body */
//   bodyClass?: string;
//   /** Content to be printed */
//   content: Function;
//   /** Copy styles over into print window. default: true */
//   copyStyles?: boolean;
//   /** Set the title for printing when saving as a file */
//   documentTitle?: string;
//   /** */
//   fonts?: { family: string, source: string }[];
//   /** Callback function to trigger after print */
//   onAfterPrint?: () => void;
//   /** Callback function to trigger before page content is retrieved for printing */
//   onBeforeGetContent?: () => void | Promise<any>;
//   /** Callback function to trigger before print */
//   onBeforePrint?: () => void | Promise<any>;
//   /** Callback function to listen for printing errors */
//   onPrintError?: (errorLocation: "onBeforeGetContent" | "onBeforePrint" | "print", error: Error) => void;
//   /** Override default print window styling */
//   pageStyle?: string | Function<string>;
//   /** Override the default `window.print` method that is used for printing */
//   print?: (target: HTMLIFrameElement) => Promise<any>;
//   /** Remove the iframe after printing. */
//   removeAfterPrint?: boolean;
//   /** Suppress error messages */
//   suppressErrors?: boolean;
//   /** Set the nonce attribute for whitelisting script and style -elements for CSP (content security policy) */
//   nonce?: string;
// }

const defaultProps = {
  copyStyles: true,
  pageStyle: `
      @page {
          /* Remove browser default header (title) and footer (url) */
          margin: 0;
      }
      @media print {
          body {
              /* Tell browsers to print background colors */
              -webkit-print-color-adjust: exact; /* Chrome/Safari/Edge/Opera */
              color-adjust: exact; /* Firefox */
          }
      }
  `,
  removeAfterPrint: false,
  suppressErrors: false,
}

class VueToPrint {
  constructor (props = {}) {
    this.props = Object.assign(defaultProps, props)
  }

  startPrint (target) {
    const {
      onAfterPrint,
      onPrintError,
      print,
      documentTitle,
    } = this.props

    setTimeout(() => {
      if (target.contentWindow) {
        target.contentWindow.focus() // Needed for IE 11

        if (print) {
          print(target)
            .then(this.handleRemoveIframe)
            .catch((error) => {
              if (onPrintError) {
                onPrintError('print', error)
              } else {
                this.logMessages(['An error was thrown by the specified `print` function'])
              }
            })
        } else if (target.contentWindow.print) {
          const tempContentDocumentTitle = target.contentDocument?.title ?? ''
          const tempOwnerDocumentTitle = target.ownerDocument.title

          // Override page and various target content titles during print
          // NOTE: some browsers seem to take the print title from the highest level
          // title, while others take it from the lowest level title. So, we set the title
          // in a few places and hope the current browser takes one of them :pray:
          if (documentTitle) {
            // Print filename in Chrome
            target.ownerDocument.title = documentTitle

            // Print filename in Firefox, Safari
            if (target.contentDocument) {
              target.contentDocument.title = documentTitle
            }
          }

          target.contentWindow.print()

          // Restore the page's original title information
          if (documentTitle) {
            target.ownerDocument.title = tempOwnerDocumentTitle

            if (target.contentDocument) {
              target.contentDocument.title = tempContentDocumentTitle
            }
          }
        } else {
          // Some browsers, such as Firefox Android, do not support printing at all
          // https://developer.mozilla.org/en-US/docs/Web/API/Window/print
          this.logMessages(['Printing for this browser is not currently possible: the browser does not have a `print` method available for iframes.'])
        }

        if (onAfterPrint) {
          onAfterPrint()
        }

        this.handleRemoveIframe()
      } else {
        this.logMessages(['Printing failed because the `contentWindow` of the print iframe did not load. This is possibly an error with `react-to-print&
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值