日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

vue 生成PDF(A4标准PDF分页)

發布時間:2023/12/20 vue 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vue 生成PDF(A4标准PDF分页) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.先安裝兩個插件

//頁面轉圖片

npm install --save html2canvas

//圖片轉PDF

npm install jspdf --save?

2. 在需要導出的dom節點增加ref='pdf' 例如

<div ref="pdf"> 這是待轉換的頁面,點擊 <button @click="handleExport">導出</button> 點擊導出PDF</div>

3.定義導出方法

handleExport() {

? ? ? downloadPDF(this.$refs.pdf);

? ?},

4.在頁面導入?import { downloadPDF } from "@/utils/pdf.js";

pdf.js 內容如下

import html2canvas from "html2canvas";

import jsPDF from "jspdf";

export const downloadPDF = page => {

? ? html2canvas(page).then(function (canvas) {

? ? ? ? canvas2PDF(canvas);

? ? });

};

const canvas2PDF = canvas => {

? ? let contentWidth = canvas.width;

? ? let contentHeight = canvas.height;

? ? //a4紙的尺寸[595.28,841.89],html頁面生成的canvas在pdf中圖片的寬高

? ? let imgWidth = 595.28;

? ? let imgHeight = 592.28 / contentWidth * contentHeight;

? ? //let imgHeight = 700/contentWidth * contentHeight;

? ? //一頁pdf顯示html頁面生成的canvas高度;

? ? var pageHeight = contentWidth / 592.28 * 841.89;

? ? let totalHeight = contentHeight;

? ? // 第一個參數: l:橫向 ?p:縱向

? ? // 第二個參數:測量單位("pt","mm", "cm", "m", "in" or "px")

? ? let pdf = new jsPDF("p", "pt");

? ? let position = 0;

? ? // ? pdf.addImage(

? ? // ? ? canvas.toDataURL("image/jpeg", 1.0),

? ? // ? ? "JPEG",

? ? // ? ? 0,

? ? // ? ? 0,

? ? // ? ? imgWidth,

? ? // ? ? imgHeight

? ? // ? );

? ? if (totalHeight < pageHeight) {

? ? ? ? pdf.addImage( canvas.toDataURL("image/jpeg", 1.0), 'JPEG', 0, 0, imgWidth, imgHeight);

? ? } else { ? ?

? ? ? ? while (totalHeight > 0) {

? ? ? ? ? ? pdf.addImage( canvas.toDataURL("image/jpeg", 1.0), 'JPEG', 0, position, imgWidth, imgHeight)

? ? ? ? ? ? totalHeight -= pageHeight;

? ? ? ? ? ? position -= 841.89;

? ? ? ? ? ? //避免添加空白頁

? ? ? ? ? ? if (totalHeight > 0) {

? ? ? ? ? ? ? ? pdf.addPage();

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? // pdf.addImage(

? ? // ? ? canvas.toDataURL("image/jpeg", 1.0),

? ? // ? ? "JPEG",

? ? // ? ? 0,

? ? // ? ? position,

? ? // ? ? imgWidth,

? ? // ? ? imgHeight

? ? // );


?

? ? pdf.save("導出.pdf");

};

5.點擊導出即可成功

總結

以上是生活随笔為你收集整理的vue 生成PDF(A4标准PDF分页)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。