science-ation/lpdf.php
2005-01-24 18:00:03 +00:00

378 lines
11 KiB
PHP

<?
/*
This file is part of the 'Science Fair In A Box' project
SFIAB Website: http://www.sfiab.ca
Copyright (C) 2005 Sci-Tech Ontario Inc <info@scitechontario.org>
Copyright (C) 2005 James Grant <james@lightbox.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation, version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
?>
<?
class lpdf
{
var $pdf;
var $yloc=10.25;
var $page_header;
var $page_subheader;
var $pagenumber;
var $logoimage;
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'];
//only put the logo on the page if we actually have the logo
if($this->logoimage)
{
//now place the logo image in the top-left-ish
pdf_place_image($this->pdf,$this->logoimage,$this->loc(0.75),$this->loc($this->yloc+.02),.20);
}
//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.20;
//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()
{
if($this->pagenumber>0)
pdf_end_page($this->pdf);
$this->pagenumber++;
//Letter size (8.5 x 11) is 612,792
pdf_begin_page($this->pdf,612,792);
pdf_setlinewidth($this->pdf,0.3);
$this->addHeaderAndFooterToPage();
}
function vspace($space)
{
$this->yloc-=$space;
}
function setFontSize($size)
{
$this->currentFontSize=$size;
pdf_setfont($this->pdf,$this->normalfont,$size);
$leading=round($size*1.3);
pdf_set_value($this->pdf,"leading",$leading);
}
function addText($text,$align="left")
{
$fontsize=pdf_get_value($this->pdf,"fontsize",0);
$lineheight=ceil($fontsize*1.3);
//the line height should be 1.2 * fontsize (approx)
$stringwidth=pdf_stringwidth($this->pdf,$text,$this->normalfont,$fontsize);
$textstr=$text;
$nr=0;
$prevnr=0;
do
{
//echo "textstr=$textstr";
$len=strlen($textstr);
// echo "(lh:$lineheight nr:$nr len:$len)".$textstr;
$nl=false;
//now lets handle a newline at the beginning, just rip it off and move the yloc ourself
while($textstr[0]=="\n")
{
$textstr=substr($textstr,1);
$this->yloc-=$lineheight/72;
$nl=true;
}
if(!$nl)
$this->yloc-=$lineheight/72;
$nr=pdf_show_boxed($this->pdf,$textstr, $this->loc(0.75),$this->loc($this->yloc),$this->loc(7),$lineheight,$align,null);
if($nr==$prevnr)
{
echo "breaking becausae nr==prevnr ($nr==$prevnr) trying to output [$textstr]";
break;
}
$prevnr=$nr;
// printf("x=%f y=%f w=%f h=%f",$this->loc(0.75),$this->loc($this->yloc),$this->loc(7),$lineheight);
// echo "$nr didnt fit";
// echo "<br>doing: substr($textstr,-$nr) <br>";
$textstr=substr($textstr,-$nr);
// echo "nr=$nr";
} while($nr>0);
// pdf_rect($this->pdf,$this->loc(0.75),$this->loc($this->yloc),$this->loc(7),$height);
}
function addTextX($text,$xpos)
{
pdf_show_xy($this->pdf,$text,$this->loc($xpos),$this->loc($this->yloc));
}
function nextLine()
{
$this->yloc-=$this->currentFontSize*1.4/72;
}
function hr()
{
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.25;
}
function hline($x1,$x2)
{
pdf_moveto($this->pdf,$this->loc($x1),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc($x2),$this->loc($this->yloc));
pdf_stroke($this->pdf);
}
function heading($text)
{
pdf_setfont($this->pdf,$this->headerfont,12);
//move down the full line height
$this->yloc-=12/72;
pdf_show_xy($this->pdf,$text,$this->loc(0.5),$this->loc($this->yloc));
pdf_setfont($this->pdf,$this->normalfont,$this->currentFontSize);
//now leave some space under the heading (4 is 1/3 of 12, so 1/3 of the line height we leave)
$this->yloc-=4/72;
}
function addTable($table)
{
$height['tableheader']=0.2;
$height['tabledata']=0.18;
$xpos_of_table=0.5;
$this->yloc-=$height['tableheader'];
$top_of_table=$this->yloc;
$table_width=array_sum($table['widths']);
$table_cols=count($table['header']);
$table_padding=0.03;
//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;
}
}
//now do the data in the table
if($table['data'])
{
pdf_setfont($this->pdf,$this->normalfont,10);
foreach($table['data'] AS $dataline)
{
$this->yloc-=$height['tabledata'];
$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+$table_padding),$this->loc($this->yloc),$this->loc($width-2*$table_padding),$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);
if($this->yloc<1)
{
//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));
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));
pdf_stroke($this->pdf);
$this->newPage();
$this->yloc-=$height['tableheader'];
$top_of_table=$this->yloc;
//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;
}
}
pdf_setfont($this->pdf,$this->normalfont,10);
}
}
}
//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));
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));
pdf_stroke($this->pdf);
}
function output()
{
pdf_end_page($this->pdf);
//only close the image if it was opened to begin with
if($this->logoimage)
pdf_close_image($this->pdf,$this->logoimage);
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,$logo)
{
$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);
if(file_exists($logo))
$this->logoimage=pdf_open_image_file($this->pdf,"gif",$logo,"",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");
$this->page_header=$header;
$this->page_subheader=$subheader;
$this->pagenumber=0;
//add the stuff to the first page
// $this->addHeaderAndFooterToPage();
}
}