vue3 前端实现pdf打印预览 printjs

vue3 前端实现pdf打印预览 printjs_第1张图片

  1. 在utils建print.ts文件

    interface PrintFunction {
    extendOptions: Function;
    getStyle: Function;
    setDomHeight: Function;
    toPrint: Function;
    }

    const Print = function (dom, options?: object): PrintFunction {
    options = options || {};
    // @ts-expect-error
    if (!(this instanceof Print)) return new Print(dom, options);
    this.conf = {
    styleStr: “”,
    // Elements that need to dynamically get and set the height
    setDomHeightArr: [],
    // Callback before printing
    printBeforeFn: null,
    // Callback after printing
    printDoneCallBack: null
    };
    for (const key in this.conf) {
    // eslint-disable-next-line no-prototype-builtins
    if (key && options.hasOwnProperty(key)) {
    this.conf[key] = options[key];
    }
    }
    if (typeof dom === “string”) {
    this.dom = document.querySelector(dom);
    } else {
    this.dom = this.isDOM(dom) ? dom : dom.$el;
    }
    if (this.conf.setDomHeightArr && this.conf.setDomHeightArr.length) {
    this.setDomHeight(this.conf.setDomHeightArr);
    }
    this.init();
    };

    Print.prototype = {
    /**

    • init
      /
      init: function (): void {
      const content = this.getStyle() + this.getHtml();
      this.writeIframe(content);
      },
      /
      *
    • Configuration property extension
    • @param {Object} obj
    • @param {Object} obj2
      /
      extendOptions: function (obj, obj2: T): T {
      for (const k in obj2) {
      obj[k] = obj2[k];
      }
      return obj;
      },
      /
      *
      Copy all styles of the original page
      */
      getStyle: function (): string {
      let str = “”;
      const styles: NodeListOf = document.querySelectorAll(“style,link”);
      for (let i = 0; i < styles.length; i++) {
      str += styles[i].outerHTML;
      }
      str += ;
      return str;
      },
      // form assignment
      getHtml: function (): Element {
      const inputs = document.querySelectorAll(“input”);
      const selects = document.querySelectorAll(“select”);
      const textareas = document.querySelectorAll(“textarea”);
      const ca

你可能感兴趣的:(前端,pdf,状态模式)