Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 880
Carlos Uriel Santiago Avatar asked Dec 06 '17 06:12

Carlos Uriel Santiago


People also ask

How to convert html to PDF in vue js?

To get started, first install html2pdf. js and its dependencies using npm by running npm install --save html2pdf. js in your terminal. export default { name: 'app', methods: { exportToPDF() { html2pdf(document.

Can we generate PDF using JavaScript?

Generate PDF using JavaScriptSpecify the content in text() method of jsPDF object. Use the addPage() method to add new page to PDF. Use the save() method to generate and download PDF file.

What is JavaScript jsPDF?

What is jsPDF? jsPDF is an open-source library for generating PDF documents using JavaScript. It provides multiple options to generate PDF files, with custom properties. It has numerous plugins to support various ways of PDF generation.


1 Answers

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

like image 197
samayo Avatar answered Sep 19 '22 12:09

samayo