|
|
X. ClibPDF functions
ClibPDF lets you create PDF documents with PHP. It is available at
but it isn't free
software. You should definitely read the licence before you start
playing with ClibPDF. If you cannot fullfil the licence agreement
consider using pdflib by Thomas Merz, which is also very powerful.
ClibPDF functionality and API is similar to Thomas Merz's pdflib but,
according to FastIO, ClibPDF is faster and creates smaller documents.
This may have changed with the new version 2.0 of pdflib. A simple
benchmark (the pdfclock.c example from pdflib 2.0 turned into a php
script) actually shows no difference in speed at all. The file size
is also similar if compression is turned off. So, try them both
and see which one does the job for you.
This documentation should be read alongside the ClibPDF manual since it
explains the library in much greater detail.
Many functions in the native ClibPDF and the PHP module, as
well as in pdflib, have the same name. All functions except for
cpdf_open() take the handle for the document as
their first parameter.
Currently this handle is not used internally since ClibPDF does
not support the creation of several PDF documents at the same time.
Actually, you should not even try it, the results are unpredictable. I
can't oversee what the consequences in a multi threaded environment
are. According to the author of ClibPDF this will change in one of
the next releases (current version when this was written is 1.10).
If you need this functionality use the pdflib module.
참고:
The function cpdf_set_font() has changed since PHP 3
to support asian fonts. The encoding parameter is no longer an integer
but a string.
One big advantage of ClibPDF over pdflib used to be the possibility
to create
the pdf document completely in memory without using temporary files.
It also provides the ability to pass coordinates in a predefined
unit length. This is a handy feature but can be simulated with
pdf_translate().
Another nice feature of ClibPDF is the fact that any page can be modified
at any time even if a new page has been already opened. The function
cpdf_set_current_page() allows to leave the current
page and presume modifying an other page.
Most of the functions are fairly easy to use. The most difficult part
is probably creating a very simple PDF document at all. The following
example should help you to get started. It creates a document
with one page. The page contains the text "Times-Roman" in an
outlined 30pt font. The text is underlined.
예 1. Simple ClibPDF Example <?php
$cpdf = cpdf_open(0);
cpdf_page_init($cpdf, 1, 0, 595, 842);
cpdf_add_outline($cpdf, 0, 0, 0, 1, "Page 1");
cpdf_begin_text($cpdf);
cpdf_set_font($cpdf, "Times-Roman", 30, "WinAnsiEncoding");
cpdf_set_text_rendering($cpdf, 1);
cpdf_text($cpdf, "Times Roman outlined", 50, 750);
cpdf_end_text($cpdf);
cpdf_moveto($cpdf, 50, 740);
cpdf_lineto($cpdf, 330, 740);
cpdf_stroke($cpdf);
cpdf_finalize($cpdf);
Header("Content-type: application/pdf");
cpdf_output_buffer($cpdf);
cpdf_close($cpdf);
?> |
|
The pdflib distribution contains a more complex example which creates a
series of pages with an analog clock. Here is that example converted
into PHP using the ClibPDF extension:
예 2. pdfclock example from pdflib 2.0 distribution <?php
$radius = 200;
$margin = 20;
$pagecount = 40;
$pdf = cpdf_open(0);
cpdf_set_creator($pdf, "pdf_clock.php3");
cpdf_set_title($pdf, "Analog Clock");
while($pagecount-- > 0) {
cpdf_page_init($pdf, $pagecount+1, 0, 2 * ($radius + $margin), 2 * ($radius + $margin), 1.0);
cpdf_set_page_animation($pdf, 4, 0.5, 0, 0, 0); /* wipe */
cpdf_translate($pdf, $radius + $margin, $radius + $margin);
cpdf_save($pdf);
cpdf_setrgbcolor($pdf, 0.0, 0.0, 1.0);
/* minute strokes */
cpdf_setlinewidth($pdf, 2.0);
for ($alpha = 0; $alpha < 360; $alpha += 6)
{
cpdf_rotate($pdf, 6.0);
cpdf_moveto($pdf, $radius, 0.0);
cpdf_lineto($pdf, $radius-$margin/3, 0.0);
cpdf_stroke($pdf);
}
cpdf_restore($pdf);
cpdf_save($pdf);
/* 5 minute strokes */
cpdf_setlinewidth($pdf, 3.0);
for ($alpha = 0; $alpha < 360; $alpha += 30)
{
cpdf_rotate($pdf, 30.0);
cpdf_moveto($pdf, $radius, 0.0);
cpdf_lineto($pdf, $radius-$margin, 0.0);
cpdf_stroke($pdf);
}
$ltime = getdate();
/* draw hour hand */
cpdf_save($pdf);
cpdf_rotate($pdf, -(($ltime['minutes']/60.0) + $ltime['hours'] - 3.0) * 30.0);
cpdf_moveto($pdf, -$radius/10, -$radius/20);
cpdf_lineto($pdf, $radius/2, 0.0);
cpdf_lineto($pdf, -$radius/10, $radius/20);
cpdf_closepath($pdf);
cpdf_fill($pdf);
cpdf_restore($pdf);
/* draw minute hand */
cpdf_save($pdf);
cpdf_rotate($pdf, -(($ltime['seconds']/60.0) + $ltime['minutes'] - 15.0) * 6.0);
cpdf_moveto($pdf, -$radius/10, -$radius/20);
cpdf_lineto($pdf, $radius * 0.8, 0.0);
cpdf_lineto($pdf, -$radius/10, $radius/20);
cpdf_closepath($pdf);
cpdf_fill($pdf);
cpdf_restore($pdf);
/* draw second hand */
cpdf_setrgbcolor($pdf, 1.0, 0.0, 0.0);
cpdf_setlinewidth($pdf, 2);
cpdf_save($pdf);
cpdf_rotate($pdf, -(($ltime['seconds'] - 15.0) * 6.0));
cpdf_moveto($pdf, -$radius/5, 0.0);
cpdf_lineto($pdf, $radius, 0.0);
cpdf_stroke($pdf);
cpdf_restore($pdf);
/* draw little circle at center */
cpdf_circle($pdf, 0, 0, $radius/30);
cpdf_fill($pdf);
cpdf_restore($pdf);
cpdf_finalize_page($pdf, $pagecount+1);
}
cpdf_finalize($pdf);
Header("Content-type: application/pdf");
cpdf_output_buffer($pdf);
cpdf_close($pdf);
?> |
|
User Contributed Notes ClibPDF functions |
add a note |
daniel[at]lorch.cc
22-Feb-2002 12:50 |
|
If you don't want to pay for the library or your hoster does not have it
installed, have a look at "PHP pdf Class":
|
|
egrebNOSPAM at trivini dot com
04-Sep-2002 09:13 |
|
As of version 2.02-r1-1, ClibPDF claims to be multi-threaded-safe.
|
|
12-Mar-2003 02:33 |
|
You could also use FPDF. Find it at this address:
|
|
sinaler at superonline dot com
26-Mar-2003 06:33 |
|
In your manual, the cpdf functions are missing or non-updated.
for
example cpdf_textbox()....
|
|
puterasalji at yahoo dot com
07-Apr-2003 03:32 |
|
I've finally found a way to display text
<?php $radius =
200; $margin = 20; $pagecount = 1;
$pdf =
cpdf_open(0);
cpdf_page_init($pdf, $pagecount+1, 0, 2 * ($radius
+ $margin), 2 * ($radius + $margin), 1.0);
cpdf_translate($pdf,
$radius + $margin, $radius + $margin);
cpdf_begin_text($pdf);
cpdf_set_font($pdf, "Arial", 30, "NULL");
cpdf_set_text_rendering($pdf, 1); cpdf_text($pdf,
"Yahoo!!", -198, 50); cpdf_end_text($pdf);
/*
minute strokes*/ for ($alpha = 0; $alpha < 90; $alpha += 6)
{ cpdf_setrgbcolor($pdf,9.0,9.0,0.0); cpdf_rotate($pdf,
6.0); cpdf_moveto($pdf, $radius, 0.0); cpdf_lineto($pdf,
$radius-$margin/3, 0.0); cpdf_stroke($pdf);
}
cpdf_finalize($pdf); Header("Content-type:
application/pdf"); cpdf_output_buffer($pdf); cpdf_close($pdf); ?>
I'm
not sure why, but the text appeared only after I've pasted the minute
strokes draw function from clock.php example. I user the cpdf_translate to
begin the x,y coordinate in the middle of the document
|
|
add a note |
| |