Merge pull request #6762 from atm-alexis/develop

NEW Add option USE_PDFTK_FOR_PDF_CONCAT to use pdftk to concat mass pdf
This commit is contained in:
Laurent Destailleur 2017-05-05 09:24:21 +02:00 committed by GitHub
commit bd8e4cf6aa

View File

@ -406,60 +406,105 @@ if (! $error && $massaction == "builddoc" && $permtoread && ! GETPOST('button_se
$outputlangs->setDefaultLang($newlang); $outputlangs->setDefaultLang($newlang);
} }
// Create empty PDF if(!empty($conf->global->USE_PDFTK_FOR_PDF_CONCAT)) {
$pdf=pdf_getInstance(); // Create output dir if not exists
if (class_exists('TCPDF')) dol_mkdir($diroutputmassaction);
{
$pdf->setPrintHeader(false); // Defined name of merged file
$pdf->setPrintFooter(false); $filename=strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
$filename=preg_replace('/\s/','_',$filename);
// Save merged file
if ($filter=='paye:0')
{
if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
}
if ($year) $filename.='_'.$year;
if ($month) $filename.='_'.$month;
if (count($files)>0)
{
$now=dol_now();
$file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';
$input_files = '';
foreach($files as $f) {
$input_files.=' '.escapeshellarg($f);
}
$cmd = 'pdftk '.$input_files.' cat output '.escapeshellarg($file);
exec($cmd);
if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
$langs->load("exports");
setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs');
}
else
{
setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
}
} }
$pdf->SetFont(pdf_getPDFFont($outputlangs)); else {
// Create empty PDF
$pdf=pdf_getInstance();
if (class_exists('TCPDF'))
{
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
}
$pdf->SetFont(pdf_getPDFFont($outputlangs));
if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false); if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false);
// Add all others // Add all others
foreach($files as $file) foreach($files as $file)
{ {
// Charge un document PDF depuis un fichier. // Charge un document PDF depuis un fichier.
$pagecount = $pdf->setSourceFile($file); $pagecount = $pdf->setSourceFile($file);
for ($i = 1; $i <= $pagecount; $i++) for ($i = 1; $i <= $pagecount; $i++)
{ {
$tplidx = $pdf->importPage($i); $tplidx = $pdf->importPage($i);
$s = $pdf->getTemplatesize($tplidx); $s = $pdf->getTemplatesize($tplidx);
$pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L'); $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
$pdf->useTemplate($tplidx); $pdf->useTemplate($tplidx);
} }
} }
// Create output dir if not exists // Create output dir if not exists
dol_mkdir($diroutputmassaction); dol_mkdir($diroutputmassaction);
// Defined name of merged file // Defined name of merged file
$filename=strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel))); $filename=strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
$filename=preg_replace('/\s/','_',$filename); $filename=preg_replace('/\s/','_',$filename);
// Save merged file // Save merged file
if ($filter=='paye:0') if ($filter=='paye:0')
{ {
if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late"))); if ($option=='late') $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))); else $filename.='_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
} }
if ($year) $filename.='_'.$year; if ($year) $filename.='_'.$year;
if ($month) $filename.='_'.$month; if ($month) $filename.='_'.$month;
if ($pagecount) if ($pagecount)
{ {
$now=dol_now(); $now=dol_now();
$file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf'; $file=$diroutputmassaction.'/'.$filename.'_'.dol_print_date($now,'dayhourlog').'.pdf';
$pdf->Output($file,'F'); $pdf->Output($file,'F');
if (! empty($conf->global->MAIN_UMASK)) if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK)); @chmod($file, octdec($conf->global->MAIN_UMASK));
$langs->load("exports"); $langs->load("exports");
setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs'); setEventMessages($langs->trans('FileSuccessfullyBuilt',$filename.'_'.dol_print_date($now,'dayhourlog')), null, 'mesgs');
} }
else else
{ {
setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors'); setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
}
} }
} }