Search code examples
javascriptpdfvuejs2jspdf

Generate pdf with jspdf and Vue.js


I am new with Vue.js, and I am trying to generate a PDF, but I have no idea how to do it.

This is what I have:

import * as jsPDF  from "jspdf"

export default {

  props: ['id'],


  methods: {
    pdf () {
      const doc = new jsPDF()
    }
  }

}

Error:

Property or method "pdf" is not defined on the instance but referenced during render


Solution

  • First import the PDF library as:

    import jsPDF from 'jspdf'
    

    Then simply instantiate the object and give it the contents:

    methods: {
      createPDF () {
        let pdfName = 'test'; 
        var doc = new jsPDF();
        doc.text("Hello World", 10, 10);
        doc.save(pdfName + '.pdf');
      }
    }
    

    Make sure to read the documentation for more