将本地的pdf文件添加到canvas中-fabric.js的使用-参考官网

导入文件

  <div>
        <input type="file" accept="application/pdf">
    </div>

input type=“file” 属性

multiple
multiple="multiple" 

可以接受多个值的文件上传

accept
accept="application/pdf"

限定文件上传的类型
也可以定义多个文件类型,用逗号进行隔开
但注意不要将这个属性作为唯一的验证工具,应该在服务器上对文件进行验证
在这里插入图片描述

<canvas id="canvas" width="300px" height="200px"></canvas>
import {
    fabric } from 'fabric'
    mounted() {
   
        const canvas = this._canvas = new fabric.Canvas('canvas', {
   
            width:600,height:600 //发现在这里面设置canvas的高度和宽度才有效
        })

添加元素

        const text = new fabric.Text('Upload PDF')
        canvas.add(text)

在这里插入图片描述

监听input事件

只要输入的值变化了就会触发input事件,当文件上传完成后就会触发

        <input type="file" accept="application/pdf" @input="input">
            methods: {
   
        input() {
   
        console.log('2');
    },

因为是在methods里面定理的事件,需要处理canvas和text,因此需要对数据进行重新修改

data() {
   
    return {
   
        text: {
   },
        canvas: {
   }
    }
    },
    mounted() {
   
        this.canvas = this._canvas = new fabric.Canvas('canvas', {
   
            width:600,height:600
        })
        this.text = this._text= new fabric.Text('Upload PDF')
        this.canvas.add(this.text)
    },
  
    methods: {
   
        input(e) {
   
            console.log(this.text);
            this.text.set('text','loading...')
        console.log(e.target);
    },

设备像素比

const scale = 1 / window.devicePixelRatio

设备像素比返回了设备上的物理像素和当前设备CSS像素的一个壁纸,也就是说明浏览器多少屏幕实际的像素点被用来画了一个CSS像素点

window.devicePixelRatio = 物理像素 / css像素
常用来修正canvas的像素比

安装pdfjs-dist

npm install --save pdfjs-dist@2.0.943

Blob对象表示一个不可变、原始数据的类文件对象

自己理不清楚了,先全部拷贝试一下

 <div>
        <input type="file" accept="application/pdf">
    </div>
    <canvas id="c" width="500" height="620">
    </canvas>
    <script src="//mozilla.github.io/pdf.js/build/pdf.js"></script>
    <script src="https://unpkg.com/fabric@4.6.0/dist/fabric.min.js"></script>
    <script>
        const Base64Prefix = "data:application/pdf;base64,";

        function getPdfHandler() {
   
            console.log(window['pdfjs-dist/build/pdf']);
            return window['pdfjs-dist/build/pdf'];
        }

        function readBlob(blob) {
   
            return new Promise((resolve, reject) => {
   
                const reader = new FileReader();
                reader.addEventListener('load', () => resolve(reader.result));
                reader.addEventListener('error', reject)
                reader.readAsDataURL(blob);
            })
        }

        async function p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一夕ξ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值