Creating PDF Invoices - Are There Any Templating Solutions

How to bulk generate PDF from HTML templates ready for double-sided printing in PHP?

Unfortunately wkhtmltopdf is the library, which is written in C language and we can not dynamically add one page on the fly like in PHP libraries.

Citate from your comment: Due to number of items ordered or ammount of customer data each invoice can be anywhere from 1 to 3 pages long.

And because of this we can not precalculate the number of pages and write it to a database.

I think you have only one possibility / solution: you have to write behind each invoice a blank page and after the whole PDF was generated you have to edit it with free PHP library like FPDI. In combination with FPDI it is even possible to edit PDF documents.

By PDF editing you could delete all blank pages which you do not need if they starts with odd page number (like 3, 5, etc.). And in FPDI you have the possibility to detect a page number. It is much faster than the solution which you use now.

And the blank(or empty) pages you could detect on content length with FPFI like follows:

<?php
require('fpdf.php');
require_once('setasign/Fpdi/autoload.php');

class Pdf extends \setasign\Fpdi\Fpdi
{
private $pdfReader;
function Header()
{
if(is_null($this->pdfReader))
{
$readerId = $this->getPdfReaderId('blank-pages.pdf');
$this->pdfReader = $this->getPdfReader($readerId);
}

$page_fpdi = $this->pdfReader->getPage($this->PageNo());
$this->content = $page_fpdi->getContentStream();

$this->Cell(0, 15, 'page content length: '.strlen($this->content));
}

protected function _putimages(){}
}

$pdf = new Pdf();
$pdf->SetFont('Arial', '', 12);
$pdf->AddPage(); //page content length: 70 // page with 'Hello World!' string
$pdf->AddPage(); //page content length: 30 // empty page
$pdf->AddPage(); //page content length: 30 // empty page
$pdf->Output();
?>

My blank-pages.pdf I have generated using FPDF with following code:

<?php
require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->AddPage();
$pdf->AddPage();
$pdf->Output();
?>

PHP create PDF invoice

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.

possible options to create pdf file using html elements to generate invoice in php and codeigniter

I would suggest using tcpdf which worked out great for me, here is an example where i used it:

    $query = "SELECT * FROM login WHERE type_login='customer'";
$result=mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$output .='
<tr>
<td>'.$row["id_login"].'</td>
<td>'.$row["name_login"].'</td>
<td>'.$row["user_email"].'</td>
</tr>
';
}

return $output;
}

if(isset($_POST["create_pdf"]))
{
require_once("tcpdf/tcpdf.php");
$obj_pdf = new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true,"UTF-8",false);
$obj_pdf->SetCreator(PDF_CREATOR);
$obj_pdf->SetTitle("Customer List");
$obj_pdf->SetHeaderData("","", PDF_HEADER_TITLE, PDF_HEADER_STRING);
$obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN,"",PDF_FONT_SIZE_MAIN));
$obj_pdf->SetFooterFont(Array(PDF_FONT_NAME_DATA,"",PDF_FONT_SIZE_DATA));
$obj_pdf->SetDefaultMonospacedFont('helvetica');
$obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$obj_pdf->SetMargins(PDF_MARGIN_LEFT,'5',PDF_MARGIN_RIGHT);
$obj_pdf->SetPrintHeader(false);
$obj_pdf->SetPrintFooter(false);
$obj_pdf->SetAutoPageBreak(TRUE,10);
$obj_pdf->SetFont('helvetica',"",12);
$obj_pdf->AddPage();

$content="";
$content.='
<h3 align="center"> Customer List </h3>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<th width=5%>customer ID</th>
<th width=10%>Customer Full name</th>
<th width=15%>Customer Email</th>
</tr>
';

$content .= fetch_data();
$content .= '</table>';
$obj_pdf->writeHTML($content);
$obj_pdf->Output("sample.pdf","I");



}
?>

<html>
<head>
<title>Customer List</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<br><br />
<div class="container" style="width:700px;">
<h3 align="center"> Customer List </h3>
<br />
<div class="table-responsive">
<table class="table table-bordered" border="1" cellpadding="4">
<tr>
<td width="5%"><strong>customer ID</strong></td>
<td width="10%"><strong>Customer Full name</strong></td>
<td width="15%"><strong>Customer Email</strong></td>
</tr>
<?php
echo fetch_data();
?>
</table>

or alternative if you dont want to use tcpdf:

    if (isset($_POST['txtNameSearch'])){
$search = $_POST['txtNameSearch'];
$connection = mysqli_connect('localhost', 'root', '', 'bookstore');
$query = "SELECT * FROM tblproduct right join order_details on tblproduct.prod_id=order_details.prod_id WHERE prod_no = $search ";
$result=mysqli_query($connection, $query);
while($row = mysqli_fetch_array($result))
{
$output .='
<tr>
<td>'.$row["prod_id"].'</td>
<td>'.$row["prod_name"].'</td>
<td>'.$row["order_id"].'</td>
<td>'.$row["quantity"].'</td>
</tr>
';
}

return $output;
}
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>HTML to PDF</title>
</head>
<body>
<form method="POST" action="index.php">
<input type="text" name="txtNameSearch" />
<input class="src_btn" type="submit" name="btnSearch" value="Search" />
</form>
<!--
content of this area will be the content of your PDF file
-->
<div id="HTMLtoPDF">

<table class="table table-bordered" border="1" cellpadding="4">
<tr>
<td width="25%"><strong>prod_id</strong></td>
<td width="25%"><strong>prod_name</strong></td>
<td width="25%"><strong>order_id</strong></td>
<td width="25%"><strong>quantity</strong></td>
</tr>

<?php
echo fetch_data();

?>

</div>

<!-- here we call the function that makes PDF -->
<a href="#" onclick="HTMLtoPDF()">Download PDF</a>
<a href="/DEVProject/admin/admin_addnew_user.php"> Back to Admin Panel </a>

If you want i can prepare a download link of the fully working code as well

Templates pdf for invoices with ReportLab

If you are familiar with HTML/CSS, you can first generate an HTML invoice using django's template, and then use the xhtml2pdf package to convert it into PDF format.

The xhtml2pdf is based on top of Reportlab, html5lib and pyPdf. I've used it before, and it solved my problem.

How can I create a well-formatted PDF?

For your requirement, XSL-FO might just do the trick. It is much cleaner to produce the pdf's directly from the data, then going the cumbersome html path, unless you need to display the html as well, then you might consider converting from html to pdf, but it will always be messy.

You can get xml results from mysql quite easily (mysql --xml) and then you write one (or several) xsl-fo stylesheet for the data. then, you cannot only produce pdfs, but also postscript files or rtf's with some processors.

XSL-FO has its limitations tho, but for your situation, it should suffice.

I admit, the learning curve can be steep, and maintaining xslt-stylesheets can get very tiring, but as you start knowing more about it, you end up writing less code.

another possibility is to do the whole thing in e.g. java or c# - send select statements and loop the results and iteratively build the pdf using a library like iText.

PDF Invoice Generation in Magento

Well I hope this helps someone else. Sometimes when you ask a detailed question, it gets you thinking more clearly. It was a very simple solution, with no clear error message indicating what the problem was!

Magento uses two folders for template files adminhtml and frontend. By default, there is only a .phtml for the payment data helper pdf action on the back-end. My script was running on the front-end, not finding this file, and outputting an empty string.

So in short, if your PDF invoices in Magento <= 1.6.1 are missing billing information when rendered on the front-end, take this file:

app/design/adminhtml/default/default/template/paygate/info/pdf.phtml

Then copy/paste it to:

app/design/frontend/base/default/template/paygate/info/pdf.phtml (Or if you prefer, your custom template directory)

In hindsight, I probably should be using the back-end layout for my cron scripts.

Edit: Today I learned about the var/log/system.log file in Magento... (Re-editing for clarity) The /var/log/system.log file was clearly telling me what the problem was, I just failed to read it.

Creating invoice pdf within ZUGFeRD xml embeded on MERN project

Since the xml is to comply with Zugferd/Factur-X then it is a very specialist embed to match the standards. Hevierweight Java or C# is required for speed, though you could command line from any script outside of restricted HTML.js

This is European Plutocracy at its finest, only those controlling money will pay each other. For a wider code orientated usage definition see https://duckduckgo.com/?q=EN+%2B16931

The key coding elements shown here. Are a strictly defined XML input (4 page on right in example) (could have been much simpler written as ODT) is processesed to generate a 100% synchronized colour PDF output (1 page in example on left) Which then in turn must include the 100% verifiable XML source and colour profile plus fonts!, all wrapped into one PDFA archive (could have been simpler if as just one Zipped.docX but was not to be!).

XML embedded in PDF for extract and process
Sample Image

You will need to ensure your XML matches the PDF data with respect to layout and that is entirely down to customer customization, so nobody here can guide on that step. However, it then (IMHO stupidly) requires a precise combination of colour profile and matching colour setting!, those can be auto templated for command line insertion, but you must get it right once, so its repeatable.

Thus you should be able to do it using Ghostscript and read carefully https:/ghostscript.com/blog/zugferd.html however Ghostscript should naturally be licensed by you for your commercial use.

For an alternative Java based Open Source up to date project see https://mustangproject.org/interface

It is not a lightweight read in either case, but for compatibly consider both tools and compare.



Related Topics



Leave a reply



Submit