Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created May 18, 2019 20:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kmaher9/54b94968a005ea42f055538fa89687b1 to your computer and use it in GitHub Desktop.
Save kmaher9/54b94968a005ea42f055538fa89687b1 to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const PDFDocument = require('pdfkit');
const fs = require('fs');
(async () => {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('http://localhost:8080')
// await page.screenshot({path: 'screenshots/example.png'})
let entries = await page.evaluate(
() => Array.from(document.querySelectorAll('tbody tr td')).map((entry) => entry.innerText)
)
let clients = new Array()
let amounts = new Array()
let dueDates = new Array()
let VATs = new Array()
let x = 1
for (let i = 0; i < entries.length; i++) {
let entry = entries[i]
if (x === 5) x = 1
if (x === 1) clients.push(entry)
if (x === 2) amounts.push(entry)
if (x === 3) dueDates.push(entry)
if (x === 4) VATs.push(entry)
x++
}
for(let i = 0; i < clients.length; i++) {
let client = clients[i]
let amount = amounts[i]
let dueDate = dueDates[i]
let VAT = VATs[i]
const doc = new PDFDocument
let totalFee = ((parseInt(VAT.replace(/[\W_]+/g,'')) * parseInt(amount)) / 100) + parseInt(amount)
doc.pipe(fs.createWriteStream(`PDFs/${client}.pdf`))
doc
.fontSize(15)
.text('Account Invoicing Limited.', 100, 100)
doc
.fontSize(9)
.text('You have an invoice that is due soon, please respond immediately.', 100, 125)
doc
.fontSize(15)
.text(`For the attention of: ${client}`, 100, 175)
doc
.fontSize(10)
.text(`Amount Due: £${amount}`, 100, 220)
doc
.fontSize(10)
.text(`VAT Rate: ${VAT}`, 100, 240)
doc
.fontSize(12)
.text(`Total Due: £${totalFee}`, 100, 260)
doc
.fillColor('red')
.fontSize(12)
.text(`Date Due: ${dueDate}`, 100, 280)
doc
.fillColor('black')
.fontSize(7)
.text(`If you have any issues with this invoice please contact accountant@accountinvoicinglimited.com`, 100, 300)
doc.end()
}
await browser.close()
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment