create custom pdfs with node js code example

Example 1: node js serve pdf file

app.use('/pdf', express.static(__dirname + '/pathToPDF'));

Example 2: node js create pdf from html

The problem with using PDF converter libraries available on NPM like pdfkit is that, you gonna have to recreate the page structures again in your html templates to get the desired output.

One of the best approach to rendering html and convert to pdf is by using Puppeteer on NodeJs. Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It can be used to generate screenshots and PDFs of html pages in your case.

Example 3: extract data from pdf nodejs

// Installation
// npm install pdf-parse

// Basic Usage
const fs = require('fs');
const pdf = require('pdf-parse'); 
let dataBuffer = fs.readFileSync('path to PDF file...'); 
pdf(dataBuffer).then(function(data) {
     /*  number of pages    
	console.log(data.numpages);     
	number of rendered pages    
	console.log(data.numrender);     
	PDF info    
	console.log(data.info);     
	PDF metadata    
	console.log(data.metadata);      
	PDF.js version     
	check https://mozilla.github.io/pdf.js/getting_started/    
	console.log(data.version);     PDF text    console.log(data.text);  */ 
});