The best IT partner for your business English version

Framework docs

Presentation > Reports > PDF Report

More productivity with Adianti Studio Pro

  • Forms designer;
  • Automatic form generation;
  • Automatic datagrid generation;
  • Create DB from your UML;
Ver detalhes...

This sample demonstrates the usage of a report created in Adianti Studio PDF Designer. The goal of this sample is to demonstrate how to create a report with a table, supported by the Adianti Studio PDF Design tool. In the Adianti Studio PDF designer, we have created only the header and the column titles. The rest of the report data is dynamically shown.

When the report is generated (onGenerate()), we collect the objects of Customer from database. The TPDFDesigner class is used to load the pdf design through fromXml() method. The document is created in memory by generate(). After this, we use gotoAnchorXY() to change the position at the document, and methods of FPDF like Cell() to write content from database in the document. At the end, we use the save() method to write the document in the filesystem.

Important: Adianti PDF Designer is a tool that allows you to draw reports and documents that will be later generated in PDF. This "design" can be manipulated and completed with dynamic data from your database through your PHP code, using the methods from the traditional FPDF library.


Click here for
live demonstration

Example

  1. <?php
  2. /**
  3.  * PDF Designed Customer report
  4.  *
  5.  * @version    1.0
  6.  * @package    samples
  7.  * @subpackage tutor
  8.  * @author     Pablo Dall'Oglio
  9.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10.  * @license    http://www.adianti.com.br/framework-license
  11.  */
  12. class PDFDesignReportView extends TPage
  13. {
  14.     private $form// form
  15.     
  16.     /**
  17.      * Class constructor
  18.      */
  19.     function __construct()
  20.     {
  21.         parent::__construct();
  22.         
  23.         // creates the form and a inner table
  24.         $this->form = new TForm('form_pdf_report');
  25.         $table = new TTable;
  26.         $this->form->add($table);
  27.         // creates an action button
  28.         $save_button=new TButton('generate');
  29.         $save_button->setAction(new TAction(array($this'onGenerate')), 'Generate');
  30.         $save_button->setImage('ico_save.png');
  31.         // add a row for the form action
  32.         $table->addRowSet($save_button);
  33.         // define wich are the form fields
  34.         $this->form->setFields(array($save_button));
  35.         
  36.         // wrap the page content using vertical box
  37.         $vbox = new TVBox;
  38.         $vbox->add(new TXMLBreadCrumb('menu.xml'__CLASS__));
  39.         $vbox->add($this->form);
  40.         parent::add($vbox);
  41.     }
  42.     /**
  43.      * method onGenerate()
  44.      * Executed whenever the user clicks at the generate button
  45.      */
  46.     function onGenerate()
  47.     {
  48.         try
  49.         {
  50.             // open a transaction with database 'samples'
  51.             TTransaction::open('samples');
  52.             
  53.             // load all customers
  54.             $repository = new TRepository('Customer');
  55.             $criteria   = new TCriteria;
  56.             $customers $repository->load($criteria);
  57.             
  58.             $data $this->form->getData();
  59.             $this->form->validate();
  60.             
  61.             $designer = new TPDFDesigner;
  62.             $designer->fromXml('app/reports/report.pdf.xml');
  63.             $designer->generate();
  64.             
  65.             $fill TRUE;
  66.             $designer->gotoAnchorXY('details');
  67.             $designer->SetFont('Arial'''10);
  68.             $designer->setFillColorRGB'#F9F9FF' );
  69.             
  70.             if ($customers)
  71.             {
  72.                 foreach ($customers as $customer)
  73.                 {
  74.                     $designer->gotoAnchorX('details');
  75.                     $designer->Cell3412$customer->id10'C'$fill);
  76.                     $designer->Cell(16012utf8_decode($customer->name), 10'L'$fill);
  77.                     $designer->Cell(15212utf8_decode($customer->address), 10'L'$fill);
  78.                     $designer->Cell(15212utf8_decode($customer->city_name), 10'L'$fill);
  79.                     $designer->Ln(12);
  80.                     
  81.                     // grid background
  82.                     $fill = !$fill;
  83.                 }
  84.             }
  85.             
  86.             $file 'app/output/pdf_report.pdf';
  87.             
  88.             if (!file_exists($file) OR is_writable($file))
  89.             {
  90.                 $designer->save($file);
  91.                 parent::openFile($file);
  92.             }
  93.             else
  94.             {
  95.                 throw new Exception(_t('Permission denied') . ': ' $file);
  96.             }
  97.             
  98.             new TMessage('info''Report generated. Please, enable popups in the browser (just in the web).');
  99.             
  100.             // close the transaction
  101.             TTransaction::close();
  102.         }
  103.         catch (Exception $e// in case of exception
  104.         {
  105.             new TMessage('error''<b>Error</b> ' $e->getMessage());
  106.             TTransaction::rollback();
  107.         }
  108.     }
  109. }
  110. ?>