Last active
November 15, 2023 11:53
-
-
Save documentprocessing/154a64c9abc2e45ace7f82c9953869c0 to your computer and use it in GitHub Desktop.
Create PDF Documents with Tables, Images and Password-Protect your PDFs in JavaScript using pdfmake library. Check https://products.documentprocessing.com/editor/javascript/pdfmake/ for more details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import required modules from pdfmake library | |
const pdfMake = require('pdfmake/build/pdfmake'); | |
const pdfFonts = require('pdfmake/build/vfs_fonts'); | |
// Import Node.js built-in fs module for file system operations | |
const fs = require('fs'); | |
// Configure virtual file system of pdfMake | |
pdfMake.vfs = pdfFonts.pdfMake.vfs; | |
// Function to generate PDF | |
function generatePDF() { | |
// Define the content of your PDF document | |
var docDefinition = { | |
userPassword: '123', | |
content: [ | |
{ | |
text: 'This is a password protected pdf', | |
bold: true, | |
style: 'normalText' | |
} | |
] | |
}; | |
// Generate the PDF using the default font settings | |
var pdfDoc = pdfMake.createPdf(docDefinition); | |
// Use the callback function to handle the completion | |
pdfDoc.getBuffer((buffer) => { | |
// Here, you can handle the PDF buffer, such as saving it to a file or sending it as a response. | |
fs.writeFileSync('passwordProtected.pdf', buffer); | |
}); | |
} | |
generatePDF(() => { | |
console.log('PDF generation complete'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import required modules from pdfmake library | |
const pdfMake = require('pdfmake/build/pdfmake'); | |
const pdfFonts = require('pdfmake/build/vfs_fonts') | |
// Configure virtual file system of pdfMake | |
pdfMake.vfs = pdfFonts.pdfMake.vfs; | |
// Function to generate PDF | |
function generatePDF() { | |
// Define the content of your PDF document | |
var docDefinition = { | |
content: [ | |
'Hello, PDFMake!', // A simple text element | |
{ | |
text: 'This is a bold text', | |
bold: true, | |
style: 'normalText' | |
}, | |
{ | |
text: 'This is an italic text', | |
italics: true, | |
style: 'normalText' | |
}, | |
{ | |
text: 'This is a link', | |
link: 'https://example.com', | |
style: 'normalText', | |
color: "blue" | |
}, | |
], | |
}; | |
// Generate the PDF using the default font settings | |
var pdfDoc = pdfMake.createPdf(docDefinition); | |
// Use the callback function to handle the completion | |
pdfDoc.getBuffer((buffer) => { | |
// Here, you can handle the PDF buffer, such as saving it to a file or sending it as a response. | |
const fs = require('fs'); | |
fs.writeFileSync('New.pdf', buffer); | |
}); | |
} | |
generatePDF(() => { | |
console.log('PDF generation complete'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import required modules from pdfmake library | |
const pdfMake = require('pdfmake/build/pdfmake'); | |
const pdfFonts = require('pdfmake/build/vfs_fonts'); | |
// Import Node.js built-in fs module for file system operations | |
const fs = require('fs'); | |
// Configure virtual file system of pdfMake | |
pdfMake.vfs = pdfFonts.pdfMake.vfs; | |
// Function to generate PDF | |
function generatePDF() { | |
// Copy the image to the virtual file system provided by pdfmake | |
const image = fs.readFileSync('./js-logo.jpg', 'base64'); | |
pdfMake.vfs['js-logo.jpg'] = image; | |
// Define the content of your PDF document | |
var docDefinition = { | |
content: [ | |
{ | |
text: 'We are going to add an image to the PDF using the pdfmake library', | |
bold: true, | |
style: normalText | |
}, | |
{ | |
image: 'js-logo.jpg' | |
} | |
] | |
}; | |
// Generate the PDF using the default font settings | |
var pdfDoc = pdfMake.createPdf(docDefinition); | |
// Use the callback function to handle the completion | |
pdfDoc.getBuffer((buffer) => { | |
// Here, you can handle the PDF buffer, such as saving it to a file or sending it as a response. | |
fs.writeFileSync('Image.pdf', buffer); | |
}); | |
} | |
generatePDF(() => { | |
console.log('PDF generation complete'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import required modules from pdfmake library | |
const pdfMake = require('pdfmake/build/pdfmake'); | |
const pdfFonts = require('pdfmake/build/vfs_fonts') | |
// Configure virtual file system of pdfMake | |
pdfMake.vfs = pdfFonts.pdfMake.vfs; | |
// Function to generate PDF | |
function generatePDF() { | |
// Define the content of your PDF document | |
var docDefinition = { | |
content: [ | |
{ | |
table: { | |
// Headers are automatically repeated if the table spans over multiple pages | |
// You can declare how many rows should be treated as headers | |
headerRows: 1, | |
widths: [ '*', 'auto', 100, '*' ], | |
body: [ | |
[ 'Heading 1', 'Heading 2', 'Heading 3', 'Heading 4' ], | |
[ 'Value 1', 'Value 2', 'Value 3', 'Value 4' ], | |
[ { text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Val 4' ] | |
] | |
} | |
} | |
] | |
}; | |
// Generate the PDF using the default font settings | |
var pdfDoc = pdfMake.createPdf(docDefinition); | |
// Use the callback function to handle the completion | |
pdfDoc.getBuffer((buffer) => { | |
// Here, you can handle the PDF buffer, such as saving it to a file or sending it as a response. | |
const fs = require('fs'); | |
fs.writeFileSync('Table.pdf', buffer); | |
}); | |
} | |
generatePDF(() => { | |
console.log('PDF generation complete'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment