Skip to main content

Angular Export to PDF using PDFMake

https://github.com/ngdevelop-tech/angular-8-export-pdf


Angular Export to PDF using PDFMake

Generating PDF for reports, forms, invoices, and other data is a common use case for any web application. In a web application, We can generate pdf using various approaches:

  1. Using browser print function: It is an easy option when you want to print a complete web page as a pdf. You can also customize pdf up to some limits. limitation of this approach is we don’t have strong control on format and design of pdf.
  2. Generating PDF using Backend application or third-party reporting tools and download it on client-side: You have more control over pdf formatting and design and you can process large amounts of data. Though this type of pdf generation approach required a separate API call for generating the pdf.

We can solve the limitations of both ways by generating pdf at client side. We can format and design pdf as per our requirement without calling separate API.

Following are the two popular open-source javascript libraries available for client-side pdf generation. 

In this article, I will show you how to export a pdf file in angular 8 using pdfmake.

Further, Read Below PDFMake Articles

Angular 8 Export to PDF using PDFMake

PDFMake is very popular client-side and server-side pdf generation javascript library. It has 100,000+ weekly downloads from npm. And 7K+ GitHub stars

It is easy to use and provides all required features for pdf design and formatting with some extraordinary features like QR Code, Table of contents and Helper methods for Opening pdf, download pdf, and pdf printing. 

We will create an Online resume builder application in angular 8 and PDFMake. Here we will get the personal details, educational details and experience details and generate pdf at client side. We will provide different options for print pdf and download pdf.

I have published this application on netlify you can check it here https://online-resume-builder.netlify.com/

Create a Angular Project

Use below command to create a new Angular project with Angular CLI.

ng new online-resume-builder

Install PDFMake Library

npm install --save pdfmake

Import pdfmake and vfs_fonts

To begin in browser with the default configuration, we should include two files 

Pdfmake.js
 and 
vfs_fonts.js
. When you install 
Pdfmake
 from npm it comes with the both file.

Now to use this files in angular component or service add below import statement on top of component/service

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;

Generate single line text pdf for testing our environment setup

PDFMake follows a declarative approach. It basically means, you’ll never have to calculate positions manually or use commands like 

writeText(text, x, y)
moveDown
etc…, as you would with a lot of other libraries. The most fundamental concept to be mastered is the 
document-definition-object
 which can be as simple as:

All the pdf formatting and design configuration are written in document-definition-object. As shown below :

export class AppComponent {
generatePdf(){
const documentDefinition = { content: 'This is an sample PDF printed with pdfMake' };
pdfmake.createPdf(documentDefinition).open();
}
}
<button (click)="generatePdf()">Open PDF</button>

Add 

Open PDF
 button  on 
app.component.html
 and call 
generatePdf()
.

Serve your application and test. This will show pdf as below : 

Initial PDF generated using PDFMake

If you can see above pdf on click of Open PDF button then environment setup is done. Now we can start with our resume builder application

PDFMake comes with inbuilt methods :

  • Download the PDF : 
    pdfMake.createPdf(docDefinition).download();
  • Open the PDF in new window : 
    pdfMake.createPdf(docDefinition).open();
  • Open PDF in same window : 
    pdfMake.createPdf(docDefinition).open({}, window);
  • Print the PDF: 
    pdfMake.createPdf(docDefinition).print();

PDFMake also provides methods for :

  • Put the PDF into your own page as URL data
  • Get the PDF as base64 data
  • Get the PDF as a buffer
  • Get the PDF as Blob

Refer here for more details.

Same as PDF, You can generate excel at client side. Refer Export to Excel in Angular blog for client-side excel generation in Angular.

Now for an online resume builder, I have designed a resume form to get personal details, skills, experience details, education details, and other details. As shown below :

Online Resume Builder Form Design | Client side pdf generation using Angular and PDFMake

Here I have used the template-driven form and 

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...