Symfony 1.4 with TCPDF:如何从数据库中检索数据并将其显示为 .pdf 文件? [英] Symfony 1.4 with TCPDF: How to retrieve data from a database and show it as a .pdf file?
问题描述
我使用 Symfony 1.4.我想使用 TCPDF 创建一个 pdf 文件,其中包含涉及多个表的查询内容.这是我在 actions.class.php 中的代码:
公共函数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 DESC')->执行();$config = sfTCPDPluginConfigHandler::loadConfig();//pdf 对象$pdf = 新的 sfTCPD();//设置文档信息$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('J.H.');$pdf->SetTitle('TCPD Example 001');$pdf->SetSubject('TCPD 教程');$pdf->SetKeywords('TCPD, PDF, example, test, guide');//设置默认标题数据$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.'001', PDF_HEADER_STRING);//设置页眉和页脚字体$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));//设置默认的等宽字体$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);//设置边距$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);//设置自动分页符$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);//设置图像比例因子$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);//---------------------------------------------------------//设置默认字体子集模式$pdf->setFontSubsetting(true);//设置字体//dejavusans 是一种 UTF-8 Unicode 字体,如果你只需要//打印标准 ASCII 字符,您可以使用核心字体,例如//helvetica 或减少文件大小的次数.$pdf->SetFont('dejavusans', '', 10);//添加一个页面//此方法有多个选项,查看源代码文档以获取更多信息.$pdf->AddPage();$pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);$pdf->SetFont('helvetica', '', 6);//设置一些要打印的内容$html = $this->getPartial('generar_pdf',//在这个数组中放入所有你想赋予部分的变量数组('posts' => $posts));$pdf->writeHTML($html, true, false, false, false, '');//---------------------------------------------------------//关闭并输出PDF文档//此方法有多个选项,查看源代码文档以获取更多信息.$pdf->Output('example_conflictos.pdf', 'I');//停止 symfony 进程抛出新的 sfStopException();}
这里是我的模板代码(名为 _generar_pdf.php 的部分).我遵循了这里的建议: 这里:
使用带有 TCPDF 的 PHP 从数据库中检索数据(使用 mysql)并将其显示为 .pdf 文件
在此模板中,我们生成视图并捕获浏览器写入"的所有内容.我现在添加一个链接来生成 PDF <a href="<?php echo url_for('conflictos/testpdf') ?>">Generar Pdf</a>
.它通过 Symfony 的用户类将变量 $posts
的内容传递给新操作 testpdf
:
setAttribute('para_pdf', $posts);?>
它需要一个名为 testpdf
的新操作.在新动作executeTestpdf
中,注意下面这行代码,它检索变量$posts
的值:
$ html = $ this->getUser() ->getAttribute('para_pdf');
公共函数executeTestpdf(){$config = sfTCPDPluginConfigHandler::loadConfig();//pdf 对象$pdf = 新的 sfTCPD();//设置文档信息$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('J.H.');$pdf->SetTitle('TCPD Example 001');$pdf->SetSubject('TCPD 教程');$pdf->SetKeywords('TCPD, PDF, example, test, guide');//设置默认头数据$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.'001', PDF_HEADER_STRING);//设置页眉和页脚字体$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));//设置默认的等宽字体$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);//设置边距$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);//设置自动分页符$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);//设置图像比例因子$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);//---------------------------------------------------------$pdf->setPageOrientation("L");//设置默认字体子集模式$pdf->setFontSubsetting(true);//设置字体$pdf->SetFont('helvetica', '', 6);//添加一个页面//此方法有多个选项,查看源代码文档以获取更多信息.$pdf->AddPage();$pdf->Write(0, '', '', 0, 'L', true, 0, false, false, 0);//设置一些要打印的内容$html = $this->getUser()->getAttribute('para_pdf');$pdf->writeHTML($html, true, false, false, false, '');//---------------------------------------------------------//关闭并输出PDF文档//此方法有多个选项,查看源代码文档以获取更多信息.$pdf->Output('example_conflictos.pdf', 'I');//停止 symfony 进程抛出新的 sfStopException();}
然后,我终于生成了正确显示所有数据的 PDF!
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:
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 DESC')
->execute();
$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);
// ---------------------------------------------------------
// set default font subsetting mode
$pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 10);
// 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);
$pdf->SetFont('helvetica', '', 6);
// Set some content to print
$html = $this->getPartial(
'generar_pdf',
// put in this array all variables you want to give to the partial
array('posts' => $posts)
);
$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();
}
Here my template code (a partial called _generar_pdf.php). I have followed the advice here: Using PHP with TCPDF to retrieve data(using mysql) from a database and show it as a .pdf file
<?php ob_start(); ?>
<h2>Listado de Conflictos</h2>
<table cellspacing="1" border="1">
<thead>
<tr>
<th>Id</th>
<th>Ver acciones<br>conflictivas</th>
<th>Ver actores</th>
<th>Fecha comienzo</th>
<th>Relacion conflictual principal</th>
<th>Sector actividad</th>
<th>Subsector actividad</th>
<th>Demandas</th>
<th>Descripcion-general</th>
<th>Descripcion protagonista</th>
<th>Descripcion antagonista</th>
<th>Descripcion demandaprinc</th>
<th>Nivel estado</th>
<th>Descripcion sector</th>
<th>Fecha final</th>
</tr>
</thead>
<tbody>
<?php foreach ($conflictos1s as $conflictos1): ?>
<tr>
<td><?php echo $conflictos1->getId() ?></a></td>
<td><?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>
<td><?php if(!is_null($conflictos1->getFechaFinal())){ echo date('d/m/Y', strtotime($conflictos1->getFechaFinal()));} ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php $posts = ob_get_contents();
ob_end_clean();
echo $posts ?>
Here my result: I can not see the variable data. What am I doing wrong?
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!.
这篇关于Symfony 1.4 with TCPDF:如何从数据库中检索数据并将其显示为 .pdf 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!