This topic is locked

Guide 41 – Print Product Labels

7/19/2022 11:05:57 AM
PHPRunner Tips and Tricks
fhumanes author

img alt
Generating PDF's in PHPRunner, in the latest versions, is quite simple, but being able to make adjustments to the generation of these PDF documents is very complex or impossible, with the standard PHPRunner solution.

For the latest versions of PHPRunner, where the recommendation is to use the PHP 8.X version, I have told you that I have used the PhpJasperXml 2.0.1 solution , obtaining very, very good results.

To give you a simple example of using PhpJasperXml 2.0.1 , I have created this guide so you can evaluate how simple it is to integrate it into PHPRunner.

Goal

Generate labels to print on a Zebra printer (label printers), selecting records from a list. With small variations it can be used in standard laser printers with label sheets (which I understand is the best solution for many cases)

Technical Solution

As I have indicated, the example contemplates the use of the PhpJasperXML 2.0.1 library , which is an efficient solution for making PDF reports of any type, which have previously been designed with Jasper Studio .

The functionality is very simple, from the list of products, we select the records for which we want to generate the labels and press the orange print button.

img alt
The labels have the following characteristics:

  • Has a special page size
  • A QR code is generated with the information of a field or with the desired content
  • In a “memorandum” type field, the information is extended to fit the indicated space.

img alt

The code to generate the labels is:

<?php
// Required to take the phprunner context
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");
require_once(__DIR__."/../include/dbcommon.php");

require __DIR__."/../../ComponentCode/phpJasperXML_2.0.1/autoload.php";

use simitsdk\phpjasperxml\PHPJasperXML;
$filename = __DIR__.'/labels.jrxml';

// read parameters
$P1 = '';
$parameters = $_SESSION['record_select'];
for($i = 0; $i < count($parameters); $i++) {
$P1 .= $parameters[$i].',';
}
$P1 = substr($P1, 0, -1); // Delete last caracter ','
// Read record
$dat = array();
$rs = DB::Query("SELECT * FROM pdf_labels WHERE id_PDF_labels in ($P1)");
while( $row= $rs->fetchAssoc() )
{
$data[] = $row;
}

$config = [
'driver'=>'array',
'data'=> $data
];

/*
$config = [
'driver'=>'mysql',
'host'=>'127.0.0.1',
'user'=>'root',
'pass'=>'humanes',
'name'=>'pruebas',
];
*/
$parameters = [];
/*
$parameters = [
'P1'=> 1
];
*/

$type = 'Pdf';
$fileOutput= '';
// $fileOutput= __DIR__.'/<name of file>';
$creator = 'FHumanes';
$author = 'fernandohumanes@gmail.com';
$title = 'Product labels';
$keywords = 'PHPRunner';
$format_intl = 'es_ES';

$report = new PHPJasperXML();
$report->load_xml_file($filename)
->setParameter($parameters)
->setDataSource($config)
->export($type,$fileOutput,$title,$creator,$author,$keywords,$format_intl);
// ->export('Pdf');

Here are some features of this code:

  • The library to generate the report is outside the project, specifically in the "ComponentCode" directory, so that the project is not very heavy and because it can be used by multiple projects.
  • The records selected from the LIST page come to us in a session variable in an Array. It is passed to “string” format as a list of records.
  • The PHPRunner connection is used to access the data and pass it to an array, which is what is delivered to the utility to generate the report.
  • The generated PDF is delivered to the browser, but can also be saved to a file on the server.
  • The attributes of the generated PDF file are indicated.
  • The language is specified. “es_ES” in this case, which determines the symbols of the numerical values.

At this time I cannot provide a test URL, since it requires PHP 8, my "hosting" is in PHP 7.4, so that many of the examples that I have from versions prior to PHPRunner 10.7 work. Soon, I will generate all the examples so that they work in PHP 8 and thus be able to leave an example of everything explained in my portal.

I leave you all the code so that you can install it on your PC's and you can make new adjustments and tests.

For any questions or what you need, contact me through my email fernandohumanes@gmail.com