#native_company# #native_desc#
#native_cta#

PHP for PDF Page 3

By Luca Perugini
on October 26, 2000

Distilling PDF document

Now we’re ready to generate PDF on the fly!
In this little example we’ll generate brochure on demand for FlyStore Inc.,
taking data from a catalogue database.

Prepare Database Test

I am going to assume a *little* experience in databases. Having said that, I’m
really only hoping that you know how to create a database and insert tables
into that database.
Creating table catalogue:
create table catalogue(
  id           smallint(8) unsigned DEFAULT '0' NOT NULL,
  item         varchar(100)         DEFAULT ''  NOT NULL,
  description  tinytext,
  img_data     longblob,
  imgname      varchar(60),
  imgsize      varchar(60),
  imgtype      varchar(60),
  price        smallint(8) unsigned DEFAULT '0' NOT NULL,
  PRIMARY      KEY (id),
  KEY          item (item(20))
);

Sending MIME Header Information

In order to have our document show up correctly, we need to
send correct header information to the user’s browser.
With PHP we can do that using the header function.
The following code sends the correct MIME type to the browser.
header( "Content-type: application/pdf" );
header( "Content-Disposition: attachment; filename=modulo.pdf" ); 
header( "Content-Description: PHP3 Generated Data" );

Important Notes

What you need to know is that you must not send anything before header output.
A common mistake is the presence of spaces at the beginning of the file.

Getting Data From MySQL

Here we’ve got a snapshot of code for retrieving data from the catalogue database.

<?php

  $link mysql_connect ("127.0.0.1""flyadm""flystore")

     or die (
"Could not connect");

  mysql_select_db ("flystore"$link);

  $result mysql_query ("SELECT * FROM catalogue"$link)

     or die (
"Invalid query");

  $data mysql_fetch_row ($result);

 ....

 ....

  
mysql_close ($link);

?>