Page 299 - Open Soource Technologies 304.indd
P. 299
Unit 12: Portable Document Format
The pdf_translate( ) function moves the origin to the top of the page, and pdf_scale( ) inverts Notes
the Y-axis coordinates. To avoid producing text that can be read only in a mirror, we set the
horizscaling parameter.
12.3.2 Text Functions
PDF files have the concept of the current text position. It’s like a cursor unless you specify
another location, when you insert text it appears at the current text location. You set the text
location with the pdf_set_textpos( ) function:
pdf_set_textpos(pdf, x, y);
Once you have positioned the cursor, use the pdf_show ( ) function to draw text there:
pdf_show(pdf, text);
After you call pdf_show( ), the cursor moves to the end of the inserted text.
You can also move the location and draw text in one function, with pdf_show_xy( ):
pdf_show_xy(pdf, text, x, y);
The pdf_continue_text( ) function moves to the next line and outputs text:
pdf_continue_text(pdf, text);
Set the leading parameter with pdf_set_parameter( ) to change the vertical separation between
lines.
The pdf_show_boxed( ) function lets you define a rectangular area within which a string of
text is formatted:
$c = pdf_show_boxed(pdf, text, x, y, width, height, mode [, feature]);
The mode parameter controls the alignment of the text within the box, and can be “left”, “right”,
“center”, “justify”, or “fulljustify”. The difference between “justify” and “fulljustify” is in the
treatment of the last line. The last line in a “justify”-formatted area is not justified, whereas in
a “fulljustify” area it is. Following Example shows all five cases.
Text alignment within a box
<?php
$pdf = pdf_new( );
pdf_open_file($pdf);
pdf_begin_page($pdf,612,792);
$font = pdf_findfont($pdf,”Helvetica-Bold”,”host”,0);
pdf_setfont($pdf,$font,38);
$text = <<<FOO This is a lot of text inside a text box in a small pdf file. FOO; pdf_show_
boxed($pdf, $text, 50, 590, 300, 180, “left”);
pdf_rect($pdf,50,590,300,180);
pdf_stroke($pdf);
pdf_show_boxed($pdf, $text, 50, 400, 300, 180, “right”);
pdf_rect($pdf,50,400,300,180);
pdf_stroke($pdf);
LOVELY PROFESSIONAL UNIVERSITY 293