New tcpdf barcode label printing class

New tcpdf barcode label printing class and add parameters to printsheet
for using this class.
Add parameter to modules_xxx.php functions to pass printing templete
class to use
This commit is contained in:
Francis Appels 2015-05-03 10:05:59 +02:00
parent 701c85620b
commit 96dfdfeb73
4 changed files with 441 additions and 42 deletions

View File

@ -123,7 +123,7 @@ if ($action == 'builddoc')
$code=$forbarcode;
$generator=$stdobject->barcode_type_coder; // coder (loaded by fetch_barcode). Engine.
$encoding=strtoupper($stdobject->barcode_type_code); // code (loaded by fetch_barcode). Example 'ean', 'isbn', ...
$barcodeimage=$conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
$diroutput=$conf->barcode->dir_temp;
dol_mkdir($diroutput);
@ -142,27 +142,36 @@ if ($action == 'builddoc')
$result=@include_once $newdir.$generator.'.modules.php';
if ($result) break;
}
// Load barcode class
$classname = "mod".ucfirst($generator);
$module = new $classname($db);
if ($module->encodingIsSupported($encoding))
{
dol_delete_file($barcodeimage);
// File is created with full name $barcodeimage = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
$result=$module->writeBarCode($code,$encoding,'Y',4);
if ($result <= 0 || ! dol_is_file($barcodeimage))
{
$error++;
setEventMessage('Failed to generate image file of barcode for code='.$code.' encoding='.$encoding.' file='.basename($barcodeimage), 'errors');
}
}
else
{
$error++;
setEventMessage("Error, encoding ".$encoding." is not supported by encoder ".$generator.'. You must choose another barcode type or install a barcode generation engine that support '.$encoding, 'errors');
}
// Load barcode class for generating barcode image
$classname = "mod".ucfirst($generator);
$module = new $classname($db);
if ($generator != 'tcpdfbarcode') {
$template = 'standardlabel';
$is2d = false;
if ($module->encodingIsSupported($encoding))
{
$barcodeimage=$conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
dol_delete_file($barcodeimage);
// File is created with full name $barcodeimage = $conf->barcode->dir_temp.'/barcode_'.$code.'_'.$encoding.'.png';
$result=$module->writeBarCode($code,$encoding,'Y',4);
if ($result <= 0 || ! dol_is_file($barcodeimage))
{
$error++;
setEventMessage('Failed to generate image file of barcode for code='.$code.' encoding='.$encoding.' file='.basename($barcodeimage), 'errors');
}
}
else
{
$error++;
setEventMessage("Error, encoding ".$encoding." is not supported by encoder ".$generator.'. You must choose another barcode type or install a barcode generation engine that support '.$encoding, 'errors');
}
} else {
$template = 'tcpdflabel';
$encoding = $module->getTcpdfEncodingType($encoding); //convert to TCPDF compatible encoding types
$is2d = $module->is2d;
}
}
if (! $error)
@ -188,11 +197,12 @@ if ($action == 'builddoc')
// For labels
if ($mode == 'label')
{
$txtforsticker="%PHOTO%";
$textleft=make_substitutions($txtforsticker, $substitutionarray);
$textheader='';
$textfooter='';
$textright='';
$txtforsticker="%PHOTO%"; // Photo will be barcode image, %BARCODE% posible when using TCPDF generator
$textleft=make_substitutions((empty($conf->global->BARCODE_LABEL_LEFT_TEXT)?$txtforsticker:$conf->global->BARCODE_LABEL_LEFT_TEXT), $substitutionarray);
$textheader=make_substitutions((empty($conf->global->BARCODE_LABEL_HEADER_TEXT)?'':$conf->global->BARCODE_LABEL_HEADER_TEXT), $substitutionarray);
$textfooter=make_substitutions((empty($conf->global->BARCODE_LABEL_FOOTER_TEXT)?'':$conf->global->BARCODE_LABEL_FOOTER_TEXT), $substitutionarray);
$textright=make_substitutions((empty($conf->global->BARCODE_LABEL_RIGHT_TEXT)?'':$conf->global->BARCODE_LABEL_RIGHT_TEXT), $substitutionarray);
$forceimgscalewidth=(empty($conf->global->BARCODE_FORCEIMGSCALEWIDTH)?1:$conf->global->BARCODE_FORCEIMGSCALEWIDTH);
$forceimgscaleheight=(empty($conf->global->BARCODE_FORCEIMGSCALEHEIGHT)?1:$conf->global->BARCODE_FORCEIMGSCALEHEIGHT);
@ -203,6 +213,9 @@ if ($action == 'builddoc')
'textheader'=>$textheader,
'textfooter'=>$textfooter,
'textright'=>$textright,
'code'=>$code,
'encoding'=>$encoding,
'is2d'=>$is2d,
'photo'=>$barcodeimage // Photo must be a file that exists with format supported by TCPDF
);
}
@ -222,7 +235,7 @@ if ($action == 'builddoc')
{
$mesg=$langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("DescADHERENT_ETIQUETTE_TYPE"));
}
if (! $mesg) $result=members_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs, $diroutput);
if (! $mesg) $result=members_label_pdf_create($db, $arrayofmembers, $modellabel, $outputlangs, $diroutput, $template);
}
if ($result <= 0)

View File

@ -65,10 +65,12 @@ class ModelePDFCards
* @param DoliDB $db Database handler
* @param array $arrayofmembers Array of members
* @param string $modele Force modele to use ('' to not force)
* @param Translate $outputlangs Objet langs to use for translation
* @param Translate $outputlangs Object langs to use for translation
* @param string $outputdir Output directory
* @param string $template pdf generenate document class to use default 'standard'
* @return int <0 if KO, >0 if OK
*/
function members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs)
function members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs, $outputdir='', $template='standard')
{
global $conf,$langs;
$langs->load("members");
@ -97,13 +99,12 @@ function members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs)
}
}
else $code=$modele;
$modele='standard';
// If selected modele is a filename template (then $modele="modelname:filename")
$tmp=explode(':',$modele,2);
$tmp=explode(':',$template,2);
if (! empty($tmp[1]))
{
$modele=$tmp[0];
$template=$tmp[0];
$srctemplatepath=$tmp[1];
}
else $srctemplatepath=$code;
@ -116,14 +117,14 @@ function members_card_pdf_create($db, $arrayofmembers, $modele, $outputlangs)
{
foreach(array('doc','pdf') as $prefix)
{
$file = $prefix."_".$modele.".class.php";
$file = $prefix."_".$template.".class.php";
// On verifie l'emplacement du modele
$file=dol_buildpath($reldir."core/modules/member/doc/".$file,0);
if (file_exists($file))
{
$filefound=1;
$classname=$prefix.'_'.$modele;
$classname=$prefix.'_'.$template;
break;
}
}

View File

@ -0,0 +1,385 @@
<?php
/* Copyright (C) 2003 Steve Dillon
* Copyright (C) 2003 Laurent Passebecq
* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2002-2003 Jean-Louis Bergamo <jlb@j1b.org>
* Copyright (C) 2006-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2015 Francis Appels <francis.appels@yahoo.com>
*
* 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; either version 3 of the License, or
* (at your option) any later version.
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/core/modules/printsheet/doc/pdf_standardlabel.class.php
* \ingroup core
* \brief Fichier de la classe permettant d'editer au format PDF des etiquettes au format Avery ou personnalise
*/
require_once DOL_DOCUMENT_ROOT.'/core/class/commonstickergenerator.class.php';
/**
* Class to generate stick sheet with format Avery or other personalised
*/
class pdf_tcpdflabel extends CommonStickerGenerator
{
// define 1d barcode style
private $_style1d = array(
'position' => '',
'align' => 'C',
'stretch' => false,
'fitwidth' => true,
'cellfitalign' => '',
'border' => false,
'hpadding' => 'auto',
'vpadding' => 'auto',
'fgcolor' => array(0,0,0),
'bgcolor' => false,
'text' => true,
'font' => 'helvetica',
'fontsize' => 8,
'stretchtext' => 4
);
// set style for 2d barcode
private $_style2d = array(
'border' => false,
'vpadding' => 'auto',
'hpadding' => 'auto',
'fgcolor' => array(0,0,0),
'bgcolor' => false,
'module_width' => 1, // width of a single module in points
'module_height' => 1 // height of a single module in points
);
private $_align2d = 'N';
private $_xres = 0.4;
/**
* write barcode to pdf
*
* @param PDF &$pdf PDF reference
* @param string $code code to print
* @param string $encoding type of barcode
* @param boolean $is2d true if 2d barcode
* @param int $x x position in user units
* @param int $y y position in user units
* @param int $w width in user units
* @param int $h height in user units
*/
private function writeBarcode(&$pdf, $code, $encoding, $is2d, $x, $y, $w, $h)
{
if ($is2d) {
$pdf->write2DBarcode($code, $encoding, $x, $y, $w, $h, $this->_style2d, $this->_align2d);
} else {
$pdf->write1DBarcode($code, $encoding, $x, $y, $w, $h, $this->_xres, $this->_style1d);
}
}
/**
* 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
*/
function addSticker(&$pdf,$outputlangs,$param)
{
global $mysoc,$conf;
$textleft = $param['textleft'];
$header = $param['textheader'];
$footer = $param['textfooter'];
$textright = $param['textright'];
$code = $param['code'];
$encoding = $param['encoding'];
$is2d = $param['is2d'];
// 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;
}
}
$xleft = 2;
$ytop = 2;
// Top
if ($header!='')
{
$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
$pageMargins = $pdf->getMargins();
$maxwidthtouse = round($this->_Width - 2*$xleft);
$maxheighttouse = round($this->_Height - 2*$ytop);
$maxheighttouse -= (empty($footer)?0:(1+$this->_Line_Height));
$defaultratio = ($maxwidthtouse/$maxheighttouse);
$widthtouse = $maxwidthtouse;
$heighttouse = $maxheighttouse;
$logoHeight = $heighttouse;
$logoWidth = $heighttouse;
//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, 0, $logoHeight);
else if ($code && !empty($encoding))
{
$this->writeBarcode($pdf, $code, $encoding, $is2d, $_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!='') // left and right part
{
if (($textleft == '%LOGO%' || $textleft == '%PHOTO%' || $textleft == '%BARCODE%') && !strstr($textright, '%') ) // left part logo/barcode right part text
{
if ($textleft == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, 0);
else if ($code && !empty($encoding))
{
$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse/2, $heighttouse);
}
$pdf->SetXY($_PosX+($widthtouse/2), $_PosY+$ytop);
$pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textright), 0, 'R');
}
else if (($textright == '%LOGO%' || $textright == '%PHOTO%' || $textright == '%BARCODE%') && !strstr($textleft, '%')) // right part logo/barcode left part text
{
if ($textright == '%LOGO%' && $logo) $pdf->Image($logo, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, 0);
else if ($code && !empty($encoding))
{
$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+($widthtouse/2), $_PosY+$ytop, $widthtouse/2, $heighttouse);
}
$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
$pdf->MultiCell($widthtouse/2, $this->_Line_Height, $outputlangs->convToOutputCharset($textleft),0,'L');
}
else if ($textleft == '%LOGO%') // left part logo right part text/barcode
{
if ($logo) $pdf->Image($logo, $_PosX+$xleft, $_PosY+$ytop, 0, $logoHeight);
if ($code && !empty($encoding))
{
$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft+$logoWidth+1, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
} else {
$pdf->SetXY($_PosX+$xleft+$logoWidth+1, $_PosY+$ytop);
$pdf->MultiCell($widthtouse-$logoWidth1-1, $this->_Line_Height, $outputlangs->convToOutputCharset($textright),0,'R');
}
}
else if ($textright == '%LOGO%') // right part logo left part text/barcode
{
if ($logo) $pdf->Image($logo, $_PosX+$xleft+$widthtouse-$logoWidth+1, $_PosY+$ytop, 0, $logoHeight);
if ($code && !empty($encoding))
{
$this->writeBarcode($pdf, $code, $encoding, $is2d, $_PosX+$xleft, $_PosY+$ytop, $widthtouse-$logoWidth-1, $heighttouse);
} else {
$pdf->SetXY($_PosX+$xleft, $_PosY+$ytop);
$pdf->MultiCell($widthtouse-$logoWidth-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, 0, $logoHeight);
else if ($code && !empty($encoding))
{
$this->writeBarcode($pdf, $code, $encoding, $is2d, $_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!='')
{
$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;
}
}
/**
* 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->addSticker($pdf, $outputlangs, $val);
}
//$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;
}
}

View File

@ -67,9 +67,10 @@ class ModelePDFLabels
* @param string $modele Force le modele a utiliser ('' to not force)
* @param Translate $outputlangs Objet lang a utiliser pour traduction
* @param string $outputdir Output directory
* @param string $template pdf generenate document class to use default 'standardlabel'
* @return int <0 if KO, >0 if OK
*/
function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir='')
function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outputdir='', $template='standardlabel')
{
global $conf,$langs;
$langs->load("members");
@ -98,13 +99,12 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
}
}
else $code=$modele;
$modele='standardlabel';
// If selected modele is a filename template (then $modele="modelname:filename")
$tmp=explode(':',$modele,2);
$tmp=explode(':',$template,2);
if (! empty($tmp[1]))
{
$modele=$tmp[0];
$template=$tmp[0];
$srctemplatepath=$tmp[1];
}
else $srctemplatepath=$code;
@ -117,14 +117,14 @@ function members_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $
{
foreach(array('doc','pdf') as $prefix)
{
$file = $prefix."_".$modele.".class.php";
$file = $prefix."_".$template.".class.php";
// On verifie l'emplacement du modele
$file=dol_buildpath($reldir."core/modules/printsheet/doc/".$file,0);
if (file_exists($file))
{
$filefound=1;
$classname=$prefix.'_'.$modele;
$classname=$prefix.'_'.$template;
break;
}
}