Are you trying to work with PDF in Nodejs? In this article, we will discuss the package pdfmake. pdfmake is a library that can be used for server-side and client-side to create pdfs, which is purely written in JavaScript. We will get started with pdfmake, and create a basic pdf file.
Let's get started.
We will be working on server-side, so let's initialize a Node project.
:~/working-with-pdf$npm init -y
Let's create an index.js file, and update the package.json by adding the start script.
Now, Let's install the pdfmake
:~/working-with-pdf$npm install pdfmake
Before getting started, we will need fonts. By default, pdfmake will look for 'Roboto' in style 'normal'. So let's download the fonts. [Roboto on Google Fonts]. Once downloaded, extract the zip and place the font files on font/roboto/
Now we will instantiate the pdfmake, define a document, pass the defined doc to createPdfKitDocument to get PdfKit instance. Create a stream and pipe it to pdfKit instance.
Make sure you have created that pdfs/ directory first.
createPdfKitDocument will return the instance of pdfKit, which is a readable Node stream. They don't get saved anywhere automatically. So we call the pipe method to send the output of the document to another writeable Node stream. We can pipe it to HTTP response as well.
Now the pdf is created.
Header and Footer
We can define a header as well as a footer with pdfmake, all the pages the content extended to will have the header and footers.
We will use an image on the header as well, so let's create a images/ directory and place an image there. Here we have info.png on images/
That's it, now we have a page number on all pages at the right of the footer.
Styling
We have already used some styling. We have used inline styling whenever needed. In pdfmake we can define style and reuse as required. We will define some styles and create pdf.
letstylingDocs = {
content: [{
text:"Report",
style:'header'
}, {
text:'This is an anual report. bla bla, some text goes here about this and that',
style:'text'
}, {
text:"About Blogger Nepal",
style:'subheader'
}, {
text:'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse porta, lectus ac pellentesque suscipit, lacus felis cursus nisi, ac dictum velit ipsum ac dolor. Ut erat eros, dictum nec porttitor non, rhoncus sed augue. Maecenas consectetur aliquam lorem. Duis id congue risus. Ut vel rutrum massa, eget convallis nulla. Praesent ac sagittis diam. Aliquam erat volutpat. Fusce posuere sapien vitae suscipit interdum. Curabitur in mattis massa, vel aliquet ex. Vestibulum ornare lorem et sollicitudin commodo. Sed id vestibulum ligula. ',
style:'text'
}, {
text:'\n'
}, {
text:'Nunc laoreet mauris sed vestibulum pellentesque. Nunc suscipit ante placerat orci pulvinar varius. Sed in velit maximus odio rutrum molestie et a enim. Mauris vitae dui rutrum tortor euismod tempor in eu diam. Fusce pulvinar nunc at feugiat pharetra. Morbi tempus accumsan finibus. Suspendisse dictum augue eu finibus volutpat. Suspendisse vel ipsum quis eros tincidunt posuere id eu nunc. Sed cursus sodales accumsan. Sed interdum dolor ac dignissim eleifend. ',
Now, we have created the file, but how do we use it? It won't be available right away. We should make a way when we are notified once the write is completed. We can achieve this by listening to the finish event on writestream.
That's it we worked with pdfmake and created several pdf files. Hope you get some understanding on pdfmake, and can work with it as required.
If you are trying to work with pdf in Node.JS, the module you must have to remember is pdfmake. This module helps a lot while working with pdf. pdfMake is a package that helps in generating pdf. files. This package is purely written in JavaScript and can be directly used on the client-side or on the server-side via node.
For the Client version to install you need to type:
bower install pdfmake
For the Server version in the terminal you need to type:
npm install pdfmake
If only want to use pdfMake on the client-side vfs_fonts.js is required for fonts. The file is a binary which includes the fonts.
PdfMake helps to print pdf's directly in the browser or delegate it to NodeJs backend using the same document definition in both cases.
Once you have pdfMake no worry about the manual calculation of x,y positioning etc. Just you have to declare document structure and let pdfmake do the rest.
You can declare your own styles, use custom fonts, build a DSL and extend the framework. Using paragraphs, columns, lists, tables, canvas, etc. customizing and styling document structure.
2 Comments
How to add base64 images
ReplyDeleteNice, concise and very helpful article.
ReplyDelete