note 8352 deleted from ref.cpdf by jimw
| From: | jimw@php.net | Date: | Sat, 08 Dec 2001 22:25:35 +0000 |
| Subject: | note 8352 deleted from ref.cpdf by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-22303@lists.php.net to get a copy of this message | ||
Example 1 is broken since, units being left at their defaults, the script tries to write text
wwwaaayyy out of the A4 page is sets up. To better illustrate the point, here is a proposed new
version:
<?php
/* define no. of points in a millimeter */
define("PPM", 2.83464566929);
$pdf = cpdf_open(0);
/* Set page size to A4 (_MUST_ use points). */
/* Set unit to millimeters for computing object */
/* coordinates in the rest of the script. */
cpdf_page_init($pdf, 1, 0, 595, 842, PPM);
cpdf_add_outline($pdf, 0, 0, 0, 1, "Page 1");
cpdf_begin_text($pdf);
/* Note: font size (30) _MUST_ always be given in points. */
cpdf_set_font($pdf, "Times-Roman", 30, "WinAnsiEncoding");
cpdf_set_text_rendering($pdf, 1);
/* Lower left coordinates for the string _MUST_ now */
/* be given in millimeters, origin being the lower left */
/* corner of the page. */
cpdf_text($pdf, "Times Roman outlined", 100, 100);
cpdf_end_text($pdf);
/* Coordinates for segment end points _MUST_ now */
/* be given in millimeters, origin being the lower left */
/* corner of the page. */
cpdf_moveto($pdf, 1000, 950);
cpdf_lineto($pdf, 1985, 950);
cpdf_stroke($pdf);
cpdf_finalize($pdf);
Header("Content-Type: application/pdf");
cpdf_output_buffer($pdf);
cpdf_close($pdf);
?>
Beware though that, when using the Acrobat plug-in in Win32, you will be able to see the correct
output _ONLY_ under Netscape, MSIE doesn't show anything (well for the clock example at least,
refreshing the page once does the trick - go figure)!
I found out MSIE _REQUIRES_ a correctly set Content-Length header to be sent beside Content-Type to
correctly display PDFs... this is a nuisance, since the PHP ClibPDF functions don't include a
way to get the length of a document when you create it in memory, and you have to:
1) write the document to a file, get the filelength and fopen+fpassthru it after having sent the
Content-Length header;
2) e.g. fopen a "secondary" PHP script that creates the PDF in memory,
"cpdf_output_buffer"s it to the "primary" script that reads to EOF into a
string, "strlen"s it and "print"s it after setting the required header.
Both these "hacks" work, even though I'd much prefer to have an additional function
to get the buffer's length (hint, hint :) ); I also tried the new (as of PHP 4.0.2)
output_control functions (ob_start, etc.) to no avail - cpdf_output_buffer apparently is not similar
enough to echo.
Hope this helps
Massimo