Skip to content

Instantly share code, notes, and snippets.

@flee2free
Last active April 11, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flee2free/59e3e2c657d0acded0402114e1329fa7 to your computer and use it in GitHub Desktop.
Save flee2free/59e3e2c657d0acded0402114e1329fa7 to your computer and use it in GitHub Desktop.
[Image into PDF] Converting Images into PDF using PDF Make #javascript #bash
#!/bin/zsh
DIR=/Users/bbudhathoki/_Projects/Rejoice/create-pdf
rm -rf "$DIR/data/" "$DIR/pdfs/" 2> /dev/null
mkdir -p $DIR/{data,pdfs} 2> /dev/null
cp -rf ./ "$DIR/data/"
cd $DIR || exit
node ./index.js
open -a finder $DIR/pdfs
import fs from 'fs'
import PdfPrinter from "pdfmake"
(async function () {
const path = './data/'
const files = fs.readdirSync(path)
let images = []
let counter = files.length
for (const file of files.sort()) {
if (file === '.DS_Store') { continue }
images.push({
image: path + file,
fit: [530, 300],
pageBreak: (--counter) ? 'after' : ''
})
}
console.log(images)
let fonts = {
Roboto: {
normal: 'fonts/Roboto-Regular.ttf',
bold: 'fonts/Roboto-Medium.ttf',
italics: 'fonts/Roboto-Italic.ttf',
bolditalics: 'fonts/Roboto-MediumItalic.ttf'
}
};
let printer = new PdfPrinter(fonts);
let doc =
{
"background": function (currentPage, pageSize) {
return {
image: './fonts/background.png',
width: pageSize.width,
height: pageSize.height - 20,
margin: [0, 10, 0, 0]
}
},
content: images,
};
// noinspection JSCheckFunctionSignatures
let pdfDoc = printer.createPdfKitDocument(doc);
pdfDoc.pipe(fs.createWriteStream('./pdfs/extract.pdf'));
pdfDoc.end();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment