Update PDF to handle any size page (default 8.5x11) to use a different size, simply call

newPage(width,height) in inches.
This commit is contained in:
james 2005-04-22 14:04:47 +00:00
parent b7b9064810
commit 13bc47708a

View File

@ -31,6 +31,14 @@ class lpdf
var $pagenumber;
var $logoimage;
var $page_margin=0.75;
//these are defaults, they get overwritten with the first call to newPage(width,height)
var $page_width=8.5;
var $page_height=11;
var $content_width=$page_width-(2*$page_margin);
function loc($inch)
{
return $inch*72;
@ -40,28 +48,28 @@ class lpdf
function addHeaderAndFooterToPage()
{
//The title of the fair
$this->yloc=10.25;
$this->yloc=$this->page_height-$this->page_margin;
$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);
pdf_show_boxed($this->pdf,$this->page_header,$this->loc($this->page_margin),$this->loc($this->yloc),$this->loc($this->content_width),$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);
pdf_show_boxed($this->pdf,$this->page_subheader,$this->loc($this->page_margin),$this->loc($this->yloc),$this->loc($this->content_width),$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);
pdf_place_image($this->pdf,$this->logoimage,$this->loc($this->page_margin),$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_moveto($this->pdf,$this->loc($this->page_margin-0.25),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc($this->page_width-$this->page_margin+0.25),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$this->yloc-=0.20;
@ -70,22 +78,29 @@ class lpdf
$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));
pdf_show_xy($this->pdf,$footertext,$this->loc($this->page_width/2)-$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_moveto($this->pdf,$this->loc($this->page_margin-0.25),$this->loc($this->page_margin-0.05));
pdf_lineto($this->pdf,$this->loc($this->page_width-$this->page_margin+0.25),$this->loc($this->page_margin-0.05));
pdf_stroke($this->pdf);
}
function newPage()
function newPage($width="",$height="")
{
if($width && $height)
{
$this->page_width=$width;
$this->page_height=$height;
$this->content_width=$width-(2*$this->page_margin);
}
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_begin_page($this->pdf,$this->loc($this->page_width),$this->loc($this->page_height);
pdf_setlinewidth($this->pdf,0.3);
$this->addHeaderAndFooterToPage();
}
@ -131,7 +146,7 @@ class lpdf
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);
$nr=pdf_show_boxed($this->pdf,$textstr, $this->loc($this->page_margin),$this->loc($this->yloc),$this->loc($this->content_width),$lineheight,$align,null);
if($nr==$prevnr)
{
@ -163,8 +178,8 @@ class lpdf
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_moveto($this->pdf,$this->loc($this->page_margin-0.25),$this->loc($this->yloc));
pdf_lineto($this->pdf,$this->loc($this->page_width-$this->page_margin+0.25),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$this->yloc-=0.25;
}
@ -184,12 +199,12 @@ class lpdf
//12/72 is height of the heading
//4/72 is the space under the heading
if($this->yloc< (1.1 + 12/72 + 4/72) )
$this->newPage();
$this->newPage($this->page_width,$this->page_height);
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_show_xy($this->pdf,$text,$this->loc($this->page_margin),$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;
@ -197,11 +212,10 @@ class lpdf
}
function addTable($table)
function addTable($table,$align="center")
{
$height['tableheader']=0.2;
$height['tabledata']=0.18;
$xpos_of_table=0.5;
$this->yloc-=$height['tableheader'];
$top_of_table=$this->yloc;
@ -210,6 +224,13 @@ class lpdf
$table_cols=count($table['header']);
$table_padding=0.03;
switch($align)
{
case "center"; $xpos_of_table=($this->page_width-$table_width)/2; break;
case "left"; $xpos_of_table=$this->page_margin; break;
case "right"; $xpos_of_table=$this->page_width-$this->page_margin-$table_width; break;
}
//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']));
@ -278,7 +299,7 @@ class lpdf
pdf_lineto($this->pdf,$this->loc($xpos),$this->loc($this->yloc));
pdf_stroke($this->pdf);
$this->newPage();
$this->newPage($this->page_width,$this->page_height);
$this->yloc-=$height['tableheader'];
$top_of_table=$this->yloc;
@ -380,7 +401,4 @@ class lpdf
//add the stuff to the first page
// $this->addHeaderAndFooterToPage();
}
}