1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P152 pptx

5 130 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 60,33 KB

Nội dung

727 Solution Overview Here we are setting a creator, author, and title. Note that all six info fields are optional. pdf_set_info($pdf, 'Creator', 'pdftest.php'); pdf_set_info($pdf, 'Author', 'Luke Welling and Laura Thomson'); pdf_set_info($pdf, 'Title', 'Hello World (PHP)'); A PDF document consists of a number of pages.To start a new page, we need to call pdf_begin_page(). As well as the identifier returned by pdf_open(), pdf_begin_page() requires the dimensions of the page. Each page in a document can be a different size, but unless you have a good reason not to, you should use a common paper size. PDFlib works in points, both for page size, and for locating coordinate locations on each page. For reference,A4 is approximately 595 by 842 points and U.S. letter paper is 612 by 792 points.This means that our line pdf_begin_page($pdf, 8.5*72, 11*72); creates a page in our document, sized for U.S. letter paper. A PDF document does not need to be just a printable document. Many PDF features can be included in the document such as hyperlinks and bookmarks.The function pdf_add_outline() will add a bookmark to the document outline.The bookmarks in a document will appear in a separate pane in Acrobat Reader, allowing us to skip straight to important sections. This line pdf_add_outline($pdf, 'Page 1'); adds an outline entry labeled Page 1, which will refer to the current page. Fonts available on systems vary from operating system to operating system and even from individual machine to machine. In order to guarantee consistent results, a set of core fonts will work with every PDF reader.The 14 core fonts are n Courier n Courier-Bold n Courier-Oblique n Courier-BoldOblique n Helvetica n Helvetica-Bold n Helvetica-Oblique n Helvetica-BoldOblique n Times-Roman n Times-Bold n Times-Italic n Times-BoldItalic 36 525x ch30 1/24/03 3:40 PM Page 727 728 Chapter 30 Generating Personalized Documents in Portable Document Format (PDF) n Symbol n ZapfDingbats Fonts outside this set can be embedded in documents, but this will increase the file size and might not be acceptable under whatever license you own that particular font under. We can choose a font, its size, and character encoding as follows: $font = pdf_findfont($pdf, 'Times-Roman', 'host', 0); pdf_setfont($pdf, $font, 24); Font sizes are specified in points.We have chosen host character encoding.The allowable values are winansi, builtin, macroman, ebcdic, or host.The meanings of the different val- ues are as follows: n winansi—ISO 8859-1 plus special characters added by Microsoft such as a Euro symbol. n macroman—Mac Roman encoding.The default Macintosh character set. n ebcdic—EBCDIC as used on IBM AS/400 systems. n builtin—Use the encoding built-in to the font. Normally used with non-Latin fonts and symbols. n host—Automatically selects macroman on a Mac, ebcdic on an EBCDIC-based system, and winansi on all other systems. If you do not need to include special characters, the choice of encoding is not impor- tant. A PDF document is not like an HTML document or a word processor document. Text does not by default start at the top left and flow onto other lines as required.We need to choose where to place each line of text.As already mentioned, PDF uses points to specify locations.The origin (the x, y coordinate [0, 0]) is at the bottom left corner of the page. Given that our page is 612 by 792 points, the point (50, 700) is about two thirds of an inch from the left of the page and about one and one third inches from the top.To set our text position at this point, we use pdf_set_text_pos($pdf, 50, 700); Finally, having set up the page, we can write some text on it.To add text at the current position using the current font, we use pdf_show(). The line pdf_show($pdf,'Hello,world!'); adds the test "Hello World!" to our document. To move to the next line and write more text, we use pdf_continue_text().To add the string "(says PHP)",we use pdf_continue_text($pdf,'(says PHP)'); 36 525x ch30 1/24/03 3:40 PM Page 728 729 Solution Overview The exact location where this will appear will depend on the font and size selected. If rather than lines or phrases you are using contiguous paragraphs, you might find the function pdf_show_boxed() more useful. It allows you to declare a text box and flow text into it. When we have finished adding elements to a page, we need to call pdf_end_page() as follows: pdf_end_page($pdf); When we have finished the whole PDF document, we need to close it using pdf_close().When we are generating a file, we also need to close the file. The lines pdf_close($pdf); fclose($fp); complete the generation of our Hello World document. All we need to do is provide a way to download it. echo 'download the pdf <a href="hello.pdf">here</a>'; This example was derived from the C language example in the PDFlib documentation and should provide a starting point. The document we want to produce for the certificate is more complicated, including a border, a vector image, and a bitmap image.With the other two techniques, we added these features using our word processor.With PDFlib, we must add them manually. Generating Our Certificate with PDFlib In order to use PDFlib, we have chosen to make some compromises. Although it is almost certainly possible to exactly duplicate the certificate we used previously, a lot more effort would be required to generate and position each element manually rather than using a tool such as Microsoft Word to help lay out the document. We are using the same text as before, including the red rosette and the bitmap signa- ture, but we are not going to duplicate the complex border. The complete code for this script is shown in Listing 30.6. Listing 30.6 pdflib.php—Generating Our Certificate Using PDFlib <?php // create short variable names $name = $HTTP_POST_VARS['name']; $score = $HTTP_POST_VARS['score']; if(!$name||!$score) { echo '<h1>Error:</h1>This page was called incorrectly'; exit; } else 36 525x ch30 1/24/03 3:40 PM Page 729 730 Chapter 30 Generating Personalized Documents in Portable Document Format (PDF) { $date = date( 'F d, Y' ); // create a pdf document in memory $pdf = pdf_new(); pdf_open_file($pdf); // set up name of font for later use $fontname = 'Times-Roman'; // set up the page size in points and create page // US letter is 11" x 8.5" and there are approximately 72 points per inch $width = 11*72; $height = 8.5*72; pdf_begin_page($pdf, $width, $height); // draw our borders $inset = 20; // space between border and page edge $border = 10; // width of main border line $inner = 2; // gap within the border //draw outer border pdf_rect($pdf, $inset-$inner, $inset-$inner, $width-2*($inset-$inner), $height-2*($inset-$inner)); pdf_stroke($pdf); //draw main border $border points wide pdf_setlinewidth($pdf, $border); pdf_rect($pdf, $inset+$border/2, $inset+$border/2, $width-2*($inset+$border/2), $height-2*($inset+$border/2)); pdf_stroke($pdf); pdf_setlinewidth($pdf, 1.0); // draw inner border pdf_rect($pdf, $inset+$border+$inner, $inset+$border+$inner, $width-2*($inset+$border+$inner), $height-2*($inset+$border+$inner)); pdf_stroke($pdf); // add heading $font = pdf_findfont($pdf, $fontname, 'host', 0); Listing 30.6 Continued 36 525x ch30 1/24/03 3:40 PM Page 730 731 Solution Overview if ($font) pdf_setfont($pdf, $font, 48); $startx = ($width - pdf_stringwidth($pdf, 'PHP Certification'))/2; pdf_show_xy($pdf, 'PHP Certification', $startx, 490); // add text $font = pdf_findfont($pdf, $fontname, 'host', 0); if ($font) pdf_setfont($pdf, $font, 26); $startx = 70; pdf_show_xy($pdf, 'This is to certify that:', $startx, 430); pdf_show_xy($pdf, strtoupper($name), $startx+90, 391); $font = pdf_findfont($pdf, $fontname, 'host', 0); if ($font) pdf_setfont($pdf, $font, 20); pdf_show_xy($pdf, 'has demonstrated that they are certifiable '. 'by passing a rigorous exam', $startx, 340); pdf_show_xy($pdf, 'consisting of three multiple choice questions.', $startx, 310); pdf_show_xy($pdf, "$name obtained a score of $score".'%.', $startx, 260); pdf_show_xy($pdf, 'The test was set and overseen by the ', $startx, 210); pdf_show_xy($pdf, 'Fictional Institute of PHP Certification', $startx, 180); pdf_show_xy($pdf, "on $date.", $startx, 150); pdf_show_xy($pdf, 'Authorised by:', $startx, 100); // add bitmap signature image $signature = pdf_open_image_file($pdf, 'png', 'signature.png'); pdf_place_image($pdf, $signature, 200, 75, 1); pdf_close_image($pdf, $signature); // set up colors for rosette pdf_setrgbcolor_fill($pdf, 0, 0, .4); //dark blue pdf_setrgbcolor_stroke($pdf, 0, 0, 0); // black // draw ribbon 1 pdf_moveto($pdf, 630, 150); pdf_lineto($pdf, 610, 55); pdf_lineto($pdf, 632, 69); pdf_lineto($pdf, 646, 49); pdf_lineto($pdf, 666, 150); pdf_closepath($pdf); Listing 30.6 Continued 36 525x ch30 1/24/03 3:40 PM Page 731 . are n Courier n Courier-Bold n Courier-Oblique n Courier-BoldOblique n Helvetica n Helvetica-Bold n Helvetica-Oblique n Helvetica-BoldOblique n Times-Roman n Times-Bold n Times-Italic n Times-BoldItalic 36 525x. systems. n builtin—Use the encoding built-in to the font. Normally used with non-Latin fonts and symbols. n host—Automatically selects macroman on a Mac, ebcdic on an EBCDIC-based system, and winansi on all other. between border and page edge $border = 10; // width of main border line $inner = 2; // gap within the border //draw outer border pdf_rect($pdf, $inset-$inner, $inset-$inner, $width-2*($inset-$inner), $height-2*($inset-$inner)); pdf_stroke($pdf); //draw

Ngày đăng: 07/07/2014, 03:20