略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: PDF_show_boxed

2024-11-15

PDF_show_boxed

(PHP 4, PECL pdflib >= 1.0.0)

PDF_show_boxedOutput text in a box [deprecated]

说明

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
8 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
10 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
11 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
14 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
14 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
14 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
14 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
15 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
16 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
16 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
16 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
17 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
17 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
17 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
17 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
17 years ago
IT_KTS < I thought it wasn't important, but I had this fatal error :

官方地址:https://www.php.net/manual/en/function.pdf-show-boxed.php