vue實現(xiàn)圖片轉pdf的示例代碼
1.前言
嘗試了集中圖片轉pdf的方式,
(1)最終較為優(yōu)秀的一種是使用jspdf將圖片轉為pdf,支持JPG/JPEG/PNG/BMP/TIF/TIFF圖片格式轉換,詳見我的另一篇文章:利用jsPDF實現(xiàn)將圖片轉為pdf
(2)使用print-js插件,去看看
(3)pdfMake圖片轉pdf,支持JPG/JPEG/PNG圖片格式轉換,去看看
(4)html2canvas,轉出來的圖片模糊,需要處理啊,我沒處理,去看看
2.print-js圖片轉pdf
npm安裝print-js依賴
main.js:
import print from 'print-js'
使用:
printJS({ // blob鏈接 數(shù)組 printable: ['blob:http//.....'], // 打印類型 目前為圖片樣式 可以根據(jù)上面的網(wǎng)址進行修改 type: 'pdf', // 二維碼樣式 可以自己進行修改 imageStyle: 'margin:0px; padding:0px; width:40%; height:40%; display: block; margin: 0 auto; padding-top:12%' // 也可以設置以下參數(shù) 繼承所有css樣式 沒試過image的 html的效果不錯 // targetStyles:['*'] })
3.pdfMake圖片轉pdf
安裝pdfMake依賴
async convertToPDF(blob, id) { let this_ = this let base64Data = await this.readFile(blob); const docDefinition = { content: [ { image: base64Data, fit: [190, 277], alignment: 'center' } //width: 400, ] } const pdfDocGenerator = pdfMake.createPdf(docDefinition) pdfDocGenerator.getBlob(pdfBlob => { console.log("這是pdf的blob格式-----"pdfBlob); // 可以在這里使用blob,比如將其轉換為Blob URL let url = window.URL.createObjectURL(new Blob([pdfBlob], { type: 'application/pdf' })) }); }, //blob轉base64 readFile(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = function () { const contents = reader.result; resolve(contents); }; reader.onerror = function (event) { reject(event.target.error); }; reader.readAsDataURL(file); }); },
其他一些轉化方法
//ArrayBuffer轉換為Base64 arrayBufferToBase64(arrayBuffer) { const uint8Array = new Uint8Array(arrayBuffer); let binaryString = ''; for (let i = 0; i < uint8Array.length; i++) { binaryString += String.fromCharCode(uint8Array[i]); } return btoa(binaryString); },
4.html2canvas圖片轉pdf
安裝依賴
<div v-for="(item, index) in list" :key="index"> <img :id="'imageContainer'+item.id" :src="item.imgurl" alt="" /> </div>
async imgToPdf(imgUrl, id) { // 將圖片渲染為Canvas //因為img標簽是循環(huán)展示圖片的,通過id判斷是哪個img標簽 const canvas = await html2canvas(window.document.getElementById('imageContainer'+id)) // 獲取Canvas的DataURL const imageURL = canvas.toDataURL('image/png') //const imageURL = canvas.toDataURL(imgUrl) // 創(chuàng)建PDF實例并設置紙張大小 const pdf = new jsPDF('p', 'px', 'a4') // 計算圖片在PDF中的寬度和高度 const pdfWidth = pdf.internal.pageSize.getWidth() const pdfHeight = (canvas.height * pdfWidth) / canvas.width // 將圖片添加到PDF中 pdf.addImage(imageURL, 'JPEG', 0, 0, pdfWidth, pdfHeight) pdf.save() const blob = new Blob([pdf], { type: 'application/PDF' }) console.log("生成的pdf的blob文件---",blob) },
到此這篇關于vue實現(xiàn)圖片轉pdf的示例代碼的文章就介紹到這了,更多相關vue圖片轉pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue中實現(xiàn)動態(tài)更新JSON數(shù)據(jù)的三種方式
在 Vue 項目中動態(tài)更新 JSON 數(shù)據(jù),可以通過以下幾種方式實現(xiàn),具體方法取決于你的需求,比如數(shù)據(jù)是存儲在前端還是后端、是否需要持久化等,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下2025-04-04使用vue初用antd 用v-model來雙向綁定Form表單問題
這篇文章主要介紹了使用vue初用antd 用v-model來雙向綁定Form表單問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vue element-ui el-tooltip組件失效問題及解決
這篇文章主要介紹了vue element-ui el-tooltip組件失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條功能
這篇文章主要介紹了uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條,需要的朋友可以參考下2019-11-11如何在vue3中使用滑塊檢驗vue-puzzle-verification
這篇文章主要介紹了在vue3中使用滑塊檢驗vue-puzzle-verification的相關資料,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11