I work with Symfony 1.4. I want to create a pdf file using TCPDF with the content of a query involving multiple tables. Here my code in the actions.class.php:
Here the solution I encountered: I've separated the problem in two. First, I built an actions with the query: the actions have called executeTest
(see the code below)
public function executeTest()
{
$this->conflictos1s = Doctrine_Core::getTable('Conflictos1')
->createQuery('a')
->leftJoin('a.SectorActividadCiuTa7')
->leftJoin('a.RelacionConflictualPrincipalTa9')
->leftJoin('a.SubsectorActividadTa8')
->leftJoin('a.DemandasTa2')
->leftJoin('a.ActoresTa1')
->leftJoin('a.Conflictos1HasActoresTa1')
->orderBy('a.Id')
->execute();
}
Then I built a template named testSuccess.php
. So far nothing new in Symfony 1.4. But in this template, I added the following lines of code. At first:
<?php ob_start(); ?>
Last of all, in the template I wrote:
<?php $posts = ob_get_contents(); ?>
<?php ob_end_clean(); ?>
The view content is recorded in the variable $posts
//testSuccess.php
<?php ob_start(); ?>
<h2>Listado de Conflictos</h2>
<table cellspacing="1" border="1">
<thead>
<tr>
<th width="2%">Id</th>
<th width="6%">Fecha comienzo</th>
<th>Relacion conflictual principal</th>
<th>Sector actividad</th>
<th>Subsector actividad</th>
<th>Demandas</th>
<th width="20%">Descripcion-general</th>
<th>Descripcion protagonista</th>
<th>Descripcion antagonista</th>
<th>Descripcion demandaprinc</th>
<th>Nivel estado</th>
<th>Descripcion sector</th>
</tr>
</thead>
<tbody>
<?php foreach ($conflictos1s as $conflictos1): ?>
<tr>
<td width="2%"><?php echo $conflictos1->getId() ?></td>
<td width="6%"><?php echo date('d/m/Y', strtotime($conflictos1->getFechaComienzo())) ?></td>
<td><?php echo $conflictos1->getRelacionConflictualPrincipalTa9() ?></td>
<td><?php echo $conflictos1->getSectorActividadCiuTa7() ?></td>
<td><?php echo $conflictos1->getSubsectorActividadTa8() ?></td>
<td><?php echo $conflictos1->getDemandasTa2() ?></td>
<td width="20%"><?php echo substr($conflictos1->getDescripcionGeneral(),0,60).'...' ?></td>
<td><?php echo $conflictos1->getDescripcionProtagonista() ?></td>
<td><?php echo $conflictos1->getDescripcionAntagonista() ?></td>
<td><?php echo $conflictos1->getDescripcionDemandaprinc() ?></td>
<td><?php echo $conflictos1->getNivelEstado() ?></td>
<td><?php echo $conflictos1->getDescripcionSector() ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php $posts = ob_get_contents();
ob_end_clean(); ?>
<?php $sf_user->setAttribute('para_pdf', $posts); ?>
<a href="<?php echo url_for('conflictos/testpdf') ?>">Generar Pdf</a>
This idea was taken from the answer by https://stackoverflow.com/users/1029908/alexey-gerasimov here:
Using PHP with TCPDF to retrieve data(using mysql) from a database and show it as a .pdf file
In this template we generate the view and capture everything the browser "write". I add a link to generate now the PDF <a href="<?php echo url_for('conflictos/testpdf') ?>">Generar Pdf</a>
. It passes the contents of the variable $posts
to new actions testpdf
, through Symfony's user class:
<?php $sf_user->setAttribute('para_pdf', $posts); ?>
It call for a new actions named testpdf
. In the new action executeTestpdf
, pay attention to the following line of code, which retrieve the value of the variable $posts
:
$ html = $ this-> getUser () -> getAttribute ('para_pdf');
public function executeTestpdf()
{
$config = sfTCPDFPluginConfigHandler::loadConfig();
// pdf object
$pdf = new sfTCPDF();
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('J. H.');
$pdf->SetTitle('TCPDF Example 001');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// ---------------------------------------------------------
$pdf->setPageOrientation("L");
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
$pdf->SetFont('helvetica', '', 6);
// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();
$pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);
// Set some content to print
$html = $this->getUser()->getAttribute('para_pdf');
$pdf->writeHTML($html, true, false, false, false, '');
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_conflictos.pdf', 'I');
// Stop symfony process
throw new sfStopException();
}
Then, finally I have the PDF generated with all data properly shown!.