Automatic PDF Invoices In Cryptocurrency Apps

Proper invoicing, whether it occurs through traditional means, i.e. email, and such, or takes place within an app, must contain several bits of information in order to be useful both to the invoicing organization and its customers.

Andrey Bogdanov
Frontend/Backend Developer

Despite the current practice of attaching invoices to email or loading them into cloud storage, a well-architected cryptocurrency application includes automated invoicing for customers. Proper invoicing, whether it occurs through traditional means, i.e. email, and such, or takes place within an app, must contain several bits of information in order to be useful both to the invoicing organization and its customers.

Invoices must describe, at least, the following:

  • Goods or services that were purchased
  • Both the unit and total cost of the goods/services
  • Any applicable sales or exercise taxes for the sale
  • Date of the purchase
  • Due date for delivery and payment of goods/services
  • Customer’s name, address, phone number, and point of contact (if appropriate)
  • Your business name, address, phone number along with tax identification number and point of contact (where applicable)

A proper invoicing process also helps your customer know that payment was received or needs to be made. In addition, invoices are a legal or tax requirement for many businesses.

Let’s imagine you have a PHP application, e.g a Yii application. You can use TCPDF, a very handy PHP library, to generate PDF files from HTML. It creates all standard page formats, custom formats, custom margins, and much more. Further, it requires no external libraries for the basic functions.

When using composer, also called a package manager, the following code snippet enables you to get started on invoicing within a cryptocurrency application in PHP.

composer require "tecnickcom/tcpdf": "*"

Now suppose that your application contains an $order Model (with attributes necessary for invoices, such as id, created_at, order_no, ...) and you want to generate a PDF invoice for your customers.

You need to create a helper. The path in Yii would be along the line of:

“app\helpers\InvoiceHelper”

Use code, such as that displayed below, to create an invoice and set relevant aspects, i.e., page break, margins, title, and subject.

<?php

namespace app\helpers;

class InvoiceHelper
{

   public const TEMP_DIRECTORY = 'invoice_dir';

   protected static function createPdf($data, Order $order): bool
   {
       $pdf = new \TCPDF('P', PDF_UNIT, 'A4', true, 'UTF-8', false);
       $pdf->SetMargins(PDF_MARGIN_LEFT, 20, PDF_MARGIN_RIGHT);
       $pdf->SetAutoPageBreak(true, 40);
       $pdf->setFontSubsetting(false);
       $pdf->SetCreator(PDF_CREATOR);
       $pdf->SetAuthor('Project Name Copyright');

       $pdf->SetTitle('Order #' . $order->id);
       $pdf->SetSubject('Project Name Order #' . $order->id);

       $pdf->AddPage();
       $pdf->SetFont('helvetica', '', 12);


       $content = static::generatePdfContent($order);
       $pdf->writeHTML($content, false, false, false, false, '');

       $date = $order->created_at;

       $year = date('Y', $date);
       $month = date('m', $date);

       $filePath = static::TEMP_DIRECTORY . "$year/$month/";

       try {
           $pdf->Output($filePath, 'F');
           return true;

       } catch (\ErrorException $exception) {
           // Error handling
       }

       return false;
   }


   /**
    * @param Order $order
    * @return string
    */
   protected static function generatePdfContent(Order $order): string
   {

       $contentTable = Yii::$app->controller->renderPartial(
           '@app/helpers/views/invoice_content',
           [
               'order' => $order,
           ]
       );

       return $contentTable;
   }
}

This helper has a simple structure with only two functions. The function generatePdfContent() prepares a view which is converted to a PDF and createPdf() which:

  • uses prepared data
  • sets titles
  • drops a generated file to your path

For other frameworks, you could replace the render function that prepares the view in generatePdfContent() function and use in any PHP application.

Next you will create a view for invoices, where you could use any data and customize it. Consider adding a logo, company address, title, and other useful information.

@app/helpers/views/invoice_content

<table>
    <tr>
        <td><?=Yii::t('app', 'Order No')?></td>
        <td><?= $order->order_number ?></td>        
    </tr>
    
    <tr>
        <td><?=Yii::t('app', 'Order Date')?></td>
        <td><?= $order->order_date ?></td>
    </tr> 
    ....
</table>

The example below illustrates how a PDF invoice might appear in an actual application.

Now you’ve seen an invoice that you can generate from within your PHP application using TCPDF.

Creating invoices automatically is invaluable for many applications including cryptocurrency applications. Make sure you pay attention to the options and choose to generate PDFs from within your app when that solution fits your needs best.

2amigos Consulting Group can help you build your next great PHP project. Have a need or an idea? Get in touch and let’s talk.