©
本文檔使用 php中文網手册 發布
(PHP 4, PECL pdflib >= 1.0.0)
PDF_show_boxed — Output text in a box [deprecated]
$p
, string $text
, float $left
, float $top
, float $width
, float $height
, string $mode
, string $feature
)This function is deprecated since PDFlib version 6, use PDF_fit_textline() for single lines, or the PDF_*_textflow() functions for multi-line formatting instead.
[#1] J Griffiths [2010-06-29 18:49:08]
As this is nowadays the poor/stingy man's alternative to the commercial PDFlib PDF_*_textflow() functions, I thought it might be useful to point out something that isn't explicitly mentioned here, but is helpful to determine a text box's height before committing it to the PDF:
This function returns the number of characters that would not fit in the prescribed dimensions.
Therefore, if, as noted below, the only feature, "blind", is passed as the last value to this function, you can determine whether or not a certain height is sufficient for your text, and increment as necessary, or decide to start a new page, without writing the text block to the PDF, by checking whether or not the function returns 0.
This avoids the problem of the "texty" value becoming available only when the box has been written, by which time it might too late!
[#2] han__hui at hotmail dot com [2009-04-27 00:46:43]
an example to illustrate the use of pdf_show_boxed(...).
<?php
$pdf = pdf_new();
pdf_set_parameter($pdf,"license","xxxxxxx-xxxxxx-xxxxxx-xxxxxx"); //to remove the stamp/wartermark of the generated pdf file.
pdf_set_info($pdf, "Creator", "pdf.php");
pdf_set_info($pdf, "Author", "hui");
pdf_open_file($pdf,""); //without a file name, it will open the pdf file in the browser directly.
pdf_begin_page($pdf,595,842); //A4 page
$font = pdf_findfont($pdf, "Helvetica", "host", 0);
$lineHeight = 10;
$y = 800;
pdf_setfont($pdf, $font, $lineHeight);
$text = "Hi, I am Hui.\n\rI am working in IT Dept.\nI am good at linux administration and programming in Java, PHP, Javascript, C and C++.";
$ret = pdf_show_boxed($pdf,$text,30,$y,350,$lineHeight,"left","");
$lineSpace = 5;
while($ret>0) //write the whole text into the pdf line by line including the new line character
{
$y = $y - $lineHeight-$lineSpace;
$left = substr($text,-$ret);
if($left[0] == "\n" or $left[0] == "\r") $left = substr($left,1); //this line decrements the $ret by 1.
$ret = pdf_show_boxed($pdf,$left,30,$y,350,$lineHeight,"left","");
}
pdf_end_page($pdf);
pdf_close($pdf);
$buf = pdf_get_buffer($pdf);
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=whatever.pdf");
print $buf;
?>
PS: Without "if" in the while loop, it will be an infinite loop when it encounters "\n" or "\r" in the text to be written.
[#3] Ben [2008-06-06 09:45:29]
Be aware that a long unbroken section of text, such as a line of characters that is too wide to fit into the allotted space, will cause a seg fault.
I have a user who entered a long line of Q's as a placeholder in his text. Attempting to output to PDF with the Q's in place caused a seg fault.
The solution is to test the width of words to ensure that they are narrow enough to fit in the allotted width and, if not, to break them so that they are.
[#4] james at lightbox dot org [2004-12-13 11:51:29]
I'm using the following libpdf versions:
PDFlib GmbH Version 6.0.1
PECL Version 2.0.3
I found the parameter 'top' to be misleading. Since 0,0 is in the bottom left of the page, the top parameter is actually the bottom left corner of the box you want.
using
top=72
left=72
puts the bottom left corner of the box 1 inch in from the left, and 1 inch up from the bottom.
so to make a 6.5"x3" box (with 1" page margins) at the BOTTOM of the page, you would use:
top=72
left=72
width=468
height=216
The text would then start in the top-left corner of the defined box, which would be at (72,288) (in 1" from the left and up 4" from the bottom)
[#5] asmodai at wxs dot nl [2004-11-05 03:21:32]
For those who are as confused as I was (still am in small parts):
mode can be at least: left, right, center, justify
feature: if specified as "" will immediately display the desired text, if specified as "blind" will hide the text
This had me stumped for a while.
[#6] phict4 at hotmail dot com [2004-10-30 06:45:50]
With PHP5 the feature parameter is required, set this to null or false to produce the same result as without the parameter in versions previous to 5.
[#7] flyingman15 at yahoooo point fr [2004-09-23 05:01:40]
[#8] sebastien at galliot dot com [2004-06-25 15:27:25]
easier than pdf_show_boxed () !
to center text in a box :
<?php
pdf_fit_textline ($pdf, "$string", $x, $y, "boxsize {40 20} position 50");
?>
You can also fit text proportionnally in the box :
<?php
pdf_fit_textline ($pdf, "$string", $x, $y, "boxsize {40 20} position 50 fitmethod meet");
?>
See Pdflib documentation for other text placement functions
(4.8.2 Placing Text in a Box in the Pdf documentation)
[#9] hackajar matt yahoo trott com [2004-02-10 21:12:47]
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.
[#10] matt at unixhead withoutspam dot org [2003-02-04 05:12:38]
note if you would rather resize the font than the textbox on the PDF you produce (i.e. if you need a set layout) you could do something like:
while (pdf_show_boxed($pdfdoc, $txt, 220, 100, 180, 250, "left", "blind") > 1) {
$font--;
pdf_set_font($pdfdoc, "Courier", $font, "host");
}
pdf_show_boxed($pdfdoc,$txt,220,100,180,250,"left");
This will keep downsizing the font until all the text fits in the box you specify.
[#11] bloecherATgmxDOTde [2002-08-15 04:37:22]
better use pdf_get_value($p, 'leading') for calculating the height of your textbox, coz 'fontsize' might be a little to small as it's not the distance between the baselines =)
[see pdflib-manual, table 4.7]
[#12] kinneko at whopper dot de [2002-07-15 17:14:34]
If you have newlines in your data ("\n") a following PDF_get_value($pdf, "texty") may not return the positon of the last char that is displayed.
[#13] ceo at l-i-e dot com [2002-02-18 01:00:30]
D'oh!
Ignore previous note.
There were newlines in my data. :-(
So a box only one line tall would fit any characters.
So the "Gotcha" is that embedded new-lines can make pdf_show_boxed incapable of drawing *any* of your text in a one-line tall box.
[#14] ceo at l-i-e dot com [2002-02-17 17:07:26]
It might be because my font files are corrupted, and I'm using setfont... 'host', 0
but it seems like sometimes pdf_draw_boxed can't draw *ANY* characters of perfectly normal text.
The words at the beginning of the text are certainly capable of fitting.
It works fine for some strings, and not for others.
Hard to say if it's really a bug or not, and yes, I'll file a bug report, but in the meantime, one has to be careful of not creating an infinite loop trying to draw the same impossible text over and over.
If the number of characters not drawn (return value of pdf_show_boxed) is equal to the length of the string you tried to draw, GAME OVER.
[#15] Jochen dot Riehm at teilauto dot net [2002-01-09 11:50:06]
It should be noted that creating a "blind" text box and using the texty value does not work since it is (as correctly mentioned in the manual) not changed by "blind" boxes
[#16] [2001-12-06 05:29:07]
Some improvement on leea@digitus.com.au code to get
a dynamic text box. This first compute a minimum height:
$fontsize = pdf_get_value($pdf,'fontsize');
$length = pdf_stringwidth($pdf,$text);
$textHeight = ceil($length/$width)*$fontsize;
while(pdf_show_boxed($pdf,$text, $left,$top,$width,$textHeight,$h_align,'blind') > 1)
$textHeight += $fontsize;
pdf_show_boxed($pdf,$text,$left,$top,$width,$textHeight,$h_align);
Ivan
[#17] lofino at caramail dot com [2001-10-29 22:21:08]
[#18] IT_KTS at PHISL dot NET [2001-09-06 03:47:10]
pdf_show_boxed only works with 2 or more words not one word... try it people..
[#19] bob at nijman dot de [2001-08-02 07:10:26]
In the example given above we don't handle the overflow properly.
Try making the text shorter and see what happens.
Try making it longer too...
[#20] bob at nijman dot de [2001-08-02 07:05:55]
Try this:
<?php
//Create & Open PDF-Object
$pdf = pdf_new();
pdf_open_file($pdf);
pdf_set_info($pdf, "Author","Bob Nijman");
pdf_begin_page($pdf, 300, 300);
pdf_set_font($pdf, "Helvetica", 10, "host");
//for most german will be as good as latin, no? :-)
$text = <<<EOD
Unsere Konzepte sind innovativ und kreativ.
Sie orientieren sich am Zeitgeist, ohne sich diesem auszuliefern.
So auch unsere Arbeitsweise.
F?r verschiedene Arbeitsmodule werden freie Mitarbeiter herangezogen,
damit wir absolute Flexibilit?t behalten und messerscharf kalkulieren k?nnen.
Die volle Verantwortung liegt damit beim Projektleiter.
Er vergibt Arbeitsmodule, begutachtet deren Werdegang und letztendlich deren Zusammenwachsen.
EOD;
$nr = pdf_show_boxed($pdf, $text, 10.0, 180.0, 140.0, 100.0, "left");
$nr = pdf_show_boxed($pdf, substr($text, -$nr), 150.0, 180.0, 140.0, 100.0, "left");
//close it up
pdf_end_page($pdf);
pdf_close($pdf);
$data = pdf_get_buffer($pdf);
header('Content-type: application/pdf');
header('Content-disposition: inline; filename=myTest.pdf');
header('Content-length: ' . strlen($data));
echo $data;
?>
[#21] leea at digitus dot com dot au [2001-01-30 17:41:53]
Here's a way to make a dynamic size text box for text coming from a database
<?php
$TextHeight = 10;
while ((pdf_show_boxed($pdf,$rstJobs["JobDetails"], 123, $varPosition, 308,$TextHeight,left,blind)) > 1) {
$TextHeight = $TextHeight + 10;
}
?>
pdf_show_boxed ($pdf, $rstJobs["JobDetails"], 123, $varPosition - $TextHeight + 10, 308,$TextHeight,left);
You will need to change the value of 10 to your font size.
this may not be the best way to do this so if you find a better way add it here!