Skip to content

Instantly share code, notes, and snippets.

@samson-sham
Created September 8, 2022 07:47
Show Gist options
  • Save samson-sham/bc3e548338cae978ac8c978e20f1dc18 to your computer and use it in GitHub Desktop.
Save samson-sham/bc3e548338cae978ac8c978e20f1dc18 to your computer and use it in GitHub Desktop.
make JPGs to PDF
import PDFDocument from 'pdfkit';
import fs from 'fs';
const pageProp = {
size: 'A4',
layout: 'landscape',
autoFirstPage: false,
};
const doc = new PDFDocument(pageProp);
doc.pipe(fs.createWriteStream('./document.pdf'));
fs.promises.readdir('.')
.then(files => files.filter(file => file.endsWith('.jpg')))
.then(files => files.forEach((file, i) => {
doc.addPage(pageProp);
doc.image(file, 0, 0, { fit: [842, 596], align: 'center', valign: 'center'});
}))
.then(() => doc.end());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment