Vue exports the page as PDF

tags: vue

Download dependence

First.Convert page HTML into pictures
npm install --save html2canvas 
 the second.Generate PDF
npm install jspdf --save

2. Find the DOM you need to download

Multi-block elements can customize a DIV custom one ID facilitating GET to this DOM
willprintOutThis method is putmethods Call

printOut(name) {
        let shareContent = document.getElementById('id9527'),/ / Requires screenshots (native) DOM objects
          width = shareContent.clientWidth, / / Get the DOM width
          height = shareContent.clientHeight , // Get the DOM height
          canvas = document.createElement("canvas"), // Create a Canvas node
          scale = 1; // Define any magnification support decimal
        canvas.width = width * scale; / / Define Canvas Width * Zoom
        canvas.height = height * scale; / / Define Canvas Height * Zoom
        canvas.style.width = shareContent.clientWidth * scale + "px";
        canvas.style.height = shareContent.clientHeight * scale + "px";
        canvas.getContext("2d").scale(scale, scale); / / Get context, set Scale
        let opts = {
          scale: scale, // Added Scale parameters
          canvas: canvas, / / Custom CANVAS
          logging: false, // Log Switch, easy to see the internal execution process of HTML2CANVAS
          width: width, // DOM original width
          height: height,
          useCORS: true, // [Important] Turn on cross-domain configuration
        };

        html2Canvas(shareContent, opts).then(() => {
          var contentWidth = canvas.width;
          var contentHeight = canvas.height;
          // One page PDF shows the CANVAS height generated by the HTML page;
          var pageHeight = (contentWidth / 592.28) * 841.89;
          // Do not generate the HTML page height of the PDF
          var leftHeight = contentHeight;
          // page offset
          var position = 0;
          // A4 paper size [595.28, 841.89], the HTML page generated by Canvas in PDF is high in PDF
          var imgWidth = 595.28;
          var imgHeight = (592.28 / contentWidth) * contentHeight;
          var pageData = canvas.toDataURL("image/jpeg", 1.0);
          var PDF = new JsPDF("", "pt", "a4");
          if (leftHeight < pageHeight) {
            PDF.addImage(pageData, "JPEG", 15, 50, imgWidth * 0.95, imgHeight);
          } else {
            while (leftHeight > 0) {
              PDF.addImage(pageData, "JPEG", 0, position, imgWidth, imgHeight);
              leftHeight -= pageHeight;
              position -= 841.89;
              if (leftHeight > 0) {
                PDF.addPage();
              }
            }
          }
          PDF.save(name + ".pdf"); / / Here is the exported file name
        });
      },

If you print the parameters in the case of interception, if it is not all possible, it is not possible to be the following method for the page too long.

  getPdf(title) {
    var myDom = document.getElementById('id9527')
    var cloneDom = myDom.cloneNode(true)
    cloneDom.style.position = 'absolute'
    cloneDom.style.top = '0px'
    cloneDom.style.zIndex = '-1'
    cloneDom.style.width = myDom.style.width
    document.body.appendChild(cloneDom)
    html2Canvas(cloneDom, {
      taintTest: false,
      useCORS: true,
    }).then(function (canvas) {
      const contentWidth = canvas.width
      const contentHeight = canvas.height
      const imgWidth = 595.28
      const imgHeight = 592.28 / contentWidth * contentHeight
      const pageData = canvas.toDataURL('image/jpeg', 1.0)
      const PDF = new JsPDF('', 'pt', [imgWidth, imgHeight + 50])
      PDF.addImage(pageData, 'JPEG', 10, 10, imgWidth - 20, imgHeight)
      PDF.save(title + '.pdf')
    })
    cloneDom.remove()
  },

Intelligent Recommendation

JS exports the page as PDF

1, page introduction JS 2, the ID = "PDF-Wrapper" to be exported, used in the 3rd step FUNCTION according to ID DIV 3, JS download method...

Vue exports the current page is PDF, and then saved to the back end

Vue exports the current page is PDF, and then saved to the back end front end Install HTML2CANVAS and JSPDF dependencies Create htmltopdf.js files in Utils, and copy the following code Main.js introdu...

VUE exports PDF format

First, VUE exports PDF format Download two modules The code is as follows (example): 2. Define global functions to create an HTMLTOPDF.js file in the specified location. The code is as follows (exampl...

VUE exports PDF files

Simple logging VUE exports PDF file 1, download package: npm install html2canvas npm install jspdf 2, write JS import html2Canvas from ‘html2canvas’ import JsPDF from ‘jspdf’ d...

More Recommendation

VUE exports PDF

environment vue3.4.0 rely Tools export-pdf.js use .end...

Vue exports PDF documentation

Vue exports PDF documentation Install Import Or in the global main.js global introduction Export PDF 1. Package into a public component 2. Call html...

Ruoyi-Vue exports PDF

I. Back code Introduced 2. Business code Tools Service Controller Two. Front end code api.js Notice: responseType:'blob' Must be added, otherwise the export will report the error. VUE component Three ...

VUE exports multi-page PDF (HTML2CANVAS + JSPDF) (Call currently can't control the upper and lower margins of PDF)

refer tohere 1, turn HTML to Picture Canvas Scrolly: refDom.top, // Key code, intercept length Height: refDom.height // Add height, avoid interception The above two attributes are the key to intercept...

VUE exports DIV or picture to PDF

4 steps 1, download: npm install --save html2canvas npm install jspdf --save 2, create a new JS file under the Utils folder, and copy the following 3. Introducing the page that needs to be exported in...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top