PHP Classes

File: genBook.php

Recommend this page to a friend!
  Classes of Luciano Salvino   Verbo Magia   genBook.php   Download  
File: genBook.php
Role: Example script
Content type: text/plain
Description: Create PDF Book
Class: Verbo Magia
Create PDF documents with index section
Author: By
Last change: Now you can add images
Date: 8 years ago
Size: 16,331 bytes
 

Contents

Class file image Download
<?php define('FPDF_FONTPATH','font/'); require('fpdf.php'); class PDF extends FPDF { function PDF() { $this->FPDF(); } function toLetters($a){ if($a<27){ return strtoupper(chr($a+96)); }else{ while($a > 26){ $b++; $a = $a-26; } $b = strtoupper(chr($b+96)); $a = strtoupper(chr($a+96)); return $b.$a; } } function put_img($x,$y,$imageWith,$imagePath,$dpi='72',$url='') { $imgSize = getimagesize($imagePath); $imgRatio = $imgSize[0] / $imageWith; $outWidth = ($imgSize[0]/($imgRatio)); $outHeight = ($imgSize[1]/($imgRatio)); $this->Image($imagePath,$x,$y,$outWidth,$outHeight,'',$url); $this->SetY($y+$outHeight); } function hex2rgb($hex) { $color = str_replace('#','',$hex); $rgb = array('r' => hexdec(substr($color,0,2)), 'g' => hexdec(substr($color,2,2)), 'b' => hexdec(substr($color,4,2))); return $rgb; } function Footer() { global $title,$author,$sitename,$siteUrl,$lang,$indice,$font; //Position at 1.5 cm from bottom $this->SetY(-15); //$fuente italic 8 $this->SetFont($font,'I',8); //Text color in gray $this->SetTextColor(128); //Page number $this->Write(10,"$sitename","$siteUrl"); $this->SetY(-15); if($lang=="En") { $t_page = "Page"; $t_by = "by"; $t_back = "Back to index"; } elseif($lang=="Sp") { $t_page = "Página"; $t_by = "Por"; $t_back = "Volver al índice"; } $this->Cell(0,10,$title." | $t_by ".$author." | $t_page ".$this->PageNo(),0,0,'R'); } /* CellFitScale */ //Cell with horizontal scaling if text is too wide function CellFit($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='',$scale=0,$force=1) { //Get string width $str_width=($this->GetStringWidth($txt)>0)?$this->GetStringWidth($txt):1; //Calculate ratio to fit cell if($w==0) $w=$this->w-$this->rMargin-$this->x; $ratio=($w-$this->cMargin*2)/$str_width; $fit=($ratio < 1 || ($ratio > 1 && $force == 1)); if ($fit) { switch ($scale) { //Character spacing case 0: //Calculate character spacing in points $char_space=($w-$this->cMargin*2-$str_width)/max($this->MBGetStringLength($txt)-1,1)*$this->k; //Set character spacing $this->_out(sprintf('BT %.2f Tc ET',$char_space)); break; //Horizontal scaling case 1: //Calculate horizontal scaling $horiz_scale=$ratio*100.0; //Set horizontal scaling $this->_out(sprintf('BT %.2f Tz ET',$horiz_scale)); break; } //Override user alignment (since text will fill up cell) $align=''; } //Pass on to Cell method $this->Cell($w,$h,$txt,$border,$ln,$align,$fill,$link); //Reset character spacing/horizontal scaling if ($fit) $this->_out('BT '.($scale==0 ? '0 Tc' : '100 Tz').' ET'); } //Cell with horizontal scaling only if necessary function CellFitScale($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') { $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,1,0); } //Cell with horizontal scaling always function CellFitScaleForce($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') { $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,1,1); } //Cell with character spacing only if necessary function CellFitSpace($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') { $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,0,0); } //Cell with character spacing always function CellFitSpaceForce($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') { //Same as calling CellFit directly $this->CellFit($w,$h,$txt,$border,$ln,$align,$fill,$link,0,1); } //Patch to also work with CJK double-byte text function MBGetStringLength($s) { if($this->CurrentFont['type']=='Type0') { $len = 0; $nbbytes = strlen($s); for ($i = 0; $i < $nbbytes; $i++) { if (ord($s[$i])<128) $len++; else { $len++; $i++; } } return $len; } else return strlen($s); } function WordWrap(&$text, $maxwidth) { $text = trim($text); if ($text==='') return 0; $space = $this->GetStringWidth(' '); $lines = explode("\n", $text); $text = ''; $count = 0; foreach ($lines as $line) { $words = preg_split('/ +/', $line); $width = 0; foreach ($words as $word) { $wordwidth = $this->GetStringWidth($word); if ($width + $wordwidth <= $maxwidth) { $width += $wordwidth + $space; $text .= $word.' '; } else { $width = $wordwidth + $space; $text = rtrim($text)."\n".$word.' '; $count++; } } $text = rtrim($text)."\n"; $count++; } $text = rtrim($text); return $count; } function CreaIndex($title,$author,$chapters,$subchap,$subitem,$images,$text,$lang,$num_type){ global $font,$siteUrl,$sitename; $ftype = ''; if($lang=="En") { $t_by = "by"; $t_created = "This publication was created by verboMagia"; $t_index = "Index"; } elseif($lang=="Sp") { $t_by = "Por"; $t_created = "Esta publicación ha sido generada con verboMagia"; $t_index = "Índice"; } // Título $this->SetY(70); $this->SetFont($font,'',30); $this->MultiCell(0,15,$title,0,'C',0); // Autor $this->SetFont($font,'',18); $this->MultiCell(0,5,"$t_by ".$author,0,'C',0); $this->put_img(80,100,50,"logo.png",72,"http://your.url"); // Creador $this->SetY(200); $this->SetTextColor(128); $this->SetFont($font,'',11); $this->Cell($this->w-20,5,"$t_created:",0,1,'C'); $this->Ln(28); //$this->AddFont('dutch'); $this->SetFont('arial',$ftype,65); $rgb = $this->hex2rgb('c02afd'); $this->SetTextColor($rgb['r'],$rgb['g'],$rgb['b']); $this->SetXY(10,220); $this->CellFitScale(95,17,'verbo',0,1,'R',0,"$siteUrl"); $rgb = $this->hex2rgb('006e01'); $this->SetTextColor($rgb['r'],$rgb['g'],$rgb['b']); $this->SetXY(105,220); $this->CellFitScale(85,17,'magia',0,1,'L',0,"$siteUrl"); $this->SetTextColor(128); $this->SetX(75); $this->SetFont($font,'I',12); $this->Write(5,$sitename,"$siteUrl"); $this->SetFont($font,'',20); $this->SetTextColor(0); $this->AddPage(); //Index title $indice=$this->AddLink(); $this->SetFontSize(20); $this->SetLink($indice); $this->Cell(0,5,"$t_index",0,1,'C'); if($font=="Courier") { $wide_i = 24; $wide_j = 29; $wide_k = 34; } else { $wide_i = 19; $wide_j = 24; $wide_k = 29; } $this->SetFontSize(12); $this->Ln(10); for($i=0; $i < count($chapters); $i++) { $strsize=$this->GetStringWidth($chapters[$i]); //Link Chapter $link[$i]=$this->AddLink(); if($num_type=="num") { $t_num = ($i+1); } elseif($num_type=="let") { $t_num = strtolower($this->toLetters($i+1)); } $this->SetX(10); $strsize_t_num=$this->GetStringWidth($t_num); //Filling dots $w=$this->w-$this->rMargin-($strsize+$wide_i+$strsize_t_num); if($w < 1) $nb = 1; else $nb=$w/$this->GetStringWidth('.'); $dots=str_repeat('.',$nb); $this->Write(6,$t_num." ".$dots." ".$chapters[$i],$link[$i]); // Chapters $this->Ln(6); for($j=0; $j < count($subchap[$i]); $j++) { //Link Sub Chapter $link_sub[$i][$j]=$this->AddLink(); $strsize_sub=$this->GetStringWidth($subchap[$i][$j]); if(!$subchap[$i][$j]) { } else { $this->SetX(15); if($num_type=="num") { $t_sub_num = ($i+1).".".($j+1); } elseif($num_type=="let") { $t_sub_num = strtolower($this->toLetters($i+1)).".".strtolower($this->toLetters($j+1)); } $strsize_t_sub_num=$this->GetStringWidth($t_sub_num); $w_sub=$this->w-$this->rMargin-($strsize_sub+$wide_j+$strsize_t_sub_num); if($w_sub < 1) $nb_sub = 1; else $nb_sub=$w_sub/$this->GetStringWidth('.'); $dots_sub=str_repeat('.',$nb_sub); $this->Write(6,$t_sub_num." ".$dots_sub." ".$subchap[$i][$j],$link_sub[$i][$j]); // Sub Chapters $this->Ln(6); for($k=0; $k < count($subitem[$i][$j]); $k++) { //Link Sub Chapter $link_sub_item[$i][$j][$k]=$this->AddLink(); $strsize_sub_item=$this->GetStringWidth($subitem[$i][$j][$k]); if(!$subitem[$i][$j][$k]) { } else { $this->SetX(20); if($num_type=="num") { $t_sub_item_num = ($i+1).".".($j+1).".".($k+1); } elseif($num_type=="let") { $t_sub_item_num = strtolower($this->toLetters($i+1)).".".strtolower($this->toLetters($j+1)).".".strtolower($this->toLetters($k+1)); } $strsize_t_sub_item_num=$this->GetStringWidth($t_sub_item_num); $w_sub_item=$this->w-$this->rMargin-($strsize_sub_item+$wide_k+$strsize_t_sub_item_num); if($w_sub_item < 1) $nb_sub_item = 1; else $nb_sub_item=$w_sub_item/$this->GetStringWidth('.'); $dots_sub_item=str_repeat('.',$nb_sub_item); $this->Write(6,$t_sub_item_num." ".$dots_sub_item." ".$subitem[$i][$j][$k],$link_sub_item[$i][$j][$k]); // Sub Items $this->Ln(6); } } } } } $this->show_chapters($chapters,$subchap,$subitem,$images,$text,$link,$link_sub,$link_sub_item,$indice,$lang,$num_type); } function show_chapters($chapters,$subchap,$subitem,$images,$text,$link,$link_sub,$link_sub_item,$indice,$lang,$num_type) { global $font; if($lang=="En") { $t_back = "Back to index"; } elseif($lang=="Sp") { $t_back = "Volver al índice"; } for($i=0; $i < count($chapters); $i++) { if($num_type=="num") { $t_num = ($i+1); } elseif($num_type=="let") { $t_num = strtolower($this->toLetters($i+1)); } //Pages $this->AddPage(); $this->SetFontSize(20); $this->SetLink($link[$i]); $this->Write(8,$t_num.". ".$chapters[$i],$indice); $this->Ln(15); for($j=0; $j < count($subchap[$i]); $j++) { if($num_type=="num") { $t_sub_num = ($i+1).".".($j+1); } elseif($num_type=="let") { $t_sub_num = strtolower($this->toLetters($i+1)).".".strtolower($this->toLetters($j+1)); } $this->SetLink($link_sub[$i][$j]); $this->SetFontSize(18); if(!$subchap[$i][$j]) { } else { $this->MultiCell(0,8,$t_sub_num.". ".$subchap[$i][$j],0,1); } $this->Ln(4); for($k=0; $k < count($subitem[$i][$j]); $k++) { if($num_type=="num") { $t_sub_item_num = ($i+1).".".($j+1).".".($k+1); } elseif($num_type=="let") { $t_sub_item_num = strtolower($this->toLetters($i+1)).".".strtolower($this->toLetters($j+1)).".".strtolower($this->toLetters($k+1)); } $this->SetLink($link_sub_item[$i][$j][$k]); $this->SetFontSize(15); if(!$subitem[$i][$j][$k]) { } else { $this->MultiCell(0,7,$t_sub_item_num.". ".$subitem[$i][$j][$k],0,1); } $this->Ln(2); $curY = $this->GetY(); $this->put_img(80,($curY+5),$images[$i][$j][$k]['width'],$images[$i][$j][$k]['file'],$images[$i][$j][$k]['dpi'],$images[$i][$j][$k]['url']); $this->SetFontSize(12); $this->Write(5,$text[$i][$j][$k]); $this->Ln(8); } $this->Ln(8); } } } } $tapp = "myob"; $version = "1.3.2"; $sitename = 'verbo magia'; $siteUrl = 'http://your.server.domain'; $t_site = "$sitename - $tapp $version"; $font = 'arial'; $title = 'Book Title'; $author = 'Luciano Salvino'; $chap = array( 'Chapter 1', 'Chapter 2', 'Chapter 3', ); $subchap = array( array('Sub-Chapter 1.1'), array('Sub-Chapter 2.1','Sub-Chapter 2.2'), array('Sub-Chapter 3.1'), ); $subitem = array( array( array('Sub-Item 1.1.1'), ), array( array('Sub-Item 2.1.1'), array('Sub-Item 2.2.1'), ), array( array('Sub-Item 3.1.1','Sub-Item 3.1.2','Sub-Item 3.1.3'), ), ); $images = array( array( array( array( 'file'=>'logo.png', 'width'=>50, 'dpi'=>72, 'url'=>'http://your.url' ) ), ), array( array( array( 'file'=>'logo.png', 'width'=>50, 'dpi'=>72, 'url'=>'http://your.url' ) ), array( array( 'file'=>'logo.png', 'width'=>50, 'dpi'=>72, 'url'=>'http://your.url' ) ), ), array( array( array( 'file'=>'logo.png', 'width'=>50, 'dpi'=>72, 'url'=>'http://your.url', ), array( 'file'=>'logo.png', 'width'=>50, 'dpi'=>72, 'url'=>'http://your.url' ), array( 'file'=>'logo.png', 'width'=>50, 'dpi'=>72, 'url'=>'http://your.url' ) ), ), ); $text = array( array( array( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', ), ), array( array( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', ), array( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', ), ), array( array( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum', ), ), ); $arrLang = array('En','Sp'); $arrNumType = array('num','let'); $lang = $arrLang[0]; $num_type = $arrNumType[1]; $pdf = new PDF( '$p_orient', 'mm', '$p_size' ); $pdf->Open(); $pdf->SetTitle($title); $pdf->SetAuthor($author); $pdf->SetCreator($t_site); $pdf->SetFont($font,'',15); $pdf->AddPage(); $pdf->CreaIndex($title,$author,$chap,$subchap,$subitem,$images,$text,$lang,$num_type); $pdf->Output();