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.
PDF_show_boxed
(PHP 4, PECL pdflib >= 1.0.0)
PDF_show_boxed — ボックスにテキストを出力する [古い関数]
説明
int PDF_show_boxed
( resource $p
, string $text
, float $left
, float $top
, float $width
, float $height
, string $mode
, string $feature
)
この関数は PDFlib バージョン 6 で廃止されました。かわりに 単一行の場合は PDF_fit_textline()、 複数行の場合は PDF_*_textflow() 関数を 使用してください。
PDF_show_boxed
Ben
06-Jun-2008 07:45
06-Jun-2008 07:45
james at lightbox dot org
13-Dec-2004 09:51
13-Dec-2004 09:51
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)
asmodai at wxs dot nl
05-Nov-2004 01:21
05-Nov-2004 01:21
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.
phict4 at hotmail dot com
30-Oct-2004 04:45
30-Oct-2004 04:45
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.
flyingman15 at yahoooo point fr
23-Sep-2004 03:01
23-Sep-2004 03:01
Little improve from what bob at nijman dot de said :
$text = "Rduire le dficit de l'tat, voil bien l'ambition annonce de Nicolas Sarkozy. Pour son unique budget, le mdiatique patron des finances franaises estime avoir rempli son pari. Mais quel prix ? Ce projet de loi de finances oublie la promesse prsidentielle de baisser l'impt sur le revenu et la pression fiscale sur les mnages augmente.Face aux nombreux journalistes, le bouillonnant Sarkozy dfend son bb. Un premier n, bientt orphelin d'un pre qui s'en va prendre, en novembre, la tte de l'UMP.";
$line_width = 300.0;
$line_height = 20.0;
$x = 10.0;
$y = 230.0;
$nr = pdf_show_boxed($pdf, $text, $x, $y, $line_width, $line_height, "left");
$y = $y - $line_height;
while($nr > 0){
$nr = pdf_show_boxed($pdf, $nr.substr($text, -$nr), $x, $y, $line_width , $line_height, "left");
$y = $y - $line_height;
}
Quite usefull....for me at least, so if that can help anybody else...
sebastien at galliot dot com
26-Jun-2004 01:27
26-Jun-2004 01:27
easier than pdf_show_boxed () !
to center text in a box :
<?
pdf_fit_textline ($pdf, "$string", $x, $y, "boxsize {40 20} position 50");
?>
You can also fit text proportionnally in the box :
<?
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)
hackajar matt yahoo trott com
11-Feb-2004 07:12
11-Feb-2004 07:12
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.
matt at unixhead withoutspam dot org
04-Feb-2003 03:12
04-Feb-2003 03:12
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.
bloecherATgmxDOTde
15-Aug-2002 02:37
15-Aug-2002 02:37
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]
kinneko at whopper dot de
16-Jul-2002 03:14
16-Jul-2002 03:14
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.
ceo at l-i-e dot com
18-Feb-2002 11:00
18-Feb-2002 11:00
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.
ceo at l-i-e dot com
18-Feb-2002 03:07
18-Feb-2002 03:07
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.
Jochen dot Riehm at teilauto dot net
09-Jan-2002 09:50
09-Jan-2002 09:50
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
06-Dec-2001 03:29
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
lofino at caramail dot com
30-Oct-2001 08:21
30-Oct-2001 08:21
IT_KTS < I thought it wasn't important, but I had this fatal error : Fatal error: PDFlib error: floating point value too large in pdf_ftoa
After looking for the reasons of this fatal error, I've finally found how it comes :
- you have to have only one word
- this word has to be too long for the box
- you have to be in justify mode
If you meet this fatal error, jump into the "left" mode, or split your word/sentence.
IT_KTS at PHISL dot NET
06-Sep-2001 01:47
06-Sep-2001 01:47
pdf_show_boxed only works with 2 or more words not one word... try it people..
bob at nijman dot de
02-Aug-2001 05:10
02-Aug-2001 05:10
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...
bob at nijman dot de
02-Aug-2001 05:05
02-Aug-2001 05:05
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.
Fr verschiedene Arbeitsmodule werden freie Mitarbeiter herangezogen,
damit wir absolute Flexibilitt behalten und messerscharf kalkulieren knnen.
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;
?>
leea at digitus dot com dot au
31-Jan-2001 03:41
31-Jan-2001 03:41
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!
