Genii Weblog

Down to Business - PDF invoices from Notes data with Node

Mon 20 Apr 2020, 10:30 AM



by Ben Langhinrichs
Inline JPEG image
 
Because the business of the world is business, I'm starting an occasional series of posts on how our Exciton Power product can drive practical business goals. One frequently requested feature is the ability to create PDFs programmatically, not simply printing a Notes form but actually creating the PDFs. I asked on Facebook for suggestions about real-life ways that customers use PDFs, and John Head suggested invoices from a classic main doc/response doc roll-up.
 
On Friday morning, I had time to pull together a demo. I emphasize the time because it only took about an hour to figure out how to create the invoice logic using PDFKit (a free Node.js module) and Exciton Power (our product in beta). It took more time to create a simple pair of databases with dummy data. The power in Exciton Power and Node.js is not about invoices or PDFs, it is about the ability that Notes developers will recognize to be presented with a business problem and be able to code it and put it into action in a matter of hows, not days and weeks.
 
Here is a short video showing the process. For those interested in how it is coded, the JavaScript I used will follow the video. [Note: I added subtitles to make this more accessible.]
 
 
If you would be interested in participating in the Exciton beta, or even if you just have questions, contact me by e-mail at 
 
Source Code for demo:
 
The code I use that is specific to Exciton Power is 54 lines and probably fairly familiar stuff. I walk a view using a GetFirstDoc/GetNextDoc combination. One quirk of the Exciton code is that instead of returning something like a NotesViewEntry, we return meta-data about the document and the specific items requested, bundled as a JSON string. In this case, one of the items is even an @DBLookup to a separate database to retrieve a product description.
 
[Note: Once we release Exciton Power out of beta, I will post all sample code on github, (as I has been gently recommended).. At the moment, syntax is open to change, so I don't want to put up code that may not work with the Gold release.]
 
demo2.js
const useExcitonPower = require('./build/debug/ExcitonPower32');
const { createInvoice } = require("./createInvoice.js");
 
// *** Initiate a session
const gs = useExcitonPower();
 
// *** Create a collection and add a view to it
const coll = gs.useGCollection();
console.log(coll.addByView("", "AcroBatsCRM.nsf", "Customers"));
 
// *** Cycle through the view, looking for invoices and their line item response documents
let doc = coll.getFirstDoc("CompanyName,InvNo,Contact,Address,City,State");
let count = 0;
let total = 0.0
let invoiceStarted = false;
while (doc !== null && count < 10)
  {
  let docobj = JSON.parse(doc);
  if (invoiceStarted && docobj['@form'] !== "Line Item")
    {
    invoice.subtotal = total;
    invoice.paid = 0.0;
    createInvoice(invoice);
    console.log("Created invoice as "+invoice.filename+" for $"+total); 
    invoiceStarted = false;
    }
  if (docobj['@form'] == "Invoice") 
    {
    invoice = {InvNo: docobj.InvNo, filename: docobj.CompanyName+" - "+docobj.InvNo+".pdf", shipping: docobj, items: []};
 
    invoiceStarted = true;
    total = 0.0;
    count = 0;
    }
  else if (docobj['@form'] == "Line Item" && invoiceStarted)
    {
    count++;
    doc = coll.getDocByUNID(doc, "ItemNo,Qty,Price,Total,@DbLookup(\"\":\"\"; \"\":\"AcroBatsPRD.nsf\"; \"Products\"; ItemNo; \"ItemDesc\")=ItemDesc");
    docobj = JSON.parse(doc);
    invoice.items.push(docobj);
    total += docobj.Total;
    }
 
  doc = coll.getNextDoc(doc, "CompanyName,InvNo,Contact,Address,City,State");
  }
if (invoiceStarted)
  {
  invoice.subtotal = total;
  invoice.paid = 0.0;
  createInvoice(invoice);
  console.log("Created invoice as "+invoice.filename+" for $"+total); 
  }
 
The createInvoice.js script I used is based fairly directly on the example in Generate Invoices with PDFKit on Node.js. Again, I want to emphasize that the value to a Notes/Domino developer of being able to work in Node.js is the power to quickly and easily use resources like PDFKit. Many are free, some cost something, but all extend the edges of development far, far beyond what we currently have in Notes.
 

Copyright © 2020 Genii Software Ltd.

What has been said:

No documents found