1 Media Software Design DIG 3134 Fall 2011 Lecture 18: Excel, PDF J. Michael Moshell University of Central Florida Original image* by Moshell et al.

Post on 23-Dec-2015

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

1

Media Software Design

DIG 3134

Fall 2011

Lecture 18: Excel, PDFJ. Michael Moshell

University of Central Florida

Original image* by Moshell et al .

-2 -

Four Easy(?) Add-ons for PHP

1. Creating Excel (.xls) files

2. Reading Excel files

3. Uploading files

4. Creating PDF documents

-3 -

Creating an Excel file

CREATING Excel:

My method is ONLY useful for downloading

to the browser – NOT for storing a file

directly to disk on the server.

NOTE: This code will NOT work if anything, even

a single character, is emitted by the PHP code

before the 'emitxls' function is called.

-4 -

Creating an Excel file

function emitxls($data)

{

header("Content-type: application/octet-stream");

# from: forge.typo3.org/issues/show/3765

header("Content-Disposition: attachment; filename=output.xls");

header('Pragma: public');

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in past

header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');

header('Cache-Control: no-store, no-cache, must-revalidate');

header('Cache-Control: pre-check=0, post-check=0, max-age=0');

echo $data;

} #emitxls

-5 -

Creating an Excel file

///// MAIN PROGRAM /////

$t="\t"; // Tab character

$n="\n"; // Newline character

$data="Line 1".$t."column 2".$t."column 3".$n;

$data.="Line 2".$t."l2 column 2".$t."l2 column 3".$n;

$data.="Line 3".$t."l3 column 2".$t."l3 column 3".$n;

emitxls($data);

?>

-6 -

A Demo that Doesn't work

Here's one that DOES work: xlsdemo1.php

--------

<?php

// etc

Here's one that DOES NOT work: just add a space at top of xlsdemos.

-------

<?php

//etc

NOTE: The response VARIES from one Apache environment to

another. (Sometimes error message; sometimes text on screen.)

Sometimes hard to notice

the blank line at top

-7 -

Moving on ... how to READ Excel. I use a small library called excel_reader2.php.

It uses PHP Objects, as follows:

$data = new Spreadsheet_Excel_Reader($filename);

// This Spreadsheet_Excel_Reader is a CLASS defined in the excel_reader library.

// Now we use two accessor functions to find out how big the spreadsheet is

$colcnt=$data->colcount();

$rowmax=$data->rowcount();

-8 -

Reading an Excel File (Local) Demonstrate and experiment with xlsdemo2.php

Items to note:

1) This function does NOT ask the user to upload an Excel file.

It works with a file that is local to the PHP code.

So this operation is not 'symmetric' with the code to emit a file,

that I gave you previously.

2) If the file is not found, or if it's defective, you will get a zero

value for 'rowmax'. Note the error message in my

getspreadsheet function.

-9 -

Uploading a file (image) Demonstrate and experiment with upload.php

Items to note:

1) $_FILES is analogous to $_POST or $_GET

i. e. it's a "built-in global array".

2) I've not yet managed to get an error, so haven't seen

"Error Return Code" in action.

3) Functions to understand: end,

in_array,

file_exists,

move_uploaded_file

-10 -

Uploading a file (excel) Demonstrate and experiment with uploadexcel.php

Items to note:

1) I simplified it considerably from upload.php

2) I used !$target to tell if this was the first pass, and to

emit the form if this was the case.

3) This one DELIBERATELY overwrites workfile.xls, because

you will presumably now use the excel_reader2.php

library to pull workfile.xls into your software

for whatever purpose . . . .

-11 -

Producing a PDF File Somewhat similar to Excel production, in that

the PDF can be sent to the browser.

BUT you can also save a copy of the PDF file on your host.

Somewhat similar to the Excel reader, in that

a special library is used.

BUT the library is much bigger and more complex. (18 megabytes)

Tour the tcpdf website at

www.tcpdf.org

-12 -

PDF Example 1There are many examples at

http://www.tcpdf.org/examples.php

See my pdfdemo1.txt

Example 1 uses the writeHTMLcell( ) function, which

allows you to use HTML to format your text.

BUT HTML has substantial limits for formatting!

(e. g. font control.)

-13 -

PDF Example 2

Read the code.

Understand these methods:

$pdf->SetFont()

$pdf->AddPage();

$pdf->Text()

$pdf->Line()

$pdf->Output()

-14 -

The Output method

Send the document to a given destination

void Output ([string $name = ''], [string $dest = ''])

string $name: The name of the file. If not given, the document will be sent to the browser (destination I) with the name doc.pdf.

string $dest: Destination where to send the document. It can take one of the following values:

I: send the file inline to the browser

D: send to the browser and force a file download with the name given by name.

F: save to a local file with the name given by name.

-15 -

cURL

• When a PHP program needs to

invoke another Internet site• client URL Library supports

http, https

ftp, gopher, telnet, dict, file, ldap

proxies, cookies, user-password authentication

-16 - -16 -

cURL

• Demonstrate cURL:-mograb.php =

A PHP program that reads another website

and "screen scrapes" some information.

-17 - -17 -

cURL in action

Browser

The mograb.php

'scraper' program

Program sends an http

message via CURL

The scraped

Site "goldprice.html"

Access a URL

to run PHP program

-18 - -18 - -18 -

cURL in action

Browser

The mograb.php

'scraper' program

Program sends an http

message via CURL

The scraped

Site "goldprice.html"

Access a URL

to run PHP programServer

Returns the text

Of goldprice.html

CURL sends reply

to calling program

-19 - -19 - -19 -

cURL in action

Browser

The mograb.php

'scraper' program

Program sends an http

message via CURL

The scraped

Site "goldprice.html"

Access a URL

to run PHP programServer

Returns the text

Of goldprice.html

CURL sends reply

to calling programMograb creates html

to deliver the scraped information

Plus whatever else you want

To tell your user.

-20 - -20 -

cURL demo 2

• Demonstrate cURL:-Mograb2.php

This just shows that cURL can call a website that

is not just in localhost (namely, www.microsoft.com)

-21 - -21 -

cURL demo 3

• Demonstrate memcheck.php

A little "fake" membership checking site.

12345 -> Joe Wilson

67890 -> Mary Lebone

anything else ->NOT a member

-Mograb3.php

-22 - -22 -

Summary of Lecture

Emit XLS: Just a header and some tabs and newlines

Read XLS: A small (free) library of PHP code

Upload files: <form enctype="multipart/form-data">

Write PDF: A large (free) library with many capabilities

Link to other websites:

A built-in PHP capability called cURL

-23 - -23 -

PROJECT 3 – Clean-Up!

If you DO NOT SEE PROJECT 3 in the

grades-ranked.xls chart

-24 - -24 -

PROJECT 3 – Clean-Up!

If you DO NOT SEE PROJECT 3 in the

grades-ranked.xls chart

and if you

DID NOT GET E-MAIL FROM ME

(about your project)

-25 - -25 -

PROJECT 3 – Clean-Up!

If you DO NOT SEE PROJECT 3 in the

grades-ranked.xls chart

and if you

DID NOT GET E-MAIL FROM ME

(about your project)

We must fix it TODAY or it's gone ---

time to focus on Project 4 and Final Exam

top related