Convert document DOM nodes to images based on html2canvas and jspdf to generate PDF files and download them locally


Here we need to use html2canvas to convert the document DOM node into a picture, and download it to the local_Your Beloved Qiangge’s Blog-CSDN Blog The front end uses native js to edit the file content→create the generated file (the format can be customized)→download the file_Your Beloved Qiangge’s Blog-CSDN Blog. An html file will be created automatically. https://blog.csdn.net/qq_37860634/article/details/131752136

Learn about the official website and GitHub of the jspdf plugin 

jsPDFGenerate professional PDFs easily with jsPDF, the open-source solution for PDF generation. Create event tickets, reports, certificates, and more in minutes with our intuitive interface.https://parall.ax/products/jspdfGitHub - parallax/jsPDF: Client-side JavaScript PDF generation for everyone.Client-side JavaScript PDF generation for everyone. - GitHub - parallax/jsPDF: Client-side JavaScript PDF generation for everyone.https://github.com/parallax/jsPDF安装先

npm install jspdf --save

quote 

import { jsPDF } from "jspdf";

 use case code

<template>
    <div>
        <h1 style="font-size: 100px;">自定义下载PDF</h1>
        <el-button type="primary" ref="btn" @click="downloadImg({ dom: $el, fileName: '自定义.pdf' })">下载当前网页为pdf</el-button>
    </div>
</template>
<script>
import html2canvas from 'html2canvas'; // npm install --save html2canvas (官网:https://html2canvas.hertzen.com)
import { jsPDF } from "jspdf"; // npm install jspdf --save (官网:https://parall.ax/products/jspdf https://github.com/parallax/jsPDF)
export default {
    methods: {
        downloadImg({ dom, fileName = '', pdfWidth, pdfHeigth, } = {}) {
            html2canvas(dom).then(canvas => new jsPDF('l', 'pt', [pdfWidth || innerWidth, pdfHeigth || innerHeight]).addImage(canvas, 'PNG', 0, 0, canvas.width, canvas.height).save(fileName));
        },
    }
};
</script>

Guess you like

Origin blog.csdn.net/qq_37860634/article/details/131753200