index.vue 3.75 KB
<template>
	<div class="eContainer">
    <div style="float:right;position:relative;z-index:99;display:inline-block;">
      <el-button @click="print" style="margin-right:1em;" type="primary">打印</el-button>
    </div>
    <el-tabs type="card" v-model="tabName">
			<el-tab-pane :label="`格式化文本`" :name="`format`" style="overflow:auto">
				<div :ref="`formatDiv`" class="printEm">
					<table class="printTable">
						<el-row :key="index" v-for="(tagDesp,index) in formatData">
              <div v-if="tagDesp.tag==='__TITLE'" class="title">
                <el-col :span="24">
                  <b v-html="tagDesp.value"></b>
                </el-col>
              </div>
              <div v-else>
                <el-col :span="8">
                  <span style="word-break: break-word;" class="mtFontSize" v-html="tagDesp.desp"></span>
                </el-col>
                <el-col :span="4" class="mtFontSize0">
                  <span v-if="tagDesp.tag && tagDesp.tag!==''">:{{tagDesp.tag}}:</span>
                  <p v-else v-html="formatContent(' ')"></p>
                </el-col>
                <el-col :span="12" class="mtFontSize0">
                  <!-- 用空格占一下高度-->
                  <div>
                    <span v-html="formatContent(tagDesp.value || ' ')"></span>
                  </div>
                </el-col>
              </div>
						</el-row>
					</table>
				</div>
			</el-tab-pane>
		</el-tabs>
	</div>
</template>
<script>
import moment from "moment";
import { setTimeout } from "timers";
import Api from "~/service/Api";

export default {
  props: ["formatData"],
  data() {
    return {
      tabName: "format",
      printTime: ""
    };
  },
  methods: {
    formatContent(content) {
      return (
        "<span>" +
        (content || " ").replace(/\r?\n/g, "</span><span>") +
        "</span>"
      );
    },
    getNow() {
      return moment()
        .format("DD.MM.YYYY @ HH:mm:ss")
        .replace("@", "at");
    },
    loading(text) {
      const loading = this.$loading({
        lock: true,
        text,
        spinner: "el-icon-loading",
        background: "rgba(200, 200, 200, 0.3)"
      });
      return loading;
    },
    base64ToBlob(code) {
      code = code.replace(/[\n\r]/g, "");
      const raw = window.atob(code);
      const rawLength = raw.length;
      const uInt8Array = new Uint8Array(rawLength);
      for (let i = 0; i < rawLength; ++i) {
        uInt8Array[i] = raw.charCodeAt(i);
      }
      return new Blob([uInt8Array], { type: "application/pdf" });
    },
    async print() {
      //JS PDF下载
      let res = await Api.post(`/public/quesel/getPdf`, {
        format: "jsn",
        formatData: this.formatData
      });
      if (res.respCode === SUCCESS) {
        let blob = this.base64ToBlob(res.data.data);
        const fileURL = URL.createObjectURL(blob);
        let iframe = document.createElement("iframe");
        iframe.id = "printframe";
        iframe.src = fileURL;
        iframe.type = "application/pdf";
        iframe.style.display = "inline-block";
        iframe.style.width = "1px";
        iframe.style.height = "1px";
        iframe.style.position = "absolute";
        iframe.style.top = "-100px";
        iframe.onload = function() {
          iframe.contentWindow.print();
        };
        document.body.appendChild(iframe);
      }
    }
  }
};
</script>
<style lang="less" scoped>
body {
  font-family: SimSun;
  font-size: 14px;
}
.printEm {
  width: 80%;
  height: auto;
  margin: 0 auto;
  padding: 30px;
}
.printTable{
  width: 100%;
}
.mtFontSize {
  font-size: 20px;
}
.mtFontSize0 {
  font-size: 20px;
}
.title {
  display: flex;
  align-items: center;
  text-align: center;
  font-size: 20px;
  height: 40px !important;
}
table td {
  font-size: 20px;
  color: black;
}
</style>