Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Sitemap
Menu

jsPDF Tutorial to Set Custom Fonts in PDF Document Using Javascript

Posted on December 17, 2022

 

 

Welcome folks today in this blog post we will be setting the custom fonts in pdf document using jspdf library in javascript. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make an index.html file and copy paste the cdn of jspdf as shown below

 

 

index.html

 

 

1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>

 

 

And now we will be writing the javascript code to get all the custom fonts which are there inside the jspdf library. We can use a method called as getFontList() which will return the total number of fonts which are there inside the jspdf library.

 

 

JavaScript
1
2
3
var doc = new jsPDF()
 
console.log(doc.getFontList())

 

 

 

 

As you can see an object is returned and it contains a number fonts and it’s sub font types such as bold and italic. Now you can use any of the fonts inside the pdf documents using the jspdf setFont() method as shown below

 

 

JavaScript
1
2
3
4
5
6
7
doc.setFont('Courier')
 
  doc.setFontType('Oblique')
 
  doc.text("hello world",10,10)
 
  doc.save("output.pdf")

 

 

As you can see we are setting the courier font and oblique font type inside the pdf document and then saving it as output.pdf in browser.

 

Recent Posts

  • Build a Fixed Deposit Interest Calculator Using Amount and Time in Browser Using HTML5 & Javascript
  • How to Download Files From URL in Node.js Using Node-Downloader-Helper Library
  • Angular 10 Image Carousel and Video Gallery Lightbox Modal Slider Using ng-image-slider Library
  • React Unsplash Api Photo Search App
  • React Form Validation Using Formik and Yup
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • javascript
  • Koajs
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme