<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rubens Gadelha &#187; Helper</title>
	<atom:link href="http://www.rubensgadelha.com.br/tag/helper/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rubensgadelha.com.br</link>
	<description>Tecnologia da Informação e Atualidades</description>
	<lastBuildDate>Mon, 22 Feb 2010 15:38:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Convertendo HTML em PDF no Zend Framework utilizando html2fpdf</title>
		<link>http://www.rubensgadelha.com.br/2009/05/26/convertendo-html-em-pdf-no-zend-framework-utilizando-html2fpdf/</link>
		<comments>http://www.rubensgadelha.com.br/2009/05/26/convertendo-html-em-pdf-no-zend-framework-utilizando-html2fpdf/#comments</comments>
		<pubDate>Tue, 26 May 2009 20:42:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Zend]]></category>
		<category><![CDATA[Helper]]></category>
		<category><![CDATA[Pdf]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.rubensgadelha.net/?p=168</guid>
		<description><![CDATA[Olá pessoal,
Após um bom tempo sem artigos, estou de volta!   
A tarefa de criar documentos PDF sempre foi uma &#8220;pedra no sapato&#8221; de qualquer programador PHP, haja vista que era um processo trabalhoso, que exige testes e mais testes de posicionamento e ainda sim não se chegava a um resultado satisfatório.
Para sanar esse [...]]]></description>
			<content:encoded><![CDATA[<p>Olá pessoal,</p>
<p>Após um bom tempo sem artigos, estou de volta!  <img src='http://www.rubensgadelha.com.br/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>A tarefa de criar documentos PDF sempre foi uma &#8220;pedra no sapato&#8221; de qualquer programador PHP, haja vista que era um processo trabalhoso, que exige testes e mais testes de posicionamento e ainda sim não se chegava a um resultado satisfatório.</p>
<p>Para sanar esse problema, foi criada uma classe chamada <a href="www.fpdf.org">fpdf,</a> que por si só já nos era muito útil com seus métodos para estilização de texto e alguns outros recursos. Mas ainda não era o suficiente, pois a formatação e quebra de linhas ainda eram difíceis.</p>
<p>Surgiu então a classe <a href="http://html2fpdf.sourceforge.net/">html2fpdf</a> que, na minha opinião, resolveu o problema, pois agora poderíamos estilizar um código html (com algumas limitações, é claro) e gerar o PDF com base nele.</p>
<p>Como atualmente utilizo Zend Framework, resolvi reunir as vantagens das classes acima em um Action Helper:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Action Helper para gerar PDF a partir de HTML usando HTML2PDF
 * 
 * @uses Zend_Controller_Action_Helper_Abstract
 * @uses HTML2FPDF (http://html2fpdf.sourceforge.net)
 * @uses FPDF (www.fpdf.org)
 *
 * @author Rubens Gadelha
 *
 * @param string $html
 * @param 'I'|'D'|'F'|'S' $dest
 *
 * Saidas:
 * I: Envia para a saída padrão
 * D: Download do arquivo
 * F: Salva em um arquivo local
 * S: Retorna como string
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Zend_Controller_Action_Helper_Html2Pdf <span style="color: #000000; font-weight: bold;">extends</span> Zend_Controller_Action_Helper_Abstract
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> direct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #339933;">,</span> <span style="color: #000088;">$output</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'D'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$pdf</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HTML2FPDF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">AddPage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">WriteHTML</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">utf8_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$pdf</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Output</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$output</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dest</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Para utilizá-lo, é muito fácil:</p>
<p><strong>1.</strong> Salve o código acima em um arquivo chamado <strong>Html2Pdf.php</strong> na pasta <strong>helpers</strong> do seu projeto;</p>
<p><strong>2.</strong> Faça o download do html2fpdf <a href="http://sourceforge.net/projects/html2fpdf">aqui</a>;</p>
<p><strong>3.</strong> Descompacte-o dentro da pasta <em>library</em> do seu projeto;</p>
<p><strong>4.</strong> Inclua essa pasta no <em>include_path</em> que, geralmente está configurado no arquivo de <em>bootstrap</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.'</span> <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'../library/'</span>
                     <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'../application/default/models/'</span> 
                     <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                     <span style="color: #666666; font-style: italic;">// inclua a linha abaixo</span>
                     <span style="color: #339933;">.</span> PATH_SEPARATOR <span style="color: #339933;">.</span> <span style="color: #0000ff;">'../library/html2fpdf/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>5.</strong> No <em>Controller</em>, insira a linha abaixo na ação onde você deseja gerar o PDF:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Desabilita o layout</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">layout</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">disableLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Gera o PDF</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_helper<span style="color: #339933;">-&gt;</span><span style="color: #004000;">html2Pdf</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$html</span><span style="color: #339933;">,</span> <span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'D'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #666666; font-style: italic;">// Evita que o arquivo busque a respectiva view</span>
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<blockquote><p>
Onde:<br />
<strong>$html</strong> é o código base para geração do arquivo<br />
<strong>$file</strong> é o nome do arquivo<br />
<strong>&#8216;D&#8217;</strong> é um parâmetro que força o download do arquivo.</p>
<p>Os parâmetros possíveis são:<br />
<strong>I</strong>: Envia para a saída padrão<br />
<strong>D</strong>: Download do arquivo<br />
<strong>F</strong>: Salva em um arquivo local<br />
<strong>S</strong>: Retorna como string
</p></blockquote>
<p>Para mais detalhes sobre código html compatível, acesse a página do <a href="http://html2fpdf.sourceforge.net/">html2fpdf</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rubensgadelha.com.br/2009/05/26/convertendo-html-em-pdf-no-zend-framework-utilizando-html2fpdf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- www.000webhost.com Analytics Code -->
<script type="text/javascript" src="http://analytics.hosting24.com/count.php"></script>
<noscript><a href="http://www.hosting24.com/"><img src="http://analytics.hosting24.com/count.php" alt="web hosting" /></a></noscript>
<!-- End Of Analytics Code -->
