vue基于html2canvas和jspdf?生成pdf?、解決jspdf中文亂碼問題的方法詳解
html2canvas
備注:以下內(nèi)容都是基于官方文檔
簡介
該腳本允許您直接在用戶瀏覽器上對網(wǎng)頁或其部分進行“截圖”。屏幕截圖基于 DOM,因此可能不是 100% 準確到真實表示,因為它不會制作實際的屏幕截圖,而是根據(jù)頁面上可用的信息構(gòu)建屏幕截圖。
安裝及使用
npm install html2canvas
import html2canvas from 'html2canvas';
html2canvas(el,options).then(function(canvas) {
document.body.appendChild(canvas);
});el 是要生成canvas的dom對象, options是配置項,常用的配置項有:backgroundColor(背景色,默認白色)、 width(寬度)、height(高度)、useCORS(是否嘗試使用 CORS 從服務(wù)器加載圖像)、 canvas(canvas對象用作繪圖基礎(chǔ)的現(xiàn)有元素)
參考文章:html2canvas使用筆記
示例
下面的例子基于Vue
<template>
<div>
<div ref="a">
<img src="./image/aa.webp" />
<p>一張?zhí)评哮唸D片</p>
</div>
<el-button type="primary" @click="createCanvas">html轉(zhuǎn)canvas</el-button>
<div ref="b">
<p>這里放生成的canvas</p>
</div>
</div>
</template>
<script>
import html2canvas from 'html2canvas';
export default {
methods: {
createCanvas() {
// dom元素
let dom = this.$refs.a;
// 寬高
let width = dom.offsetWidth;
let height = dom.offsetHeight;
// 生成一個canvas對象
let canvas = document.createElement('canvas');
// 設(shè)置canvas的寬高
canvas.width = width * 2;
canvas.height = height * 2;
// 生成頁面模糊時,可以放大一定倍數(shù),通過調(diào)整canvas的寬高使其清晰度增加
const context = canvas.getContext('2d');
context.scale(1.5, 1.5);
// 配置項
let options = {
backgroundColor: '#ffffff',
canvas: canvas,
useCORS: true
};
// 容器
let container = this.$refs.b;
// 生成canvas并插入到容器里
html2canvas(dom,options).then(canvas => {
container.appendChild(canvas);
});
}
}
};
</script>
<style scoped lang="scss">
</style>
效果圖
jspdf
基礎(chǔ)語法
new jsPDF(orientation, unit, format) → {jsPDF}
orientation:方向,默認p(豎直方向),可選l(橫向)
unit:單位,指定坐標時要使用的測量單位。“pt”(點)、“mm”(默認)、“cm”、“in”之一
format :應(yīng)該是紙張格式,默認a4
常用方法
文本
text(text, x, y, flags) → {jsPDF}
text:文本內(nèi)容
x,y 文本相對于頁面左上角的偏移量
flags:具體是干啥的不填清楚,可以不用填
// 生成文本
createText() {
let doc = new JsPdf();
doc.text('aaa',100,100);
// 保存成pdf文件
doc.save('a.pdf');
}
下面是生成的pdf文件
關(guān)于中文亂碼問題
查了一下官網(wǎng),官網(wǎng)內(nèi)容如下(官網(wǎng)是英文的,用谷歌翻譯,翻譯了一下)
jspdf git地址
官網(wǎng)里提供的那個地址不太好使,我這里把代碼下到了本地。找到下面的這個html文件
點擊打開,可以看到下面的網(wǎng)站
上傳一個ttf的字體文件,會生成一個js文件。然后將文件引入到頁面里
import './hyjt-normal';
// 添加并設(shè)置字體
console.log(doc.getFontList());
doc.setFont('hyjt');
doc.getFontList() 用于獲取當(dāng)前支持那些字體,doc.setFont(); 設(shè)置字體,值一定要與你上面填的名一致,打開這個js文件可以看到:
示例:
// 生成文本
createText() {
let doc = new JsPdf();
// 添加并設(shè)置字體
console.log(doc.getFontList());
doc.setFont('hyjt');
doc.text('哈哈哈',100,100);
// 保存成pdf文件
doc.save('a.pdf');
}

文本顏色
setTextColor(r, g, b) // rgb 顏色值(0~255)
添加新頁面和設(shè)置指針所在頁數(shù)
//添加新頁面 addPage() //設(shè)置當(dāng)前焦點在第幾頁 setPage(number)
示例:
// 生成文本
createText() {
let doc = new JsPdf();
doc.setFont('hyjt');
doc.addPage();
doc.text('I am on page 2', 10, 10);
doc.setPage(1);
doc.text('I am on page 1', 10, 10);
// 保存成pdf文件
doc.save('a.pdf');
}
說一下流程:默認內(nèi)容會在第一頁,執(zhí)行addPage()后會生成新的一頁,焦點跳到第二頁,I am on page 2 會在第二頁里生成;執(zhí)行setPage(1) ,焦點跳轉(zhuǎn)到第一頁,I am on page 1會在第一頁里生成
設(shè)置字體大小
setFontSize(number)
保存pdf
save(filename)
獲取可用字體列表
getFontList ()
生成圖片
addImage(imageData,format,x,y,width,height) imageData:圖片的data URL format:圖片的格式,比如png x,y:距離頁面左上角的偏移 width,height:圖片的寬高
示例:
// 生成圖片
createImage() {
// 生成pdf
let doc = new JsPdf();
doc.addImage(this.canvasImage,'jpeg',100,20,200,200);
doc.save('b.pdf');
}
主要是要獲取圖片的data URL,可以借助canvas的toDataURL方法,可以借助一下上面的html2canvas
// 生成canvas并插入到容器里
html2canvas(dom,options).then(canvas => {
container.appendChild(canvas);
this.canvasImage = canvas.toDataURL('image/jpeg');
});

相關(guān)文章
Vue 3.0 前瞻Vue Function API新特性體驗
這篇文章主要介紹了Vue 3.0 前瞻Vue Function API新特性體驗,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
elementplus實現(xiàn)多級表格(最后一級展示圖片)
本文主要介紹了elementplus實現(xiàn)多級表格(最后一級展示圖片),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
vscode 使用Prettier插件格式化配置使用代碼詳解
這篇文章主要介紹了vscode 使用Prettier插件格式化配置使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Vue中el-tree樹全部展開或收起的實現(xiàn)示例
本文主要介紹了Vue中el-tree樹全部展開或收起的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07

