This topic is locked

Create invoice with Setasign PDF (new version)

6/7/2023 12:17:02 PM
PHPRunner Tips and Tricks
fhumanes author

img alt
The previous version has been quite successful, but it was a fairly limited example and I've had a lot of questions about everything, limiting the invoice to a single page.
In this new example I try to make it much more functional, I resolve that it is only one page and I make everything much easier to use.
Objetive
Make a PDF invoice (report) with the highest quality PDF document, using a template and very little PHP code to make the report.
Use of the SETASIGN FPDI library , which is 100% PHP and its execution is very fast.
DEMO: https://fhumanes.com/invoice_pdf
img alt
If you are interested in this topic, continue reading the article at this link.

fhumanes author 6/7/2023

Technical Solution.
For the elaboration of the PDF template I have used Excel (I provide an example).
To measure the points where the PDF document should be filled, I have used the program PDFill (Editor basic) .
To use this product I have configured it to measure distances by "points".
img alt
The measures of the points are obtained:
img alt
(1).- The option to add text to the PDF document is selected.
(2).- Position the mouse (without clicking) at the point where we want to set the variable content.
(3).- Reports the size of the page.
(4).- Reports the coordinates of the point that has been marked with the mouse and values ​​that we have to transfer to our program.
To simplify printing I have created a function that, depending on the type of data, makes the "print" on the PDF page.
PDF_functions.php:
`<?php
/*
Param:

Type = char | number | integer | date | dateTime | time | dateLong | boolean | memo
NumberDecimal = 0
Align = L left | R right | C center
FontStyle = 'B' | 'I' | 'U' | ''
Font = 'helvetica'
FontSize = 10
ColorRGB = "0,0,0"
RightMargin = 0 Especial fields MEMO

/
function print_pdf($point_x = 1, $point_y = 1, $value = 0, $type = 'char', $numberDecimal = 0 ,
$align = 'L', $fontStyle = '', $font = 'helvetica', $fontSize = 11, $colorRGB = "0,0,0", $rightMargin = 0 )
{
$pdf = $GLOBALS['pdf'];
$coef_x = $GLOBALS['coef_x'];
$coef_y = $GLOBALS['coef_y'];
$wPt = $GLOBALS['wPt'];
/

$numeric_symbol_of_thousands = $GLOBALS['numeric_symbol_of_thousands'];
$numeric_decimal_symbol = $GLOBALS['numeric_decimal_symbol'];
$date_format = $GLOBALS['date_format'];
$date_and_time_format = $GLOBALS['date_and_time_format'];
$time_format = $GLOBALS['time_format'];
$long_Date_Format = $GLOBALS['long_Date_Format'];
*/

date_default_timezone_set( $GLOBALS['date_default_timezone_set'] );
setlocale(LC_TIME, $GLOBALS['setlocale_LC_TIME']);

if ( $align == 'R' ) {
if ($type == 'number' || $type == 'integer') {
$rightMargin = $wPt - $point_x ; // Obtain the distance from the right margin from the osition of the field.
}
}

$pdf->SetXY($point_x/$coef_x, $point_y/$coef_y); // Positioning on the page
$pdf->SetMargins($point_x/$coef_x,5,$rightMargin/$coef_x); // For fields MEMO
$pdf->SetFont($font,$fontStyle,$fontSize); // Font, type and size
$FontColor = explode(",", $colorRGB);
$pdf->SetTextColor($FontColor[0],$FontColor[1],$FontColor[2]); // Color in R, G, B

switch ($type) {
case 'char': // Char
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'number': // Num
$value = number_format($value, $numberDecimal ,$GLOBALS['numeric_decimal_symbol'] ,$GLOBALS['numeric_symbol_of_thousands']);
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'integer': // Integer
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'date': // Date
$date = new DateTimeImmutable($value);
$value = $date->format($GLOBALS['date_format']);
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'time': // Time
$date = new DateTimeImmutable($value);
$value = $date->format($GLOBALS['time_format']);
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'dateTime': // DateTime
$date = new DateTimeImmutable($value);
$value = $date->format($GLOBALS['date_and_time_format']);
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'dateLong': // Date Long
$date = new DateTimeImmutable($value);
$value = $date->format($GLOBALS['long_Date_Format']);
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'boolean': // Boolean
$pdf->SetFont('ZapfDingbats',$fontStyle,$fontSize); // Font, type and size
if ($value == 1) {$value = '4';} else {$value = '';} // 'l' Punto negro
$pdf->Cell(0,0,$value,0,1,$align);
break;
case 'memo': // Memo
$pdf->Write(5, $value);
break;
default:
$pdf->Cell(0,0,$value,0,1,$align);
}
}I have tried to document the code to explain what each point is, however some detail may not be sufficiently explained. Tell me so I can improve the documentation and explain it to you. I leave you all the sources of the project so that you can install it on your PC and do all the tests that you require. For any questions or what you need, tell me through my email [fernandohumanes@gmail.com](mailto:fernandohumanes@gmail.com) . If instead of an invoice it was a report, I think this example could help you. Remember that in[ guide 18 ](<a href='https://fhumanes.com/blog/guias-desarrollo/guia-18-phprunner-merge-de-ficheros-pdfs/'>https://fhumanes.com/blog/guias-desarrollo/guia-18-phprunner-merge-de-ficheros-pdfs/</a>;)I explain how to mix PDF files`

fhumanes author 6/14/2023

I have made the same reports that I did for Excel and Word , with this solution and it seemed to me a simple and quite fast way, so I advise you to see the result and if you like it, try to review the code.
The graphs in this example have been made with jpGraph , a very powerful solution.
DEMO: https://fhumanes.com/reports_setasign/ (reports made with this solution)
img alt
If you are interested in this topic, keep reading the article at this link.