PHP Create PDF
file on fly With ClibPDF
This tutorial demos how to create a PDF file on fly with
ClibPDF.
Testing ClibPDF installation
First, you need test if your PHP supports ClibPDF or not.
Copy the following code to a file (phpinfo.php) and save it in your
web root. Then launch it like : http://your-web-site/phpinfo.php.
If your PHP supports ClibPDF, you will see an section
looks like the following image:
Get input from Client
We will put client input into our PDF dynamically. Use the following
code to get client input.
<html>
<head>
<title>Name Entry Level</title>
</head>
<body>
<table>
<tr>
<td>
<form method="POST" action="cpdfdemo.php">
Enter your text here please =>
<input type="text" name="input" size="20" maxlength="20">
<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset">
</form>
</td>
</tr>
</table>
</body>
</html> |
Create PDF with CPDF
We use the following code to create PDF. Save the following code
as cpdfdemo.php.
<?php
if(isset($_POST['submit'])
{
$mytext=$_POST['
input'];
$cpdf = cpdf_open(0);
cpdf_page_init($cpdf, 1, 0, 595, 842, 1.0);
cpdf_add_outline($cpdf, 0, 0, 0, 1, "Page 1");
//write text
cpdf_setrgbcolor ($cpdf,255,0,255);
cpdf_begin_text($cpdf);
cpdf_set_font($cpdf, "Times-Roman", 30, "WinAnsiEncoding");
cpdf_set_text_rendering($cpdf, 2);
cpdf_text($cpdf, $mytext,
50, 340);
cpdf_end_text($cpdf);
//draw a blue line
cpdf_setrgbcolor ($cpdf,0,0,255); //blue line
cpdf_moveto($cpdf, 50, 50);
cpdf_lineto($cpdf, 740, 330);
cpdf_stroke($cpdf);
//draw a red circle
cpdf_setrgbcolor ($cpdf,255,0,0); //color read
cpdf_circle($cpdf,200,200,50);
cpdf_stroke($cpdf);
cpdf_finalize_page($cpdf, 1);
cpdf_finalize($cpdf);
Header("Content-type: application/pdf");
cpdf_output_buffer($cpdf);
cpdf_close($cpdf);
}
?> |
$_POST['xxx'] gets parameters from client.
cpdf_open opens a PDF file.
cpdf_page_init set PDF sizes: A4
is 595 x 842, Letter is 612 x 792 and Legal is 612 x 1008.
cpdf_add_outline adds bookmark for current page.
cpdf_setrgbcolor sets color for next drawing action.
The following code are annotated in the code.
|