Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<template>
  <PageWrapper title="截图示例">
    <CollapseContainer title="截图操作">
      <a-button type="primary" class="mr-2" @click="handleScreenshot">截取当前body</a-button>
      <a-button type="primary" class="mr-2" :disabled="!showPicture" @click="handleDelScreenshot"
        >删除截图</a-button
      >
      <a-button type="primary" class="mr-2" :disabled="!showPicture" @click="handlePrintScreenshot"
        >打印截图</a-button
      >
      <a-button
        type="primary"
        class="mr-2"
        :disabled="!showPicture"
        @click="handleDownloadScreenshot"
        >下载截图</a-button
      >
    </CollapseContainer>
 
    <Card title="截图内容" class="mt-4">
      <div ref="pictureRef" v-show="showPicture"></div>
    </Card>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { PageWrapper } from '@/components/Page';
  import html2canvas from 'html2canvas';
  import { ref } from 'vue';
  import { Card } from 'ant-design-vue';
  import { CollapseContainer } from '@/components/Container';
  import printJS from 'print-js';
  import { downloadByBase64 } from '@/utils/file/download';
 
  const pictureRef = ref();
  const showPicture = ref<boolean>(false);
  const canvasUrl = ref<string>('');
 
  function handleScreenshot() {
    if (showPicture.value) return;
    html2canvas(document.body, {
      backgroundColor: '#ffffff',
      allowTaint: true, //开启跨域
      useCORS: true,
      scrollY: 0,
      scrollX: 0,
    })
      .then(function (canvas) {
        canvas.style.width = '100%';
        canvas.style.height = '100%';
        pictureRef.value.appendChild(canvas);
        showPicture.value = true;
        canvasUrl.value = canvas.toDataURL();
      })
      .catch((err) => {
        console.log('绘制失败', err);
      });
  }
 
  function handleDelScreenshot() {
    pictureRef.value.innerHTML = '';
    canvasUrl.value = '';
    showPicture.value = false;
  }
 
  function handlePrintScreenshot() {
    if (!canvasUrl.value) return;
    printJS({
      printable: canvasUrl.value,
      type: 'image',
      base64: true,
    });
  }
 
  function handleDownloadScreenshot() {
    downloadByBase64(canvasUrl.value, 'screen_shot.png');
  }
</script>