This tutorial is intended for the PHP programmer who needs to incorporate PDF generation in a script without using external libraries such as PDFlib (often unavailable due to licensing restrictions or lack of funds). This tutorial is the second of two parts, and builds on what was covered in the first part. Therefore, if you have not yet gone through Part 1, you are advised to do so (or at least read through it), before going through this tutorial (Part 2). Apart from what was dealt with in Part 1, no knowledge of PDF file structure is required to understand this tutorial, as all references are explained.
The new example script below includes the added functions. It sets different colors, draws some lines, a rectangle and a circle, and uploads an image. Note how we can use line drawing to emulate underline, which is not natively available in the PDF syntax.
<?php require 'PDF.php'; // Require the class. $pdf = &PDF::factory('p', 'a4'); // Set up the pdf object. $pdf->open(); // Start the document. $pdf->setCompression(true); // Activate compression. $pdf->addPage(); // Start a page. $pdf->setFont('Courier', '', 8); // Set font to courier 8 pt. $pdf->text(100, 100, 'First page'); // Text at x=100 and y=100. $pdf->setFontSize(20); // Set font size to 20 pt. $pdf->setFillColor('rgb', 1, 0, 0); // Set text color to red. $pdf->text(100, 200, 'HELLO WORLD!'); // Text at x=100 and y=200. $pdf->setDrawColor('rgb', 0, 0, 1); // Set draw color to blue. $pdf->line(100, 202, 240, 202); // Draw a line. $pdf->setFillColor('rgb', 1, 1, 0); // Set fill/text to yellow. $pdf->rect(200, 300, 100, 100, 'fd'); // Draw a filled rectangle. $pdf->addPage(); // Add a new page. $pdf->setFont('Arial', 'BI', 12); // Set font to arial bold // italic 12 pt. $pdf->text(100, 100, 'Second page'); // Text at x=100 and y=100. $pdf->image('sample.jpg', 50, 200); // Image at x=50 and y=200. $pdf->setLineWidth(4); // Set line width to 4 pt. $pdf->circle(200, 300, 150, 'd'); // Draw a non-filled // circle. $pdf->output('foo.pdf'); // Output the file named foo.pdf ? >