|
PDF_show_boxed
Output text in a box [deprecated]
()
Code Examples / Notes » pdf_show_boxedphict4
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.
bob
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; ?> hackajar matt yahoo trott com
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. 06-dec-2001 01: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 it_kts
pdf_show_boxed only works with 2 or more words not one word... try it people..
matt
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. flyingman15
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... lofino
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. jochen dot riehm
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
ceo
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. bob
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... kinneko
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.
james
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) leea
Here's a way to make a dynamic size text box for text coming from a database $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! asmodai
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. sebastien
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) ceo
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. bloecheratgmxdotde
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] |
Change LanguagePDF_activate_item PDF_add_annotation PDF_add_bookmark PDF_add_launchlink PDF_add_locallink PDF_add_nameddest PDF_add_note PDF_add_outline PDF_add_pdflink PDF_add_table_cell PDF_add_textflow PDF_add_thumbnail PDF_add_weblink PDF_arc PDF_arcn PDF_attach_file PDF_begin_document PDF_begin_font PDF_begin_glyph PDF_begin_item PDF_begin_layer PDF_begin_page_ext PDF_begin_page PDF_begin_pattern PDF_begin_template_ext PDF_begin_template PDF_circle PDF_clip PDF_close_image PDF_close_pdi_page PDF_close_pdi PDF_close PDF_closepath_fill_stroke PDF_closepath_stroke PDF_closepath PDF_concat PDF_continue_text PDF_create_3dview PDF_create_action PDF_create_annotation PDF_create_bookmark PDF_create_field PDF_create_fieldgroup PDF_create_gstate PDF_create_pvf PDF_create_textflow PDF_curveto PDF_define_layer PDF_delete_pvf PDF_delete_table PDF_delete_textflow PDF_delete PDF_encoding_set_char PDF_end_document PDF_end_font PDF_end_glyph PDF_end_item PDF_end_layer PDF_end_page_ext PDF_end_page PDF_end_pattern PDF_end_template PDF_endpath PDF_fill_imageblock PDF_fill_pdfblock PDF_fill_stroke PDF_fill_textblock PDF_fill PDF_findfont PDF_fit_image PDF_fit_pdi_page PDF_fit_table PDF_fit_textflow PDF_fit_textline PDF_get_apiname PDF_get_buffer PDF_get_errmsg PDF_get_errnum PDF_get_font PDF_get_fontname PDF_get_fontsize PDF_get_image_height PDF_get_image_width PDF_get_majorversion PDF_get_minorversion PDF_get_parameter PDF_get_pdi_parameter PDF_get_pdi_value PDF_get_value PDF_info_font PDF_info_matchbox PDF_info_table PDF_info_textflow PDF_info_textline PDF_initgraphics PDF_lineto PDF_load_3ddata PDF_load_font PDF_load_iccprofile PDF_load_image PDF_makespotcolor PDF_moveto PDF_new PDF_open_ccitt PDF_open_file PDF_open_gif PDF_open_image_file PDF_open_image PDF_open_jpeg PDF_open_memory_image PDF_open_pdi_page PDF_open_pdi PDF_open_tiff PDF_pcos_get_number PDF_pcos_get_stream PDF_pcos_get_string PDF_place_image PDF_place_pdi_page PDF_process_pdi PDF_rect PDF_restore PDF_resume_page PDF_rotate PDF_save PDF_scale PDF_set_border_color PDF_set_border_dash PDF_set_border_style PDF_set_char_spacing PDF_set_duration PDF_set_gstate PDF_set_horiz_scaling PDF_set_info_author PDF_set_info_creator PDF_set_info_keywords PDF_set_info_subject PDF_set_info_title PDF_set_info PDF_set_layer_dependency PDF_set_leading PDF_set_parameter PDF_set_text_matrix PDF_set_text_pos PDF_set_text_rendering PDF_set_text_rise PDF_set_value PDF_set_word_spacing PDF_setcolor PDF_setdash PDF_setdashpattern PDF_setflat PDF_setfont PDF_setgray_fill PDF_setgray_stroke PDF_setgray PDF_setlinecap PDF_setlinejoin PDF_setlinewidth PDF_setmatrix PDF_setmiterlimit PDF_setpolydash PDF_setrgbcolor_fill PDF_setrgbcolor_stroke PDF_setrgbcolor PDF_shading_pattern PDF_shading PDF_shfill PDF_show_boxed PDF_show_xy PDF_show PDF_skew PDF_stringwidth PDF_stroke PDF_suspend_page PDF_translate PDF_utf16_to_utf8 PDF_utf32_to_utf16 PDF_utf8_to_utf16 |