bool pdf_add_note ( resource pdfdoc, float lowerleft_y, float lowerleft_y, float upperright_x, float upperright_y, string contents, string title, string icon, int open)
bool pdf_set_info ( resource pdfdoc, string key, string value)
Owing to the fact that PDFs are designed to be read like normal, printed documents, Adobe have incorporated into the PDF format the ability to add notes in the same manner one might scribble in a margin.
These notes, which can be edited and re-edited by readers, can also be created using PHP by calling the function pdf_add_note(). Here is an example of use:
pdf_add_note($pdf, 100, 500, 700, 600, "You can create notes very easily using pdf_add_note() ", "Sticky notes", "note", 1);
The second, third, fourth, and fifth parameters are respectively the lower-left X and lower-left Y co-ordinates, and the upper-right X and upper-right Y co-ordinates of the note boundaries. The sixth and seventh parameters are the text to put inside the note and the title to place at the top, and the final two parameters decide the icon used to display the note when closed and whether or not the note starts open. Note that once the PDF is loaded, your reader is often free to move these notes around and edit the text inside them.
So, in the line above, we add a 600x100 note box which is already open (use 1 to specify the note is open, and 0 to specify it is closed). Instead of "note" as the penultimate parameter, we have various other options: comment, insert, paragraph, newparagraph, key, or help. In several PDF readers, this parameter has no effect and can be just left as "note".
Another important facet to improving the usefulness of documents is to provide meta-data regarding who created the document and when. This can be achieved through the use of pdf_set_info(), which takes a key and a value as its second and third parameters. The standard keys for use as "Subject", "Title", "Creator", "Author", and "Keywords", but you are also able to add your own keys such as "Modified", "Created", etc.
Now we can finish off our script by adding in some meta-data - add these three lines just below pdf_open_file() and save the modified version as pdf5.php:
pdf_set_info($pdf, "Creator", "TelRev"); pdf_set_info($pdf, "Title", "PHP PDF 101"); pdf_set_info($pdf, "MyInfo", "You can write what you please here");
When you read the PDF generated by pdf5.php you should see the note sticking out quite obviously. The meta-data will be there too, but it is likely to be hidden away under a menu somewhere.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: PDF Conclusion >>
Previous chapter: PDF special effects
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.