Skip to content

Instantly share code, notes, and snippets.

@ozero
Created June 23, 2011 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozero/1042343 to your computer and use it in GitHub Desktop.
Save ozero/1042343 to your computer and use it in GitHub Desktop.
Haru pdf example: bordered cell+text function
<?php
/**
* libHaru Utility Class
* @author ozero
*
*/
/*
example:
$this->pdf = new HaruDoc;
$this->pdf->useJPFonts();
$this->pdf->useJPEncodings();
$page = $this->pdf->addPage();
$hpUtl = new HaruPdfUtil($this->pdf, $page);
$page->setSize(HaruPage::SIZE_A4, HaruPage::LANDSCAPE);//842x595
$hpUtl->text(24, 200,200, "Hello libHaru !",
array('lw'=>0.5, 'w'=>300, 'h'=>200, 'padding'=>0,'color'=>"f0f0f0",
'padding'=>array(0,20,0,0),
'align'=>'right','valign'=>'middle')
);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="haru.pdf"');
header("Cache-Control: maxage=1");
header("Pragma: public");
$this->pdf->output();
*/
class HaruPdfUtil{
/**
* Constructor
* @param HaruDoc $pdf
* @param HaruPage $page
*/
public function HaruPdfUtil(&$pdf,&$page) {
$this->pdf = $pdf;
$this->page = $page;
}
/**
* Text Cell output utility function
* @param float $size font size
* @param float $x position x
* @param float $y position y
* @param string $str string to render (utf8)
* @param array $ra rectSetting arg. key:lw,w,h,padding,color,align,valign
* @param array $font font name (libHaru)
*/
function text($size,$x,$y,$str,$ra = null,$font=array('MS-PGothic', "90ms-RKSJ-H")){
//init color
$this->page->setRGBStroke(0,0,0);
$this->page->setRGBFill(0,0,0);
//if fillColor & lineWidth : render fillStroke
if(isset($ra['color']) && isset($ra['lw'])){
$sc = $this->setColorRGB($ra['color']);
$this->page->setRGBFill($sc['r'], $sc['g'], $sc['b']);
$this->page->rectangle($x, $y-$ra['h'],$ra['w'], $ra['h']);
$this->page->fillStroke(true);
}else{
//if only fillColor: render bg
if(isset($ra['color'])){
$sc = $this->setColorRGB($ra['color']);
$this->page->setRGBFill($sc['r'], $sc['g'], $sc['b']);
$this->page->rectangle($x, $y-$ra['h'],$ra['w'], $ra['h']);
$this->page->fill();
}
//if only lineWidth: render border
if(isset($ra['lw'])){
$this->page->setLineWidth($ra['lw']);
$this->page->rectangle($x, $y-$ra['h'],$ra['w'], $ra['h']);
$this->page->stroke(true);
}
}
//init text rendering
$insFont = $this->pdf->getFont($font[0], $font[1]);
$this->page->setFontAndSize($insFont, $size);
$this->page->beginText();
$this->page->setRGBStroke(0,0,0);
$this->page->setRGBFill(0,0,0);
//if rectSetting available, use textRect()
if(!$ra){
$this->page->textOut($x, $y, mb_convert_encoding($str, 'SJIS-win', 'UTF-8'));
}else{
//taint
if(!is_array($ra['padding'])){
//top,right,bottom,left
$_pd=array($ra['padding'],$ra['padding'],$ra['padding'],$ra['padding']);
}else{
$_pd = $ra['padding'];
}
$ra['align'] =($ra['align']=="")?'left':$ra['align'];
$_al = array(
'left'=>HaruPage::TALIGN_LEFT,
'center'=>HaruPage::TALIGN_CENTER,
'right'=>HaruPage::TALIGN_RIGHT,
'justify'=>HaruPage::TALIGN_JUSTIFY,
);
$ra['valign'] =($ra['valign']=="")?'top':$ra['valign'];
//valign calc : if 'bottom', only 1 line rendered.
switch($ra['valign']){
case 'top':
$_alvp = 0;//align_v_padding
break;
case 'middle':
$_alvp = ($ra['h']-$_pd[0]-$_pd[2])/2 - $size/2;
break;
case 'bottom':
$_alvp = ($ra['h']-$_pd[0]-$_pd[2]) - $size;
break;
}
//render
try{
$this->page->textRect($x+$_pd[3], $y-$_pd[0] - $_alvp,
$x+$ra['w']-$_pd[1], $y-$ra['h']+$_pd[2],
mb_convert_encoding($str, 'SJIS-win', 'UTF-8'),
$_al[$ra['align']]);
}catch(HaruException $e){
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
$this->page->endText();
//init color
$this->page->setRGBStroke(0,0,0);
$this->page->setRGBFill(0,0,0);
return;
}
//colordef util function
function setColorRGB($hexcolor) {
$color['r'] = hexdec(substr($hexcolor, 0, 2))/255;
$color['g'] = hexdec(substr($hexcolor, 2, 2))/255;
$color['b'] = hexdec(substr($hexcolor, 4, 2))/255;
return $color;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment