Skip to content
Snippets Groups Projects
Commit 2fd4bf10 authored by Francis Appels's avatar Francis Appels
Browse files

Add commonstickergenerator class

Common abstract class for sticker printing.
Creates contracts for write_file and addSticker methods
parent f762d6d7
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,7 @@ ...@@ -46,7 +46,7 @@
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
/** /**
* \file htdocs/core/class/pdf_label.class.php * \file htdocs/core/class/commonstickergenerator.class.php
* \ingroup core * \ingroup core
* \brief generate pdf document with labels or cards in Avery or custom format * \brief generate pdf document with labels or cards in Avery or custom format
*/ */
...@@ -59,13 +59,13 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php'; ...@@ -59,13 +59,13 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
/** /**
* Class to generate stick sheet with format Avery or other personalised * Class to generate stick sheet with format Avery or other personalised
*/ */
class pdf_label abstract class CommonStickerGenerator
{ {
var $code; // Code of format public $code; // Code of format
var $format; // Array with informations public $format; // Array with informations
// Proprietes privees // protected
var $_Avery_Name = ''; // Nom du format de l'etiquette var $_Avery_Name = ''; // Nom du format de l'etiquette
var $_Margin_Left = 0; // Marge de gauche de l'etiquette var $_Margin_Left = 0; // Marge de gauche de l'etiquette
var $_Margin_Top = 0; // marge en haut de la page avant la premiere etiquette var $_Margin_Top = 0; // marge en haut de la page avant la premiere etiquette
...@@ -79,12 +79,10 @@ class pdf_label ...@@ -79,12 +79,10 @@ class pdf_label
var $_Line_Height = 10; // Hauteur par defaut d'une ligne var $_Line_Height = 10; // Hauteur par defaut d'une ligne
var $_Metric = 'mm'; // Type of metric.. Will help to calculate good values var $_Metric = 'mm'; // Type of metric.. Will help to calculate good values
var $_Metric_Doc = 'mm'; // Type of metric for the doc.. var $_Metric_Doc = 'mm'; // Type of metric for the doc..
var $_COUNTX = 1; var $_COUNTX = 1;
var $_COUNTY = 1; var $_COUNTY = 1;
var $_First = 1; var $_First = 1;
var $Tformat;
/** /**
* Constructor * Constructor
...@@ -96,6 +94,27 @@ class pdf_label ...@@ -96,6 +94,27 @@ class pdf_label
$this->db = $db; $this->db = $db;
} }
/**
* Function to build PDF on disk, then output on HTTP strem.
*
* @param array $arrayofrecords Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
* @param Translate $outputlangs Lang object for output language
* @param string $srctemplatepath Full path of source filename for generator using a template file
* @param string $outputdir Output directory for pdf file
* @return int 1=OK, 0=KO
*/
abstract function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='');
/**
* Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0)
*
* @param PDF &$pdf PDF reference
* @param Translate $outputlangs Output langs
* @param array $param Associative array containing label content and optional parameters
* @return void
*/
abstract function addSticker(&$pdf,$outputlangs,$param);
/** /**
* Methode qui permet de modifier la taille des caracteres * Methode qui permet de modifier la taille des caracteres
* Cela modiera aussi l'espace entre chaque ligne * Cela modiera aussi l'espace entre chaque ligne
...@@ -114,184 +133,7 @@ class pdf_label ...@@ -114,184 +133,7 @@ class pdf_label
} }
/** /**
* Output a sticker on page at position _COUNTX, _COUNTY (_COUNTX and _COUNTY start from 0) * protected Print dot line
* - %LOGO% is replace with company logo
* - %PHOTO% is replace with photo provided as parameter
*
* @param PDF &$pdf PDF reference
* @param string $textleft Text left
* @param string $header Header
* @param string $footer Footer
* @param Translate $outputlangs Output langs
* @param string $textright Text right
* @param string $photo Photo (full path to image file used as replacement for key %PHOTOS% into left, right, header or footer text)
* @return void
*/
function Add_PDF_card(&$pdf,$textleft,$header,$footer,$outputlangs,$textright='',$photo='')
{
global $mysoc,$conf,$langs;
global $forceimgscalewidth,$forceimgscaleheight;
$imgscalewidth=(empty($forceimgscalewidth)?0.3:$forceimgscalewidth); // Scale of image for width (1=Full width of sticker)
$imgscaleheight=(empty($forceimgscalewidth)?0.5:$forceimgscalewidth); // Scale of image for height (1=Full height of sticker)
// We are in a new page, then we must add a page
if (($this->_COUNTX ==0) && ($this->_COUNTY==0) and (!$this->_First==1)) {
$pdf->AddPage();
}
$this->_First=0;
$_PosX = $this->_Margin_Left+($this->_COUNTX*($this->_Width+$this->_X_Space));
$_PosY = $this->_Margin_Top+($this->_COUNTY*($this->_Height+$this->_Y_Space));
// Define logo
$logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
if (! is_readable($logo))
{
$logo='';
if (! empty($mysoc->logo_small) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small))
{
$logo=$conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
}
elseif (! empty($mysoc->logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$mysoc->logo))
{
$logo=$conf->mycompany->dir_output.'/logos/'.$mysoc->logo;
}
}
// Define photo
if (! empty($photo))
{
if (! is_readable($photo)) $photo='';
}
// Define background image
$backgroundimage='';
// Print lines
if ($this->code == "CARD")
{
$this->Tformat=$this->_Avery_Labels["CARD"];
//$this->_Pointille($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.3,25);
$this->_Croix($pdf,$_PosX,$_PosY,$_PosX+$this->_Width,$_PosY+$this->_Height,0.1,10);
}
// Background
if ($backgroundimage)
{
$pdf->image($backgroundimage,$_PosX,$_PosY,$this->_Width,$this->_Height);
}
$xleft=2; $ytop=2;
// Top
if ($header!='')
{
if ($this->code == "CARD")
{
$pdf->SetDrawColor(128,128,128);
$pdf->Line($_PosX, $_PosY+$this->_Line_Height+1, $_PosX+$this->_Width, $_PosY+$this->_Line_Height+1); // Only 1 mm and not ytop for top text
$pdf->SetDrawColor(0,0,0);
}
$pdf->SetXY($_PosX+$xleft, $_PosY+1); // Only 1 mm and not ytop for top text
$pdf->Cell($this->_Width-2*$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($header),0,1,'C');
}
$ytop+=(empty($header)?0:(1+$this->_Line_Height));
// Define widthtouse and heighttouse
$maxwidthtouse=round(($this->_Width - 2*$xleft)*$imgscalewidth); $maxheighttouse=round(($this->_Height - 2*$ytop)*$imgscaleheight);
$defaultratio=($maxwidthtouse/$maxheighttouse);
$widthtouse=$maxwidthtouse; $heighttouse=0; // old value for image
$tmp=dol_getImageSize($photo, false);
if ($tmp['height'])
{
$imgratio=$tmp['width']/$tmp['height'];
if ($imgratio >= $defaultratio) { $widthtouse = $maxwidthtouse; $heighttouse = round($widthtouse / $imgratio); }
else { $heightouse = $maxheighttouse; $widthtouse = round($heightouse * $imgratio); }
}
//var_dump($this->_Width.'x'.$this->_Height.' with border and scale '.$imgscale.' => max '.$maxwidthtouse.'x'.$maxheighttouse.' => We use '.$widthtouse.'x'.$heighttouse);exit;
// Center
if ($textright=='') // Only a left part
{
// Output left area
if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
else
{
$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
$pdf->MultiCell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
}
}
else if ($textleft!='' && $textright!='') //
{
if ($textleft == '%LOGO%' || $textleft == '%PHOTO%')
{
if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
else if ($textleft == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
$pdf->SetXY($_PosX+$xleft+$widthtouse+1, $_PosY+$ytop);
$pdf->MultiCell($this->_Width-$xleft-$xleft-$widthtouse-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
}
else if ($textright == '%LOGO%' || $textright == '%PHOTO%')
{
if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
$pdf->MultiCell($this->_Width-$widthtouse-$xleft-$xleft-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
}
else // text on halft left and text on half right
{
$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
$pdf->MultiCell(round($this->_Width/2), $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
$pdf->SetXY($_PosX+round($this->_Width/2), $_PosY+$ytop);
$pdf->MultiCell(round($this->_Width/2)-2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
}
}
else // Only a right part
{
// Output right area
if ($textright == '%LOGO%' && $logo) $pdf->Image($logo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
else if ($textright == '%PHOTO%' && $photo) $pdf->Image($photo,$_PosX+$this->_Width-$widthtouse-$xleft,$_PosY+$ytop,$widthtouse,$heighttouse);
else
{
$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
$pdf->MultiCell($this->_Width-$xleft, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
}
}
// Bottom
if ($footer!='')
{
if ($this->code == "CARD")
{
$pdf->SetDrawColor(128,128,128);
$pdf->Line($_PosX, $_PosY+$this->_Height-$this->_Line_Height-2, $_PosX+$this->_Width, $_PosY+$this->_Height-$this->_Line_Height-2);
$pdf->SetDrawColor(0,0,0);
}
$pdf->SetXY($_PosX, $_PosY+$this->_Height-$this->_Line_Height-1);
$pdf->Cell($this->_Width, $this->_Line_Height, $outputlangs->convToOutputCharset($footer),0,1,'C');
}
//print "$_PosY+$this->_Height-$this->_Line_Height-1<br>\n";
$this->_COUNTY++;
if ($this->_COUNTY == $this->_Y_Number) {
// Si on est en bas de page, on remonte le 'curseur' de position
$this->_COUNTX++;
$this->_COUNTY=0;
}
if ($this->_COUNTX == $this->_X_Number) {
// Si on est en bout de page, alors on repart sur une nouvelle page
$this->_COUNTX=0;
$this->_COUNTY=0;
}
}
/**
* Print dot line
* *
* @param PDF &$pdf PDF reference * @param PDF &$pdf PDF reference
* @param int $x1 X1 * @param int $x1 X1
...@@ -332,7 +174,7 @@ class pdf_label ...@@ -332,7 +174,7 @@ class pdf_label
} }
/** /**
* Fonction realisant une croix aux 4 coins des cartes * protected Function realisant une croix aux 4 coins des cartes
* *
* @param PDF &$pdf PDF reference * @param PDF &$pdf PDF reference
* @param int $x1 X1 * @param int $x1 X1
...@@ -366,7 +208,7 @@ class pdf_label ...@@ -366,7 +208,7 @@ class pdf_label
} }
/** /**
* Convert units (in to mm, mm to in) * protected Convert units (in to mm, mm to in)
* $src and $dest must be 'in' or 'mm' * $src and $dest must be 'in' or 'mm'
* *
* @param int $value value * @param int $value value
...@@ -386,7 +228,7 @@ class pdf_label ...@@ -386,7 +228,7 @@ class pdf_label
} }
/** /**
* Give the height for a char size given. * protected Give the height for a char size given.
* *
* @param int $pt Point * @param int $pt Point
* @return int Height chars * @return int Height chars
...@@ -403,7 +245,7 @@ class pdf_label ...@@ -403,7 +245,7 @@ class pdf_label
} }
/** /**
* Set format * protected Set format
* *
* @param PDF &$pdf PDF reference * @param PDF &$pdf PDF reference
* @param string $format Format * @param string $format Format
...@@ -425,122 +267,4 @@ class pdf_label ...@@ -425,122 +267,4 @@ class pdf_label
$this->Set_Char_Size($pdf, $format['font-size']); $this->Set_Char_Size($pdf, $format['font-size']);
} }
/**
* Function to build PDF on disk, then output on HTTP strem.
*
* @param array $arrayofrecords Array of record informations (array('textleft'=>,'textheader'=>, ..., 'id'=>,'photo'=>)
* @param Translate $outputlangs Lang object for output language
* @param string $srctemplatepath Full path of source filename for generator using a template file
* @param string $outputdir Output directory for pdf file
* @return int 1=OK, 0=KO
*/
function write_file($arrayofrecords,$outputlangs,$srctemplatepath,$outputdir='')
{
global $user,$conf,$langs,$mysoc,$_Avery_Labels;
$this->code=$srctemplatepath;
$this->Tformat = $_Avery_Labels[$this->code];
if (empty($this->Tformat)) { dol_print_error('','ErrorBadTypeForCard'.$this->code); exit; }
$this->type = 'pdf';
$this->format = $this->Tformat['paper-size'];
if (! is_object($outputlangs)) $outputlangs=$langs;
// For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO
if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1';
$outputlangs->load("main");
$outputlangs->load("dict");
$outputlangs->load("companies");
$outputlangs->load("admin");
$title=$outputlangs->transnoentities('Labels');
$keywords=$title." ".$outputlangs->convToOutputCharset($mysoc->name);
$dir = (empty($outputdir)?$conf->adherent->dir_temp:$outputdir);
$filename='tmp_address_sheet.pdf';
$file = $dir."/".$filename;
if (! file_exists($dir))
{
if (dol_mkdir($dir) < 0)
{
$this->error=$langs->trans("ErrorCanNotCreateDir",$dir);
return 0;
}
}
$pdf=pdf_getInstance($this->format,$this->Tformat['metric']);
if (class_exists('TCPDF'))
{
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
}
$pdf->SetFont(pdf_getPDFFont($outputlangs));
$pdf->SetTitle($title);
$pdf->SetSubject($title);
$pdf->SetCreator("Dolibarr ".DOL_VERSION);
$pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
$pdf->SetKeyWords($keywords);
if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
$pdf->SetMargins(0,0);
$pdf->SetAutoPageBreak(false);
$this->_Metric_Doc = $this->Tformat['metric'];
// Permet de commencer l'impression de l'etiquette desiree dans le cas ou la page a deja servie
$posX=1;
$posY=1;
if ($posX > 0) $posX--; else $posX=0;
if ($posY > 0) $posY--; else $posY=0;
$this->_COUNTX = $posX;
$this->_COUNTY = $posY;
$this->_Set_Format($pdf, $this->Tformat);
$pdf->Open();
$pdf->AddPage();
// Add each record
foreach($arrayofrecords as $val)
{
// imprime le texte specifique sur la carte
$this->Add_PDF_card($pdf,$val['textleft'],$val['textheader'],$val['textfooter'],$langs,$val['textright'],$val['photo']);
}
//$pdf->SetXY(10, 295);
//$pdf->Cell($this->_Width, $this->_Line_Height, 'XXX',0,1,'C');
// Output to file
$pdf->Output($file,'F');
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
// Output to http stream
clearstatcache();
$attachment=true;
if (! empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment=false;
$type=dol_mimetype($filename);
//if ($encoding) header('Content-Encoding: '.$encoding);
if ($type) header('Content-Type: '.$type);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
else header('Content-Disposition: inline; filename="'.$filename.'"');
// Ajout directives pour resoudre bug IE
header('Cache-Control: Public, must-revalidate');
header('Pragma: public');
readfile($file);
return 1;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment