[
  {
    "path": ".gitignore",
    "content": "node_modules\ninvoice.pdf\n"
  },
  {
    "path": "README.md",
    "content": "# PDFKit Invoices\n\n<center>\n  <a href=\"https://pspdfkit.com/web\">\n    <img src=\"./screenshot.png\" alt=\"Screenshot of an invoice PDF in the PSPDFKit for Web reader.\" width=\"1009\">\n  </a>\n</center>\n\n## Prerequisites\n\n- [Node.js](http://nodejs.org/) (with npm or Yarn)\n\n## Getting Started\n\nThis repository is an example of how to make PDF invoices with PDFKit.\n\nThere are two important fields in this this repository:\n\n- [`index.js`](index.js) is the main entry point. It defines the data structure used to create the invoices.\n- [`createInvoice.js`](createInvoice.js) exports a function that can be used to create invoice PDFs.\n\nTo get started, use the following commands:\n\n```bash\ngit clone https://github.com/PSPDFKit-labs/pdfkit-invoice.git\n\nnpm install  # Install dependencies\n\nnpm start  # This will create an invoice.pdf file in the root of the project.\n```\n\nTo learn more about this project, make sure to read the accompanying [blog post](https://pspdfkit.com/blog/2019/generate-invoices-pdfkit-node)\n\n## Contributing\n\nPlease ensure\n[you have signed our CLA](https://pspdfkit.com/guides/web/current/miscellaneous/contributing/) so that we can\naccept your contributions.\n"
  },
  {
    "path": "createInvoice.js",
    "content": "const fs = require(\"fs\");\nconst PDFDocument = require(\"pdfkit\");\n\nfunction createInvoice(invoice, path) {\n  let doc = new PDFDocument({ size: \"A4\", margin: 50 });\n\n  generateHeader(doc);\n  generateCustomerInformation(doc, invoice);\n  generateInvoiceTable(doc, invoice);\n  generateFooter(doc);\n\n  doc.end();\n  doc.pipe(fs.createWriteStream(path));\n}\n\nfunction generateHeader(doc) {\n  doc\n    .image(\"logo.png\", 50, 45, { width: 50 })\n    .fillColor(\"#444444\")\n    .fontSize(20)\n    .text(\"ACME Inc.\", 110, 57)\n    .fontSize(10)\n    .text(\"ACME Inc.\", 200, 50, { align: \"right\" })\n    .text(\"123 Main Street\", 200, 65, { align: \"right\" })\n    .text(\"New York, NY, 10025\", 200, 80, { align: \"right\" })\n    .moveDown();\n}\n\nfunction generateCustomerInformation(doc, invoice) {\n  doc\n    .fillColor(\"#444444\")\n    .fontSize(20)\n    .text(\"Invoice\", 50, 160);\n\n  generateHr(doc, 185);\n\n  const customerInformationTop = 200;\n\n  doc\n    .fontSize(10)\n    .text(\"Invoice Number:\", 50, customerInformationTop)\n    .font(\"Helvetica-Bold\")\n    .text(invoice.invoice_nr, 150, customerInformationTop)\n    .font(\"Helvetica\")\n    .text(\"Invoice Date:\", 50, customerInformationTop + 15)\n    .text(formatDate(new Date()), 150, customerInformationTop + 15)\n    .text(\"Balance Due:\", 50, customerInformationTop + 30)\n    .text(\n      formatCurrency(invoice.subtotal - invoice.paid),\n      150,\n      customerInformationTop + 30\n    )\n\n    .font(\"Helvetica-Bold\")\n    .text(invoice.shipping.name, 300, customerInformationTop)\n    .font(\"Helvetica\")\n    .text(invoice.shipping.address, 300, customerInformationTop + 15)\n    .text(\n      invoice.shipping.city +\n        \", \" +\n        invoice.shipping.state +\n        \", \" +\n        invoice.shipping.country,\n      300,\n      customerInformationTop + 30\n    )\n    .moveDown();\n\n  generateHr(doc, 252);\n}\n\nfunction generateInvoiceTable(doc, invoice) {\n  let i;\n  const invoiceTableTop = 330;\n\n  doc.font(\"Helvetica-Bold\");\n  generateTableRow(\n    doc,\n    invoiceTableTop,\n    \"Item\",\n    \"Description\",\n    \"Unit Cost\",\n    \"Quantity\",\n    \"Line Total\"\n  );\n  generateHr(doc, invoiceTableTop + 20);\n  doc.font(\"Helvetica\");\n\n  for (i = 0; i < invoice.items.length; i++) {\n    const item = invoice.items[i];\n    const position = invoiceTableTop + (i + 1) * 30;\n    generateTableRow(\n      doc,\n      position,\n      item.item,\n      item.description,\n      formatCurrency(item.amount / item.quantity),\n      item.quantity,\n      formatCurrency(item.amount)\n    );\n\n    generateHr(doc, position + 20);\n  }\n\n  const subtotalPosition = invoiceTableTop + (i + 1) * 30;\n  generateTableRow(\n    doc,\n    subtotalPosition,\n    \"\",\n    \"\",\n    \"Subtotal\",\n    \"\",\n    formatCurrency(invoice.subtotal)\n  );\n\n  const paidToDatePosition = subtotalPosition + 20;\n  generateTableRow(\n    doc,\n    paidToDatePosition,\n    \"\",\n    \"\",\n    \"Paid To Date\",\n    \"\",\n    formatCurrency(invoice.paid)\n  );\n\n  const duePosition = paidToDatePosition + 25;\n  doc.font(\"Helvetica-Bold\");\n  generateTableRow(\n    doc,\n    duePosition,\n    \"\",\n    \"\",\n    \"Balance Due\",\n    \"\",\n    formatCurrency(invoice.subtotal - invoice.paid)\n  );\n  doc.font(\"Helvetica\");\n}\n\nfunction generateFooter(doc) {\n  doc\n    .fontSize(10)\n    .text(\n      \"Payment is due within 15 days. Thank you for your business.\",\n      50,\n      780,\n      { align: \"center\", width: 500 }\n    );\n}\n\nfunction generateTableRow(\n  doc,\n  y,\n  item,\n  description,\n  unitCost,\n  quantity,\n  lineTotal\n) {\n  doc\n    .fontSize(10)\n    .text(item, 50, y)\n    .text(description, 150, y)\n    .text(unitCost, 280, y, { width: 90, align: \"right\" })\n    .text(quantity, 370, y, { width: 90, align: \"right\" })\n    .text(lineTotal, 0, y, { align: \"right\" });\n}\n\nfunction generateHr(doc, y) {\n  doc\n    .strokeColor(\"#aaaaaa\")\n    .lineWidth(1)\n    .moveTo(50, y)\n    .lineTo(550, y)\n    .stroke();\n}\n\nfunction formatCurrency(cents) {\n  return \"$\" + (cents / 100).toFixed(2);\n}\n\nfunction formatDate(date) {\n  const day = date.getDate();\n  const month = date.getMonth() + 1;\n  const year = date.getFullYear();\n\n  return year + \"/\" + month + \"/\" + day;\n}\n\nmodule.exports = {\n  createInvoice\n};\n"
  },
  {
    "path": "index.js",
    "content": "const { createInvoice } = require(\"./createInvoice.js\");\n\nconst invoice = {\n  shipping: {\n    name: \"John Doe\",\n    address: \"1234 Main Street\",\n    city: \"San Francisco\",\n    state: \"CA\",\n    country: \"US\",\n    postal_code: 94111\n  },\n  items: [\n    {\n      item: \"TC 100\",\n      description: \"Toner Cartridge\",\n      quantity: 2,\n      amount: 6000\n    },\n    {\n      item: \"USB_EXT\",\n      description: \"USB Cable Extender\",\n      quantity: 1,\n      amount: 2000\n    }\n  ],\n  subtotal: 8000,\n  paid: 0,\n  invoice_nr: 1234\n};\n\ncreateInvoice(invoice, \"invoice.pdf\");\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"pdfkit-invoice\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"index.js\",\n  \"scripts\": {\n    \"start\": \"node index.js\",\n    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\n  },\n  \"keywords\": [],\n  \"author\": \"\",\n  \"license\": \"ISC\",\n  \"dependencies\": {\n    \"pdfkit\": \"^0.13.0\"\n  }\n}\n"
  }
]