home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Programming PHPProgramming PHPSearch this book

10.5. Navigation

PDF provides several navigation features for PDF files. Bookmarks function as a table of contents for the document, and you can provide viewers with thumbnail images indicating what's at the other end of each bookmark. In addition, any part of a PDF page can be linked to another part of the current PDF file, another PDF file, or a completely different file.

10.5.1. Bookmarks and Thumbnails

Bookmarks make it easy to quickly navigate through long PDF documents. You can create a bookmark with the pdf_add_bookmark( ) function, which returns a bookmark handle:

$bookmark = pdf_add_bookmark(pdf, text, parent, open);

The text parameter is the label that the user sees. To create a nested menu of bookmarks, pass a bookmark handle as the parent option. The current location in the PDF file (as it is being created) is the destination of the bookmark.

Bookmarks can have thumbnails associated with them. To make a thumbnail, load an image and call pdf_add_thumbnail( ):

pdf_add_thumbnail(pdf, image);

Example 10-12 creates a top-level bookmark named "Countries" and nests two bookmarks, "France" and "New Zealand", under the "Countries" bookmark. It also creates a representative thumbnail image for each page. These thumbnails can be viewed in Acrobat Reader's thumbnail panel.

Example 10-12. Using bookmarks and thumbnails

<?php
 $p = pdf_new( );
 pdf_open_file($p);

 pdf_begin_page($p,595,842);
 $top = pdf_add_bookmark($p, "Countries");
 $im = pdf_open_png($p, "fr-flag.png");
 pdf_add_thumbnail($p, $im);
 pdf_close_image($p,$im);
 $font = pdf_findfont($p,"Helvetica-Bold","host",0);
 pdf_setfont($p, $font, 20);
 pdf_add_bookmark($p, "France", $top);
 pdf_show_xy($p, "This is a page about France", 50, 800);
 pdf_end_page($p);

 pdf_begin_page($p,595,842);
 $im = pdf_open_png($p, "nz-flag.png");
 pdf_add_thumbnail($p, $im);
 pdf_close_image($p,$im);
 pdf_setfont($p, $font, 20);
 pdf_add_bookmark($p, "Denmark", $top);
 pdf_show_xy($p, "This is a page about New Zealand", 50, 800);
 pdf_end_page($p);

 pdf_close($p);
 $buf = pdf_get_buffer($p);
 $len = strlen($buf);
 header("Content-Type:application/pdf");
 header("Content-Length:$len");
 header("Content-Disposition:inline; filename=bm.pdf");
 echo $buf;
 pdf_delete($p);
?>

The thumbnails generated by Example 10-12 are shown in Figure 10-14.

Figure 10-14

Figure 10-14. Thumbnails

10.5.2. Links

pdflib supports functions that specify a region on a page that, when clicked on, takes the reader somewhere else. The destination can be either another part of the same document, another PDF document, some other application, or a web site.

The pdf_add_locallink( ) function adds a local link to another place within the current PDF file:

pdf_add_locallink(pdf, llx, lly, urx, ury, page, zoom);

All links in PDF files are rectangular. The lower-left coordinate is (urx,ury) and the upper-right coordinate is (urx,ury). Valid zoom values are "retain", "fitpage", "fitwidth", "fitheight", and "fitbbox".

The following call defines a 50 x 50 area that, if clicked, takes the reader to page 3 and retains the current zoom level:

pdf_add_locallink($p, 50, 700, 100, 750, 3, "retain");

The pdf_add_pdflink( ) function adds a link to another PDF file. It takes the same parameters as the pdf_add_locallink( ) function, with the addition of a new parameter containing the filename to link to:

pdf_add_pdflink(pdf, llx, lly, urx, ury, filename, page, zoom);

For example:

pdf_add_pdflink($p, 50, 700, 100, 750, "another.pdf", 3, "retain");

The pdf_add_launchlink( ) function adds a link to another file, whose MIME type causes the appropriate program to be launched to view the file:

pdf_add_launchlink($p, 50, 700, 100, 750, "/path/document.doc");

The pdf_add_weblink( ) function creates a link whose destination is a URL:

pdf_add_weblink(pdf, llx, lly, urx, ury, url);

Example 10-13 takes an image, figures out its size, puts it at position (50,700) in the document, then adds a weblink such that if you click anywhere on the image you end up at http://www.php.net. The pdf_set_border_style( ) call, with a line width of 0, gets rid of the box that would otherwise be drawn around the image.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.