Vue JS PDF generate tutorial for begineers
Learn to generate PDF files with Vue JS in the simplest way. Our Vue JS PDF generate tutorial has covered the concept from scratch so you'll find it really easy as well as convenient.
Contents

Vue js has become our most favorite front-end JavaScript framework. We always astonished with the great features it provides. The task of PDF generation becomes really easy with Vue JS. In this Vue JS PDF generate tutorial we will look at the entire process to generate PDF with Vue JS.
Vue JS PDF generate
First of all, we will create a new project as follows. If you are new to Vue then we suggest you check out Vue JS tutorial for beginners first.
vue init webpack vuepdf
We need a library called jsPDF for this tutorial. So once the project directory is ready, move inside of it and save the library as follows.
npm i jspdf --save
So the above command will save the library as a dependency. Now we are ready to move further.
Creating the template
We have installed the router in our project. So we will directly move inside of the components folder and create a new component called Home.
Now let’s add the markup for our component as follows.
<template>
<div id="home" class="container mt-5">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card card-body">
<div class="form-group">
<input type="text" id="name" v-model="name" class="form-control" placeholder="Enter name here">
</div>
<button class="btn btn-primary pl-5 pr-5" @click="download">Download PDF</button>
</div>
</div>
</div>
</div>
</template>
As you can see we have used bootstrap classes in the above markup. You can simply add the bootstrap CDN within the index HTML file.
The above markup just contains an input field and a button. The input field is bonded with the data attribute name. On the other side, the button is also bonded with a click event.
Creating the script
Now as our template part is ready so let’s move to the script part.
First of all, before everything we need to import the jsPDF library as follows.
import jsPDF from 'jsPDF'
Now after that, we need to write code for our data and methods.
export default {
name: 'home',
data() {
return {
name: ''
}
},
methods: {
download () { }
}
}
As you can see we have created a string variable called name. It’s the one that’s bonded with our input field.
Later that we have initialized an empty method called download. This is the place where we will add code to write and download our PDF file.
let pdfName = 'test';
var doc = new jsPDF();
doc.text(this.name, 10, 10);
doc.save(pdfName + '.pdf');
A simple 4 lines of code will magically create the PDF file and write the contents and make it available for you to download. Now let’s see what each line of code does.
In the first line, we have named the PDF file. Later that we have initialized the imported jsPDF object. Next, we have used the text method to write the name inside of our form field. This method can accept a few other parameters to add spacing and font size.
Finally, the save method will download the file.
So that’s all now let’s move to the final part of our Vue js PDF generate tutorial.
Modifying the router
Once the component is ready we need to add it to the router. So that we can display it on our specified URL. We would like to display it in the root URL. We can do it as follows.
First of all, import the Home component from the components directory.
import Home from '@/components/Home'
Now let’s add it to our routes array as follows.
routes: [
{ path: '/', name: 'Home', component: Home }
]
So finally run the command below and go to localhost:8080.
npm run dev
We have come to the end of our Vue JS PDF to generate a tutorial. Hope you have liked this one. This one is quite shorter but informative so don’t forget to share with your buddy developers.
Comments
ddd
dddd
000
Reply
---------------+++++++++++++++++
Dghj
Reply
Tyhgf
Галстян Эмма
Reply
ds
Punyapha Yeamklud
Reply
xdg
Andre dolonski
Reply
THis is a comment
Andre dolonski
Reply
<input
Jack Schmidt
Reply
aaaaaaaaaa
54634
Reply
4356
AZOOUMA
Reply
teste trs cool
fh
Reply
sdfsdf
123
Reply
12312
cxdccd
Reply
ytgcgckgh
A
Reply
A
jbkb
Reply
bkjbkjb
gfgfg
Reply
fgfgf
aaa
Reply
aaa
Gros love
Reply
<3
mmmmmmmmmmm
Reply
kkkkkkkkkkkkkkkkk
dwd
Reply
dqwdwqd
kitti.pis@ku.th
Reply
efwEFQWERFAQF3WR
ii
Reply
ii
Rizki
Reply
halo
Younes
Reply
dzda
home
Reply
This is a Test message for test PDF genereter
sfgdfg
Reply
dgfdf
df
Reply
cvb
e5h6r6rh
Reply
r6h6
hui
Reply
wtf?
ad
Reply
dawda
dfdf
Reply
fdf
giang
Reply
beo nef
dfdfdfdf
Reply
fdfdfd
fedfe
Reply
fefe
asd
Reply
sadasdasd
asd
Reply
chachi
vior
Reply
Makasih
sdf
Reply
dsfdf
ddd
if you run into dependency issues, use: import jsPDF from 'jspdf' instead, this should work
mr z
Reply
hello world
asudyf
Reply
gluyv
Alexandre
Reply
afasm;fas;fas
fefe
Reply
suk dik
fhjkj
Reply
komentar
TEST
Reply
Lorem
ggg
ddd000
uva
Reply
Todo el mundo es UVA
hgjghjg
hjghj
gdjn
Reply
dfn
adadaaad
dasdasdasd
123123
Reply
321321321
کیرم تو کص ننت
Reply
آمریکا
ddd
Reply
asdf
Ejemplo
Reply
123345
اكرم
Reply
Test
Babab Black sheep
Reply
Hellooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Esse labore fugiat
Architecto animi an
یه پیام دارم
Reply
یه پیام دارم برای ادمین عزیز ....
jnjnk
nknnkk;
Marvin Jose Mejia Mejia
Reply
123
sdfsd
Reply
d
jua
Reply
dr gregerg
Youssouph Ndiour
BONJOUR EST CE QU'ON PEUT GENERER A PARTIR D'UN TABLEAU SANS UTILISER AUTOTABLE? MERCI
mr z
hello world
ooi
Reply
oi
asdfasd
safasdfg
sddddddddddddddddd
test
hello world
klnkl
smart Boy
nice
test
test
thoughts
thoughts
satu
Reply
vuejs PDF
b
Reply
b
cvb
vbvb