note 39797 added to function.pdf-show-boxed
| From: | hackajarmattyahootrottcom at rn2 dot php dot net | Date: | Wed, 11 Feb 2004 02:12:47 +0000 |
| Subject: | note 39797 added to function.pdf-show-boxed | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-64985@lists.php.net to get a copy of this message | ||
Sometimes using pdf_show_boxed() is no help at all, such as in a parser, where you have html tags to
mangle text with. Example:
pdf_show_boxed() with text tokens and html tags will look like this:
_____________________
| ------------------ |
|| text_box1 ||box| |
| ------------ 2| |
| ---- |
| |
| A PDF PAGE |
|____________________|
box 2 is where you changed to bold or italic in mid sentance, and now it moves down to next line,
but at $x cor, or if your not using pdf_show_boxed ; but pdf_show_xy, it will trail into your
right-margin or worse off the page
Solution: Use a word chuck loop
//Get space left to end of page
$space = (612-$right_margin);
//Get total length of text string
$length = pdf_stringwidth($pdf, $text);
//Check to see if there is enough space just
//to dump text to page
if($length > $space) {
//Start a parser
$count = strlen($text);
for($i=o; $i<$count;) {
//Get the first word in text string
for($e=$i; $e<$count && substr($text, $e, 1)!=" "; $e++);
//Put that word into a string (don't forget to +1
//for the space we used at stopping marker
$string = substr($text, $i, ($e-$i)+1);
//Now get the length of THAT text string
$length = pdf_stringwidth($pdf, $string);
if($length < $space) {
pdf_show_xy($pdf, $string, $x, $y);
$x = $x+$length;
//Because this routine gets cycled everytime
//you send text to it, make sure you update
//how much space is left on the line
$space = (612-36)-$x;
} else {
//Move down one line as there is no space
//left, note that I use "2" as my padding, yours
//may vary
$y = $y - ($fontsize +2);
$x = $left_margin;
pdf_show_xy($pdf, $string, $x, $y);
$x = $x+$length;
$space = (612-36)-$x;
}
//Move the loop counter to end of this word +
//the space we used as stop marker
$i=$e+1;
}
} else {
if($center == "1") {
pdf_show_boxed($pdf, $text, 0, $y, 612, $fontsize, "center");
} else {
pdf_show_xy($pdf, $text, $x, $y);
}
$x = $x + $length;
}
This is a part of a function I use, so it may look a little mangled, great for a starting point
though.
----
Manual Page -- http://www.php.net/manual/en/function.pdf-show-boxed.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39797
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39797&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39797&report=yes
Search -- http://master.php.net/manage/user-notes.php