小工具知识网

当前位置: 首页 > 行业知识 > vue项目导出pdf(前端将html转为pdf并导出)

行业知识

vue项目导出pdf(前端将html转为pdf并导出)

2026-11-02 17:13:48 来源:互联网转载

工具包

html2canvas:将页面html转canvasjspdf:根据canvas生成的图片导出pdf

官网

index.php/tags-41973.html" >GitHub - parallax/jsPDF: Client-side JavaScript PDF generation for everyone.Client-side JavaScript PDF generation for everyone. - parallax/jsPDFhttps://github.com/parallax/jsPDF

示例代码

import html2canvas from "html2canvas";import jsPDF from "jspdf";export const downloadPDF = (page, name) => {    html2canvas(page).then(function (canvas) {        canvas2PDF(canvas, name);    });};const canvas2PDF = (canvas, name) => {    let contentWidth = canvas.width;    let contentHeight = canvas.height;    //a4纸的尺寸[595.28,841.89],html页面生成的canvas在pdf中图片的宽高    let imgWidth = 455.28;    let imgHeight = 455.28 / contentWidth * contentHeight;    // 第一个参数: l:横向  p:纵向    // 第二个参数:测量单位("pt","mm", "cm", "m", "in" or "px")    let pdf = new jsPDF("p", "pt");    pdf.addImage(        canvas.toDataURL("image/jpeg", 1.0),        "JPEG",        70,        30,        imgWidth,        imgHeight    );    if (!name) name = '导出'    pdf.save(name + ".pdf");};

上一篇:遇到服务器错误怎么办?

下一篇:服务器带宽费用怎么算?