vue使用html2PDF實現(xiàn)將內(nèi)容導(dǎo)出為PDF
將 HTML 轉(zhuǎn)換為 PDF 進行下載是一個比較常見的功能。過去要實現(xiàn)這個功能通常是放在服務(wù)端來實現(xiàn),將文件生成好把鏈接發(fā)送給前端,讓前端跳轉(zhuǎn)進行下載?,F(xiàn)在對于前端來說,使用庫并寫幾行代碼就可以簡單的實現(xiàn)了。
前端 PDF 生成全部與瀏覽器的 API 調(diào)用有關(guān),JavaScript 及其相關(guān)庫使用這些 API 來完成任務(wù)?,F(xiàn)在有很多 JavaScript 庫使用,這里有個清單供參考:
- jsPDF
- html2pdf
- pdfmake
- PDFsKit
- ReLaXed
- nodeice
- Electron
- PDFObject
- pdf2json
本文跟大家分享其中一個庫 html2pdf.js。在這里將結(jié)合使用它和 Vue3 從 HTML 生成 PDF 進行下載。
Html2PDF 是創(chuàng)建報告并將其導(dǎo)出為 PDF 文件的最古老的組件,它基于使用外部 toolkit 包將任何屏幕直接打印到 PDF 文件。
所有項目的開始都是從安裝依賴庫開始:
npm install --save html2pdf.js
VUE 項目通常是從一個 App.vue 文件開始:
<template>
<div id="app" ref="document">
<div id="element-to-convert">
<center>
<img width="400" src="./assets/constellation.png" />
</center>
</div>
</div>
<button @click="downloadToPDF">Download to PDF</button>
</template>
<style>
#app {
margin-top: 60px;
}
</style>
實例效果如下:

要生成 PDF,只需在項目中添加幾行代碼:
const downloadToPDF = () => {
const element = document.getElementById("element-order-detail");
const opt = {
margin: 1,
filename: "myfile.pdf",
image: { type: "jpeg", quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: {
unit: "in",
format: "letter",
orientation: "portrait",
},
};
html2pdf().set(opt).from(element).save();
};
downloadToPDF 函數(shù)中定義了一個配置項對象。
| Name | Type | Default | Description |
|---|---|---|---|
| margin | number or array | 0 | PDF 邊距(以 jsPDF 為單位)??梢允菃蝹€數(shù)字,像CSS規(guī)則類似 [vMargin, hMargin], or [top, left, bottom, right]. |
| filename | string | 'file.pdf' | 導(dǎo)出的 PDF 的默認文件名 |
| pagebreak | object | {mode: ['css', 'legacy']} | 控制頁面上的分頁行為 |
| image | object | {type: 'jpeg', quality: 0.95} | 用于生成 PDF 的圖像類型和質(zhì)量 |
| enableLinks | boolean | true | 如果啟用,PDF 超鏈接會自動添加到所有錨標記之上 |
| html2canvas | object | { } | html2canvas 配置選項 |
| jsPDF | object | { } | jsPDF 配置選項 |
以下是一個簡單的示例 App.vue 文件完整代碼:
<script>
import html2pdf from "html2pdf.js";
import { defineComponent } from "vue";
const AppHome = defineComponent({
setup() {
const downloadToPDF = () => {
const element = document.getElementById("element-order-detail");
const opt = {
margin: 1,
filename: "myfile.pdf",
image: { type: "jpeg", quality: 0.98 },
html2canvas: { scale: 2 },
jsPDF: {
unit: "in",
format: "letter",
orientation: "portrait",
},
};
html2pdf().set(opt).from(element).save();
};
return {
downloadToPDF,
};
},
});
export default AppHome;
</script>
<template>
<div id="app" ref="document">
<div id="element-to-convert">
<center>
<img width="400" src="./assets/constellation.png" />
</center>
</div>
<button @click="downloadToPDF">Download to PDF</button>
</div>
</template>
<style>
#app {
margin-top: 60px;
text-align: center;
}
</style>
總結(jié)
html2pdf.js 可以快速、無縫地從 HTML 生成 PDF。它是構(gòu)建生成 PDF 文檔的簡單應(yīng)用程序的絕佳選擇。
到此這篇關(guān)于vue使用html2PDF實現(xiàn)將內(nèi)容導(dǎo)出為PDF的文章就介紹到這了,更多相關(guān)vue html2PDF導(dǎo)出為PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 實現(xiàn)v-for循環(huán)的時候更改 class的樣式名稱
這篇文章主要介紹了Vue 實現(xiàn)v-for循環(huán)的時候更改 class的樣式名稱,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果
這篇文章主要介紹了vue3+vite3+typescript實現(xiàn)驗證碼功能及表單驗證效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
vue語法自動轉(zhuǎn)typescript(解放雙手)
這篇文章主要介紹了vue語法自動轉(zhuǎn)typescript,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
JavaScript的MVVM庫Vue.js入門學(xué)習(xí)筆記
這篇文章主要介紹了JavaScript的MVVM庫Vue.js入門學(xué)習(xí)筆記,Vue.js是一個新興的js庫,主要用于實現(xiàn)響應(yīng)的數(shù)據(jù)綁定和組合的視圖組件,需要的朋友可以參考下2016-05-05

