Last active
April 11, 2021 14:24
-
-
Save flee2free/59e3e2c657d0acded0402114e1329fa7 to your computer and use it in GitHub Desktop.
[Image into PDF] Converting Images into PDF using PDF Make #javascript #bash
This file contains 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
#!/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 |
This file contains 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 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