PDF_show_boxed

(PHP 4, PECL pdflib >= 1.0.0)

PDF_show_boxedOutput text in a box [deprecated]

Opis

PDF_show_boxed ( resource $p , string $text , float $left , float $top , float $width , float $height , string $mode , string $feature ) : int

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.

add a note add a note

User Contributed Notes 21 notes

up
1
J Griffiths
13 years ago
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!
up
0
han__hui at hotmail dot com
14 years ago
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.
up
0
Ben
15 years ago
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.
up
0
james at lightbox dot org
19 years ago
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)
up
0
asmodai at wxs dot nl
19 years ago
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.
up
0
phict4 at hotmail dot com
19 years ago
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.
up
0
sebastien at galliot dot com
19 years ago
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)
up
0
hackajar matt yahoo trott com
20 years ago
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.
up
0
matt at unixhead withoutspam dot org
21 years ago
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.
up
0
bloecherATgmxDOTde
21 years ago
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]
up
0
kinneko at whopper dot de
21 years ago
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.
up
0
ceo at l-i-e dot com
22 years ago
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.
up
0
ceo at l-i-e dot com
22 years ago
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.
up
0
Jochen dot Riehm at teilauto dot net
22 years ago
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
up
0
Anonymous
22 years ago
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
up
0
lofino at caramail dot com
22 years ago
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.
up
0
IT_KTS at PHISL dot NET
22 years ago
pdf_show_boxed only works with 2 or more words not one word... try it people..
up
0
bob at nijman dot de
22 years ago
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...
up
0
bob at nijman dot de
22 years ago
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;

?>
up
-1
flyingman15 at yahoooo point fr
19 years ago
Little improve from what bob at nijman dot de said :

$text = "Réduire le déficit de l'État, voilà bien l'ambition annoncée de Nicolas Sarkozy. Pour son unique budget, le médiatique patron des finances françaises estime avoir rempli son pari. Mais à quel prix ? Ce projet de loi de finances oublie la promesse présidentielle de baisser l'impôt sur le revenu et la pression fiscale sur les ménages augmente.Face aux nombreux journalistes, le bouillonnant Sarkozy défend son bébé. Un premier né, bientôt orphelin d'un père qui s'en va prendre, en novembre, la tête 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...
up
-1
leea at digitus dot com dot au
23 years ago
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!
To Top