melhor forma para gerar pdf's em php

zoidberg

Folding Member
boas,

Ando aqui a tentar usar várias classes para gerar pdf's em php , mas não encontro nenhuma verdadeiramente intuitiva, facil e que funcione.

Basicamente tenho um relatorio com 4 páginas gerado em php com formatação em html e queria transformar em pdf automáticamente, contudo tudo o que tentei simplesmente não faz o parse do html correctamente ...

O que aconselham?
 
esse ainda não, mas não queria instalar 10.000 coisas no servidor para fazer isto :s

Requirements
------------

* PHP: Version 4.0.4 or greater (http://www.php.net).

* html2ps: Does the initial conversion of html to a postscript file, and
thus is the most crucial part of the conversion. More information
about it is here: http://www.tdb.uu.se/~jan/html2ps.html You will
especially want to read the user's guide here:
http://www.tdb.uu.se/~jan/html2psug.html if this script is not making
the pdf look like you want.

* ps2pdf: This comes with the Ghostscript package, and can be found
here: http://www.cs.wisc.edu/~ghost/ This package is normally
installed as an RPM on RedHat systems, so if you're using that OS you
shouldn't have to worry. There is a windows install for this as well.

* curl: Or some program that grabs documents off the web (lynx, w3m,
etc.). HOWEVER, for whatever reason only curl has worked in my tests
when the script page is running as a web script and not just a script
from the command line. Curl can be found here: http://curl.haxx.se/

* Valid HTML: Good HTML (XHTML is best) definitely helps.

* PDFEncryptor needs java (http://java.sun.com) and the iText jar file
(http://itext.sourceforge.net/downloads/)

Included Files
--------------

The following files are available in the HTML_ToPDF distribution:

README - This file
CHANGES - The list of changes
examples/ - Several example files to aid you on your way
docs/ - Class documentation (in HTML)
lib/ - Where some of the encryption libraries live

Common Problems
---------------

* How do I make this stupid thing work under windows?!
It took me many headaches to figure out, but here is what I came up with, YMMV:
- Make sure you install the windows version of all the programs above
(ghostscript, ImageMagick, curl, etc.)

- html2ps requires a bit more work to get working than the other programs.
Follow these steps to get it working:
* Install ActivePerl (http://www.activestate.com/)
* Download html2ps from http://user.it.uu.se/~jan/html2ps.html
* Extract all the files to c:\html2ps
* Create a file named html2psrc in c:\html2ps with this in it:
@html2ps {
package {
ImageMagick: 1;
}
hyphenation {
en {
file: "c:\html2ps\hyphen.tex";
}
}
}
* Open up the html2ps file in c:\html2ps folder with a decent text
editor (i.e. VIM, not notepad!) and make the following changes:

Change line 22 (or thereabouts) to:
$globrc='c:\html2ps\html2psrc';

After this line (around 497):
($scr=$tmpname)=~/\w+$/;
Add this:
$scr='c:\html2ps\scratch';

Change line 3582 (or thereabouts) from:
$_[1]=`$geturl '$url'`;
to:
$_[1]=`$geturl $url`;

* If it is having trouble converting pngs try adding this line to your
conversion script:
$pdf->addHtml2PsSettings('package { libwww-perl: 1; }');

- If you use IIS, make sure you give the IUSR_xxxx user read, write (this
is important), and exec permissions to the cmd.exe program in the
system32 folder in your Windows system directory.

- Put the following in your script (modify the paths to fit your
installation, though) after instantiating the $pdf variable:
$pdf->setHtml2Ps('perl c:\html2ps\html2ps');
$pdf->setPs2Pdf('c:\Program Files\gs\gs8.52\bin\gswin32c.exe');
$pdf->setTmpDir('\temp');
$pdf->setGetUrl('c:\PHP\curl.exe -i -s');

- You won't get any error output, so if it's not working, run each command
(you can see this by turning debugging on -- $pdf->setDebug(true) ) from
the command prompt to find out the problem.

* No files are created.
- Likely the problem is due to a lack of permissions. Either the tmp
directory that HTML_ToPDF uses for intermediary files is not writable by
the webserver or the output directory for the PDF file is not writable by
the webserver. Possible fixes are:
// Change the tmp directory
$pdf->setTmpDir('/somewhere/writable');
// Change the directory permissions
bash# chmod 777 www/pdfs; chown apache www/pdfs

* The images don't show up.
- Often the program getting the images (i.e. curl) is having a problem.
These errors can usually be seen if the program is run from the command
line.

* ps2pdf fails with error code 127
- The problem is that the ps2pdf shell script does not have the path to the
'gs' executable. Look in the ps2pdf and see where it points to (usually
it executes ps2pdf12 which in turn executes ps2pdfwr). Once your in the
file that actually does something, you will see this line at the bottom
of the file:
exec gs $OPTIONS...
Change that to:
exec /path/to/gs $OPTIONS...
 
Sugeria entao, usar a biblioteca fpdf, na pagina dos scripts têm montes de exemplos, mesmo para quando o pdf é mais "esquesito"...mas provavelmente já passaste por lá.
 
já passei por lá, mas o parse é horrivel, fica tudo desformatado ( as tabelas e imagens ).

Estavaa tentar instalar o dompdf, só que não consigo por isto a funcionar em windows com o wamp :s
 
Boas,

Se és tu quem vai criar o relatório, acho mais fácil para ti construires tu próprio através de uma livraria e não injectares o html para depois a classe fazer a conversão.

Fazes manualmente utilizando a Fpdf ou a DOMPdf a Zend Framework (componente Zend_Pdf), ou outro qualquer que exista.

É difícil encontrar uma livraria que te faça isso automaticamente a 100% com a injecção de HTML.


Saudações.
 
é que já tive muito trabalho a gerar o relatório exactamente com o template que está, não queria muito partir a cabeça novamente para colocar as coisas nos sitios no pdf :(
( tenho tabelas a ser geradas em ciclos com informação da base de dados etc etc)

qualquer das formas vou testar o TCPDF agora, já digo se correu bem

EDIT: bom, não correu nada de jeito, parece potente, mas o parse é zero :(

não queria mesmo nada ter de criar tudo de raiz :S
 
Em Java para templates existe o Jasper e o Birt, além do Crystal Reports (que é pago) e funcionam optimamente.

Mas o que parece que o zoidberg quer é algo semelhante à biblioteca de iText de Java que tem métodos que fazem o Html2Pdf através do parse de HTML e são bastante competentes uma vez que identificam a grande maioria de elementos. Mas infelizmente em PHP não conheço nada semelhante...
 
Obrigado a todos pelas dicas, mas acabei por usar o TCPDF :)

Tive de refazer grande parte do template que já tinha em html, mas como o parse do tcpdf é muito bom e é gratuito, já consegui fazer o que queria ( levei o dia todo mas já está como quero :) )

por acaso valeu apena, não é necessário instalar nenhuma extensão para o php, nem usar java nem nada! basta fazer o include dos php's e está a andar :)

recomendo para quem queira gerar pdf's em php gratuitamente sem ter grandes chatisses!
 
Última edição:
Back
Topo