PHP to dynamically generate PDF files in Web pages detailed tutorial ____php

Source: Internet
Author: User
Tags php define

We will use the score of the women's dry sliding race to demonstrate the process of dynamically building PDF files. These scores are obtained from the Web and converted to XML. Listing 1 shows a sample XML data file.

Listing 1. XML data ?

1 2 3 4 5 6 7 8 9 10 11 12-13 < events > < event name = "Beast of the East" > < Game score1 = "team1 =" Toronto gore-gore R Ollergirls "team2 =" Montreal La racaille "score2 =" one "> < game Score1 =" "team1 =" Toronto "Death Track Doll S "team2 =" Montreal Les contrabanditas "score2 =" > "</game ></game ></event > ; Event name = "Dustbowl Invitational" > ... </event > < event name = "The Great Yorkshire Showdow n > ... </event > </events >

The root element of XML is a events marker. The data is grouped by event, and each event contains more than one match. Within the events tag, is a series of event tokens in which there are multiple game tags. These game tags contain the names of the two teams participating in the game and their scores in the game.

Listing 2 shows the PHP code to read the XML.

1 2 3 4 5 6 7 8 9 <?php a function getresu LTS () {  $xml = new DOMDocument ()   $xml->load (' Events.xml ');   $events = Array ();   foreach ($xml->getelementsbytagname (' event ') as $event) {   $games = array ();    foreach ($event->getelementsbytagname (' game ') as $game) {&NBSP;&NB sp;  $games []= Array (' team1 ' => $game->getattribute (' team1 '),      ' Score1 ' => $ga Me->getattribute (' score1 '),      ' team2 ' => $game->getattribute (' team2 '),       ' Score2 ' => $game->getattribute (' Score2 ');   }    $events []= Array (' name ' => $event->getattribute (' name '),     ' games ' => $games);  }   return $events; }?>

This script implements a GetResults function to read the XML file into the DOM document. Then use DOM calls to traverse all event and game tags to build an event array. Each element in the series is a hash table that contains the event name and the array of the race item. Structure is essentially a memory version of the XML structure.

To test the role of this script, an HTML export page is built, the GetResults function is used to read the file, and the data is output in a series of HTML tables. Listing 3 shows the PHP code used for the test.

Listing 3. Results HTML page ?

1 2 3 4 5 6 7 8 (9) of the ?? php include_once (' G ") Etresults.php '); $ results = GetResults (); foreach ($results as $event) {?> < H1 ><? php echo ($event [' name '])? >

The Getresults.php,xml data file is uploaded to the WEB server via code, and you can view the HTML results, which is similar to Figure 1.
Figure 1. Contest results in HTML format



In this result, the winning team was used in bold to see which teams won the game.

Build PDF

After you get the data, focus on building the PDF file. The first step is to download the FPDF library and install it in the same directory as the existing application file set. In fact, as long as it's in the PHP library path, you can install it anywhere you like. Track where you place the font directory because you need to set ' Fpdf_fontpath ', as shown in Listing 4.

Listing 4. PDF Hello world?

1 2 3 4 5 6 7 8 9 10 11 <?php define (' Fpdf_fontpath ', '/library/webserver/documents/derby/font/');   Require (' fpdf.php '); $pdf = new FPDF (); $pdf->setfont (' Arial ', ', ', 72); $pdf->addpage (); $pdf->cell (40,10, "Hello world!", 15); $pdf->output ();?>

This script is actually a "Hello world", but it is in PDF format instead of HTML. The first action that this script performs is to use the Define statement to set the location of the FPDF font directory. Then use the Require statement to introduce the FPDF library. This script creates a FPDF object from the library, sets the font, adds a page, and then uses the Cell method to place some text on the page and output the PDF.

Figure 2 shows the results of all normal situations.

Figure 2. Hello World in PDF format



If you do not see a PDF, you may want to run the script at the command line to see if the fpdf.php file is missing or if there are other problems.

Now that the PDF is present, you should merge it with the dry-slip results file and see what can be generated dynamically. Listing 5 shows the first version of the merge operation.

Listing 5. Display the results of the first version of the PDF ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.