PHP create PDF invoice

82,428

Solution 1

I use TCPDF (see http://www.tcpdf.org/) for this: its pretty capable and not too painful to get setup. I will say that depending on your data source you may have some issues. In my case my data is sourced from a SOAP interface to my accounting system and use CodeIgniter for my app, and I can do this:

$address = $extraclient->get_company_address();

// generate the PDF invoice
$this->load->library('pdfinvoice');

// set document information
$this->pdfinvoice->SetSubject("Invoice " . $data_invoice['code_invoice']);

// add a page
$this->pdfinvoice->AddPage();
$this->pdfinvoice->StartPageOffset();

// write the client's details out
$width = $this->pdfinvoice->GetPageWidth()/2;
$margins = $this->pdfinvoice->getMargins();
$this->pdfinvoice->SetFont('times', 'b', $this->pdfinvoice->bigFont );
$this->_row($width, array("From:", "To:"));
$this->pdfinvoice->SetFont('times', 'i', $this->pdfinvoice->smallFont );
$this->_row($width, array("MY NAME", $customer['name_contact']));
$this->_row($width, array($address['phone'], $customer['name_customer']));
$this->_row($width, array($address['street'], $customer['address1_street']));
$this->_row($width, array($address['city']. ", ".$address['state']." ".$address['zipcode'],
                          $customer['address1_city']. ", ".$customer['address1_state']." ".$customer['address1_zip

The full code is quite frankly too long to insert here, but you should get the idea, and you get fairly precise layout control.

Solution 2

I would recommend creating an html / css representation of what you want a PDF of and using that to generate the PDF. There are dozens of applications to handle the conversion, and there is a good thread with a lot of answers here: Convert HTML + CSS to PDF with PHP?

Solution 3

You may wanna look at html2Pdf. It is a php class based on FPDF and it allows you to create PDF file from HTML. So just format your text using html and then create its pdf. Its very flexible and give greate control.

SourceForge Link

Solution 4

Checkout the php-pdf-invoice library from composer. The library is already designed for generating an invoice so it's easy and quick to integrate!

composer require quickshiftin/php-pdf-invoice

You can generate PDF files that look like this

Here's a sample invoice

There is a usage example on the README. Essentially you implement two interfaces, one for orders, another for order items. You can configure fonts and colors to your liking as well.

Share:
82,428
Martin
Author by

Martin

Updated on August 04, 2022

Comments

  • Martin
    Martin almost 2 years

    Hi does anyone know how I can create a nicely formatted PDF invoice through PHP?

    Ideally I'm looking for something with a header and then an itemised listing of the products with some sort of table around. After a quick Google I would be comfortable with generating a PDF but to try and style it nicely would be another thing altogether.

    Thanks