最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to convert html component to pdf print in nuxt js projects - doc.fromHTML true usage - Stack Overflow

matteradmin7PV0评论

in my nuxt project ı want to convert my html poenent to pdf. when clicked button ı want to convert poenent with all color to pdf.

import {jsPDF} from 'jspdf'
 
export default {
 
    data() {
        return {
             margins : {  top: 80,   bottom: 60, left: 40,  width: 522  }  }
    },
    methods: {
        generatePdf() {
            const doc = new jsPDF('p', 'pt', 'A4'); 
            doc.fromHTML(this.$refs.testHtml, this.margins.left, this.margins.top,{
                'width' : this.margins.width
            });
            doc.save('test.pdf');
        }
    }

}

in here , the problem is about doc.fromHTML. in many project this way is valid but in nuxt doc.fromHTML doesnt work . it gives error.

in my nuxt project ı want to convert my html poenent to pdf. when clicked button ı want to convert poenent with all color to pdf.

import {jsPDF} from 'jspdf'
 
export default {
 
    data() {
        return {
             margins : {  top: 80,   bottom: 60, left: 40,  width: 522  }  }
    },
    methods: {
        generatePdf() {
            const doc = new jsPDF('p', 'pt', 'A4'); 
            doc.fromHTML(this.$refs.testHtml, this.margins.left, this.margins.top,{
                'width' : this.margins.width
            });
            doc.save('test.pdf');
        }
    }

}

in here , the problem is about doc.fromHTML. in many project this way is valid but in nuxt doc.fromHTML doesnt work . it gives error.

Share Improve this question edited Jan 28, 2021 at 3:47 Zac Anger 7,8172 gold badges20 silver badges47 bronze badges asked Jan 27, 2021 at 19:46 user11083200user11083200 1
  • thx for editing my question but my priority is an answer for my question not editing – user11083200 Commented Jan 28, 2021 at 5:39
Add a ment  | 

3 Answers 3

Reset to default 4

I'm late but someone can use this solution.

Use this method:

 printReport() {
     window.print();
  },
<button onClick="printReport">Print !</button>

If you want to hide somthing when printing, use this in CSS.

 @media print {
     .no-printme {
    display: none !important;
  }
  .printme {
    display: block;
  }
}

use vue-html2pdf npm package this will solve your problem and also internally it uses jspdf, so you will get its configuration.

https://www.npmjs./package/vue-html2pdf

vue-html2pdf npm module does not work properly. Rather than you can try html2pdf module for convert html to pdf in vuejs.

Package link: https://www.npmjs./package/html2pdf.js/v/0.9.1

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far