science-ation/lpdf.php
james 0e10fcda45 factor out PDF code into a separate PDF class
implement new PDF class in reports_checkin
2005-01-13 22:00:34 +00:00

186 lines
5.4 KiB
PHP

<?
class lpdf
{
var $pdf;
var $yloc=10.25;
var $page_header;
var $page_subheader;
var $pagenumber;
function loc($inch)
{
return $inch*72;
}
function addHeaderAndFooterToPage()
{
//The title of the fair
$this->yloc=10.25;
$height['title']=0.25;
$height['subtitle']=0.22;
pdf_setfont($this->pdf,$this->headerfont,18);
pdf_show_boxed($this->pdf,$this->page_header,$this->loc(0.75),$this->loc($this->yloc),$this->loc(7),$this->loc($height['title']),"center",null);
$this->yloc-=$height['title'];
pdf_setfont($this->pdf,$this->headerfont,14);
pdf_show_boxed($this->pdf,$this->page_subheader,$this->loc(0.75),$this->loc($this->yloc),$this->loc(7),$this->loc($height['subtitle']),"center",null);
$this->yloc-=$height['subtitle'];
//header line
pdf_moveto($this->pdf,$this->loc(0.5),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc(8),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$this->yloc-=0.5;
//now put a nice little footer at the bottom
$footertext=date("Y-m-d h:ia")." - ".$this->page_header." - ".$this->page_subheader;
$footerwidth=pdf_stringwidth($this->pdf,$footertext,$this->normalfont,9);
pdf_setfont($this->pdf,$this->normalfont,9);
pdf_show_xy($this->pdf,$footertext,$this->loc(4.25)-$footerwidth/2,$this->loc(0.5));
//footer line
pdf_moveto($this->pdf,$this->loc(0.5),$this->loc(0.7));
pdf_lineto($this->pdf,$this->loc(8.0),$this->loc(0.7));
pdf_stroke($this->pdf);
}
function newPage()
{
$this->pagenumber++;
pdf_end_page($this->pdf);
//Letter size (8.5 x 11) is 612,792
pdf_begin_page($this->pdf,612,792);
$this->addHeaderAndFooterToPage();
}
function addTable($table)
{
$height['tableheader']=0.2;
$height['tabledata']=0.18;
$xpos_of_table=0.5;
$top_of_table=$this->yloc;
$table_width=array_sum($table['widths']);
$table_cols=count($table['header']);
//draw the top line of the table (above the table header)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc+$height['tableheader']));
pdf_stroke($this->pdf);
//draw the top line of the table (below the table header)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc));
pdf_stroke($this->pdf);
//do the header first
if($table['header'])
{
pdf_setfont($this->pdf,$this->headerfont,12);
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$head=$table['header'][$c];
$width=$table['widths'][$c];
pdf_show_boxed($this->pdf,$head,$this->loc($xpos),$this->loc($this->yloc),$this->loc($width),$this->loc($height['tableheader']),"center",null);
$xpos+=$width;
}
$this->yloc-=$height['tableheader'];
}
//now do the data in the table
if($table['data'])
{
pdf_setfont($this->pdf,$this->normalfont,10);
foreach($table['data'] AS $dataline)
{
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$width=$table['widths'][$c];
pdf_show_boxed($this->pdf,$dataline[$c],$this->loc($xpos),$this->loc($this->yloc),$this->loc($width),$this->loc($height['tabledata']),$table['dataalign'][$c],null);
$xpos+=$width;
}
//draw the line below the table data)
pdf_moveto($this->pdf,$this->loc($xpos_of_table),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc($xpos_of_table+$table_width),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$this->yloc-=$height['tabledata'];
}
}
//now draw all the vertical lines
$xpos=$xpos_of_table;
for($c=0;$c<$table_cols;$c++)
{
$width=$table['widths'][$c];
//draw the line below the table data)
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc+$height['tableheader']));
pdf_stroke($this->pdf);
$xpos+=$width;
}
//and the final line on the right side of the table:
pdf_moveto($this->pdf,$this->loc($xpos),$this->loc($top_of_table+$height['tableheader']));
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc+$height['tableheader']));
pdf_stroke($this->pdf);
}
function output()
{
pdf_end_page($this->pdf);
pdf_close($this->pdf);
$pdfdata=pdf_get_buffer($this->pdf);
header("Content-type: application/pdf");
header("Content-disposition: inline; filename=sfiab_".$this->page_subtitle.".pdf");
header("Content-length: ".strlen($pdfdata));
echo $pdfdata;
}
function lpdf($header,$subheader)
{
$this->pdf=pdf_new();
pdf_open_file($this->pdf,null);
//open up the first page
//Letter size (8.5 x 11) is 612,792
pdf_begin_page($this->pdf,612,792);
// pdf_set_parameter($this->pdf, "FontOutline", "Arial=/home/sfiab/www.sfiab.ca/sfiab/arial.ttf");
//$arial=pdf_findfont($this->pdf,"Arial","host",1);
$this->normalfont=pdf_findfont($this->pdf,"Times-Roman","host",0);
$this->headerfont=pdf_findfont($this->pdf,"Times-Bold","host",0);
pdf_set_info($this->pdf,"Author","SFIAB");
pdf_set_info($this->pdf,"Creator","SFIAB");
pdf_set_info($this->pdf,"Title","SFIAB - $subheader");
pdf_set_info($this->pdf,"Subject","$subheader");
pdf_setlinewidth($this->pdf,0.3);
$this->page_header=$header;
$this->page_subheader=$subheader;
//add the stuff to the first page
$this->addHeaderAndFooterToPage();
}
}