package pdftest;

use strict;
use English;
use CGI qw(:all);

my ($curr, $status, $message);

sub pdftest
{
# This call to CGI initialises all the required variables
	
  $curr = CGI->new();

# This generates the HTML output for the test display page
    
  my $text1 = param("SUBMIT");
    
  print header();
  print start_html(-TITLE=>'Hello Trevor',-BGCOLOR=>'228b22',-TEXT=>'FFFFFF',-ALINK=>'FFFFFF',
                   -VLINK=>'FFFFFF');
  print center();
  print "\n\n";
  print h1($text1);
  print "\n\n";
  print br();

# This call to PDFREP generates the first line of data for the pdf file
# This should be called to generate the heading line once per output file
# It will open the output file with the given name and the given directory.
# It returns a status flag = 1 succesful and 0 failure and a message text.
# The .pdf file extension is added automatically to the output file
# It also opens the temporary file for page data which is the same name and directory
# but the extension is .tmp. So make sure there is enough room in the area you are
# working in. The tmp file is deleted at the end of run or in case of an error.

# All sections of PDFREP return a status flag and error message this is so that the Package
# will not fall over and leave the error coding to the individual programmer.
# Nice open source let you do what you want huh.

  my $filenam = "pdftest1";
  my $filedir = "../";
  
  ($status, $message) = PDFREP->heading($filenam, $filedir);

# This is the error checking if failed files have to be closed and deleted by the PDFREP Package.
# This is done by calling the crashed part of the package with no parameters

  if (!$status)
  {
      print "<BR><BR><h4>PDF FILE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }
  
# The next item to setup in generating the pdf file is the fonts used throughout the document.
# This is done by calling FONTSET with two parameters.
# 1 = The font reference name - this will be used when writing out the data
# 2 = The font to be used i.e. Arial or Helvetica etc.
#
# For this example two fonts will be used as per the example above.

  ($status, $message) = PDFREP->fontset('F1','Arial');
  
  if (!$status)
  {
      print "<BR><BR><h4>PDF FONT CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }
  
  ($status, $message) = PDFREP->fontset('F2','Helvetica-Bold');
  
  if (!$status)
  {
      print "<BR><BR><h4>PDF FONT CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

# So now we have the PDF file opened and ready for input the fonts have been specified so now lets
# start writing the data. OH Joy OH Rapture.
# This is done by calling the Pagedata section of PDFREP. This section has various parameters
# 1 The type of data passed
# np = new page
# nl = new line
# You specify one of these as the first parameter passed to identify what your upto.
# Data is written out one line at a time so try not to exceed the characters.
# Always start with new page.

# The other parameters passed are
# 2  = The column offset . This is the amount of characters from the left hand column of the line.
# 3  = The font size to be used with the font set later. It is the text point size standard is 12
# 4  = The name of the font you want to use as previously set
# 5  = The size of the font on the next data line to allow for correct data throws.
# 6  = Italic 0 = No 1 = yes > 1 get some weird results.
# 7  = Red colour
# 8  = Green colour
# 9  = Blue colour 0 0 0 = black 1 1 1 = white
# 10 = The data. This is the textual data you require.

# For a new page line there are two extra parameters required page size and orientation. The document
# must have the same all the way through

# 11 = Page Size LE = letter A4 = A4
# 12 = Page Orientation PO = Portrait LA = Landscape

# Leaving these blank defaults to Letter Portrait

# Again it is a good idea to check the status after every line of data output just in case an error
# occurs like run out of disc space. OK in this example its a bit of a pain but thats life normally
# this would be a nice loop.

  ($status, $message) = PDFREP->pagedata('np','0','12','F2','12','0','0','0','0','This is the first bit of text','LE','LA');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW PAGE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the second bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the third bit of data');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the four bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F2','12','0','0','0','0','This is the five bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the six bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the seven bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the eight bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the ninth bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the tenth bit of text');
  
  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('np','0','12','F2','12','0','0','0','0','This is the first bit of text','LE','LA');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW PAGE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the second bit of text');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

  ($status, $message) = PDFREP->pagedata('nl','0','12','F1','12','0','0','0','0','This is the third bit of data');

  if (!$status)
  {
      print "<BR><BR><h4>PDF NEW DATA LINE CREATION FAILED </h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }

# OK so are all the data lines and pages written now. Hope so because it's time to write the
# PDF file. This might take a minute because it's playing with reading the temporary file and
# outputting this to the PDF File, It will also build the page cross reference list and all
# the required trailer records just for fun or to make it easier for the programmer to remember what
# is required when producing a PDF like the data haha.
# So you think all the parameters are over well think again
# 1 = Page size LE = Letter and A4 = A4 defaults to letter
# 2 = Page orientation PO = Portrait and LA = Landscape

  ($status, $message) = PDFREP->writepdf('LE','LA');

  if (!$status)
  {
      print "$status - $message<BR>\n";
      print "<BR><BR><h4>PDF WRITE THE PDF FILE FAILED</h4>- $message<BR><BR>\n";
      ($status, $message) = PDFREP->crashed();
      print "$status - $message<BR>\n";
  }
  else
  {
      print "$status - $message<BR>\n";
  }
  
  my $fileout = $filedir . $filenam . ".pdf";
  
  print br();
  print "\n\n";
  print ("<A href=$fileout>");
  print ("<FONT COLOR='000000'><B>");
  print ("PDF file is ", $filenam);
  print ("</B></FONT>");
  print ("</A>");
  print "\n\n";
  
  print end_html();  
  print "\n\n";
  
}
1;