问题:处理后得到的是images的数组,有几张就有几个数组 当时想拿images[0]作为封面,结果 base64地址用到upload的img中会被其中base64,的逗号分隔取前面的,例如到img就只有前半截
解决:要用数组[images[0]]
tip将pdf转图片
// npm i pdfjs-dist --save
//引入pdfjs库
// {/* import pdfjsLib from 'pdfjs-dist' */}
// < script src = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.6.172/pdf.min.js"
// integrity = "sha512-KEoL9wKXt/fhlAfN+ZNXf3pI48aaQE9Qd5fihHY+5n5XLTSnyGJ0uKgUj//V6KAcjFwzAbCYYPKeGlFES/H5Ng=="
// crossorigin = "anonymous"
// referrerpolicy = "no-referrer" > < /script>
//加载pdf文件
const loadPdf = async (url) => {
const loadingTask = pdfjsLib.getDocument(url)
const pdf = await loadingTask.promise
return pdf
}
//将pdf页面转换为canvas
const pdfToCanvas = async (pdf, pageIndex) => {
const page = await pdf.getPage(pageIndex)
const canvas = document.createElement('canvas')
const viewport = page.getViewport({
scale: 1
})
canvas.width = viewport.width
canvas.height = viewport.height
const context = canvas.getContext('2d')
const renderContext = {
canvasContext: context,
viewport,
}
await page.render(renderContext).promise
return canvas
}
//将canvas转换为图像
const canvasToImage = async (canvas) => {
const dataUrl = canvas.toDataURL('image/jpeg')
return dataUrl
}
//将pdf文件转换为多页图像
const pdfToImages = async (url) => {
const pdf = await loadPdf(url)
const images = []
for (let i = 1; i <= pdf.numPages; i++) {
const canvas = await pdfToCanvas(pdf, i)
const image = await canvasToImage(canvas)
images.push(image)
}
return images
}
//下载图像
const downloadImage = (url, name) => {
const link = document.createElement('a')
link.download = name
link.href = url
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
// dataurl 为传进来的base64格式的图片地址, return 返回的为file格式
const base64ImgtoFile=(dataurl, filename = 'file')=>{
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
//使用示例
// const url = 'https://example.com/example.pdf'
// const images = await pdfToImages(url)
// for (let i = 0; i < images.length; i++) {
// downloadImage(images[i], `page${i+1}.jpg`)
// }
export {
pdfToImages,
downloadImage,
base64ImgtoFile
}
二、将生成的base64转file上传取地址在显示保存
将base64转file
// dataurl 为传进来的base64格式的图片地址, return 返回的为file格式
const base64ImgtoFile=(dataurl, filename = 'file')=>{
let arr = dataurl.split(',')
let mime = arr[0].match(/:(.*?);/)[1]
let suffix = mime.split('/')[1]
let bstr = atob(arr[1])
let n = bstr.length
let u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${filename}.${suffix}`, {
type: mime
})
}
将file上传
export const uploadFile = (file) => {
const formData = new FormData();
formData.append('file', file);
return request({
headers: {
"Content-Type": "multipart/form-data"
},
url: '/fileUpload/blade-resource/oss/endpoint/put-file-attach',
method: 'post',
data: formData
})
}
在显示
async uploadAfter(res, done) {
try{
const images = await pdfToImages(res.link)
var file = base64ImgtoFile(images[0])
uploadFile(file).then(res=>{
this.form.coverImg = res.data.data.name
})
done()
}catch (e) {
console.error(e)
done()
}
// this.$message.success('上传后的方法')
},