-
-
Notifications
You must be signed in to change notification settings - Fork 2
-
FPDF is released under a permissive license: there is no usage restriction. You may embed it freely in your application (commercial or not), with or without modifications.
-
These "weird" characters are in fact the actual content of your PDF. This behavior is a bug of IE6. When it first receives an HTML page, then a PDF from the same URL, it displays it directly without launching Acrobat. This happens frequently during the development stage: on the least script error, an HTML page is sent, and after correction, the PDF arrives.
To solve the problem, simply quit and restart IE. You can also go to another URL and come back.
To avoid this kind of inconvenience during the development, you can generate the PDF directly to a file and open it through the explorer.
-
First of all, check that you send nothing to the browser after the PDF (not even a space or a carriage return). You can put an exit statement just after the call to the Output() method to be sure. If it still doesn't work, it means you're a victim of the "blank page syndrome". IE used in conjunction with the Acrobat plug-in suffers from many bugs. To avoid these problems in a reliable manner, two main techniques exist:
- Disable the plug-in and use Acrobat as a helper application. To do this, launch Acrobat, go to the Edit menu, Preferences, Internet, and uncheck "Display PDF in browser". Then, the next time you load a PDF in IE, it displays the dialog box "Open it" or "Save it to disk". Uncheck the option "Always ask before opening this type of file" and choose Open. From now on, PDF files will open automatically in an external Acrobat window.
The drawback of the method is that you need to alter the client configuration, which you can do in an intranet environment but not for the Internet.
- Use a redirection technique. It consists in generating the PDF in a temporary file on the server and redirect the client to it. For example, at the end of the script, you can put the following:
//Determine a temporary file name in the current directory $file = basename(tempnam('.', 'tmp')); rename($file, $file.'.pdf'); $file .= '.pdf'; //Save PDF to file $pdf->Output($file, 'F'); //Redirect header('Location: '.$file);
This method turns the dynamic PDF into a static one and avoids all troubles. But you have to do some cleaning in order to delete the temporary files. For example:
function CleanFiles($dir) { //Delete temporary files $t = time(); $h = opendir($dir); while($file=readdir($h)) { if(substr($file,0,3)=='tmp' && substr($file,-4)=='.pdf') { $path = $dir.'/'.$file; if($t-filemtime($path)>3600) @unlink($path); } } closedir($h); }
This function deletes all files of the form tmp*.pdf older than an hour in the specified directory. You may call it where you want, for example in the script which generates the PDF.
-
You have to enclose your string with double quotes, not single ones.
-
You have to use the
global
keyword to access global variables, for example:function Header() { global $title; $this->SetFont('Arial', 'B', 15); $this->Cell(0, 10, $title, 1, 1, 'C'); } $title = 'My title';
Alternatively, you can use an object property:
function Header() { $this->SetFont('Arial', 'B', 15); $this->Cell(0, 10, $this->title, 1, 1, 'C'); } $pdf->title = 'My title';
-
You have to create an object from the PDF class, not FPDF:
$pdf = new PDF();
-
Don't use UTF-8 encoding. Standard FPDF fonts use ISO-8859-1 or Windows-1252. It is possible to perform a conversion to ISO-8859-1 with utf8_decode():
$str = utf8_decode($str);
But some characters such as Euro won't be translated correctly. If the iconv extension is available, the right way to do it is the following:
$str = iconv('UTF-8', 'windows-1252', $str);
-
The standard fonts have the Euro character at position 128. You can define a constant like this for convenience:
define('EURO', chr(128));
-
I get the following error when I try to generate a PDF: Some data has already been output, can't send PDF file
You must send nothing to the browser except the PDF itself: no HTML, no space, no carriage return. A common case is having extra blank at the end of an included script file. If you can't figure out where the problem comes from, this other message appearing just before can help you:
Warning: Cannot modify header information - headers already sent by (output started at script.php:X)
It means that script.php outputs something at line X. Go to this line and fix it. In case the message doesn't show, first check that you didn't disable warnings, then add this at the very beginning of your script:
ob_end_clean();
If you still don't see it, disable zlib.output_compression in your php.ini and it should appear.
-
To respect dimensions, select "None" for the Page Scaling setting instead of "Shrink to Printable Area" in the print dialog box.
-
I'd like to use the whole surface of the page, but when printed I always have some margins. How can I get rid of them?
Printers have physical margins (different depending on the models); it is therefore impossible to remove them and print on the whole surface of the paper.
-
For a picture, call Image() in the Header() method, before any other output. To set a background color, use Rect().
-
Simply test the page number:
function Header() { if ($this->PageNo()==1) { //First page ... } else { //Other pages ... } }
-
Use an inheritance chain. If you have two classes, say A in a.php:
require('fpdf.php'); class A extends FPDF { ... }
and B in b.php:
require('fpdf.php'); class B extends FPDF { ... }
then make B extend A:
require('a.php'); class B extends A { ... }
and make your own class extend B:
require('b.php'); class PDF extends B { ... } $pdf = new PDF();
-
As any other file, but an easy way is to use PHPMailer and its in-memory attachment:
$mail = new PHPMailer(); ... $doc = $pdf->Output('', 'S'); $mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf'); $mail->Send();
-
There is no particular limit. There are some constraints, however:
-
The maximum memory size allocated to PHP scripts is usually 8MB. For very big documents, especially with images, this limit may be reached (the file being built into memory). The parameter is configured in the php.ini file.
-
The maximum execution time allocated defaults to 30 seconds. This limit can of course be easily reached. It is configured in php.ini and may be altered dynamically with set_time_limit().
-
Browsers generally have a 5 minute time-out. If you send the PDF directly to the browser and reach the limit, it will be lost. It is therefore advised for very big documents to generate them in a file, and to send some data to the browser from time to time (with a call to flush() to force the output). When the document is finished, you can send a redirection to it or create a link.
Remark: even if the browser times out, the script may continue to run on the server.
-
-
It is possible to import pages from an existing PDF document thanks to the FPDI extension:
http://www.setasign.de/products/pdf-php-solutions/fpdi/
You can then add some content to them.
-
No. But a GPL C utility does exist, pdftotext, which is able to extract the textual content from a PDF. It is provided with the Xpdf package:
-
Not real-world pages. But a GPL C utility does exist, htmldoc, which allows to do it and gives good results:
-
Not directly, but it is possible to use FPDI to perform this task. Some free command-line tools also exist: