Text

Text is the heart of a PDF file. As such, there are many options for changing the appearance and layout of text. In this section, we’ll discuss the coordinate system used in PDF documents, functions for inserting text and changing text attributes, and font usage.

Coordinates

The origin ((0,0)) in a PDF document is in the bottom-left corner. All of the measurements are specified in DTP points. A DTP point is equal to 1/72 of an inch, or 0.35277777778 mm.

Example 10-2 puts text in the corners and center of a page.

Example 10-2. Demonstrating coordinates

<?php
 $pdf = pdf_new(  );
 pdf_open_file($pdf);
 pdf_set_info($pdf,"Creator","coords.php");
 pdf_set_info($pdf,"Author","Rasmus Lerdorf");
 pdf_set_info($pdf,"Title","Coordinate Test (PHP)");
 pdf_begin_page($pdf,612,792);

 $font = pdf_findfont($pdf,"Helvetica-Bold","host",0);
 pdf_setfont($pdf,$font,38.0);
 pdf_show_xy($pdf, "Bottom Left", 10, 10);
 pdf_show_xy($pdf, "Bottom Right", 350, 10);
 pdf_show_xy($pdf, "Top Left", 10, 752);
 pdf_show_xy($pdf, "Top Right", 420, 752);
 pdf_show_xy($pdf, "Center",612/2-60,792/2-20);

 pdf_end_page($pdf);
 pdf_set_parameter($pdf, "openaction", "fitpage");
 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=coords.pdf");
 echo $buf;
 pdf_delete($pdf);
?>

The output of Example 10-2 is shown in Figure 10-2.

Figure 10-2. Coordinate demo output

It can be inconvenient to use ...

Get Programming PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.