158 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
161 global $user, $langs,
$conf,
$mysoc, $db, $hookmanager, $nblines;
163 dol_syslog(
"write_file outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang :
'null'));
165 if (!is_object($outputlangs)) {
166 $outputlangs = $langs;
170 $outputlangs->charset_output =
'ISO-8859-1';
174 $outputlangs->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
176 global $outputlangsbis;
177 $outputlangsbis =
null;
181 $outputlangsbis->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
184 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
191 if (
$conf->asset->dir_output.
'/asset') {
196 $dir =
$conf->asset->dir_output.
'/asset';
197 $file = $dir.
"/SPECIMEN.pdf";
200 $dir =
$conf->asset->dir_output.
'/asset/'.$objectref;
201 $file = $dir.
"/".$objectref.
".pdf";
203 if (!file_exists($dir)) {
205 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
210 if (file_exists($dir)) {
212 if (!is_object($hookmanager)) {
213 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
216 $hookmanager->initHooks(array(
'pdfgeneration'));
217 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
219 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
222 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
227 $pdf->setAutoPageBreak(
true, 0);
229 $heightforinfotot = 50;
231 $heightforfooter = $this->marge_basse + (!
getDolGlobalString(
'MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS') ? 12 : 22);
233 if (class_exists(
'TCPDF')) {
234 $pdf->setPrintHeader(
false);
235 $pdf->setPrintFooter(
false);
241 $logodir =
$conf->mycompany->dir_output;
242 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
245 $pagecount = $pdf->setSourceFile($logodir .
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
246 $tplidx = $pdf->importPage(1);
251 $pdf->SetDrawColor(128, 128, 128);
253 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
254 $pdf->SetSubject($outputlangs->transnoentities(
"PdfTitle"));
255 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
257 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getAnonymisableFullName($outputlangs)));
258 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"PdfTitle").
" ".$outputlangs->convToOutputCharset(is_object(
$object->thirdparty) ? (
string)
$object->thirdparty->name :
''));
260 $pdf->SetCompression(
false);
270 'Name' => $this->emetteur->name,
271 'Location' =>
getCountry($this->emetteur->country_code,
''),
273 'ContactInfo' => $this->emetteur->email
275 $pdf->setSignature($cert, $certprivate, $this->emetteur->name,
'', 2, $info);
279 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
283 if (!empty($tplidx)) {
284 $pdf->useTemplate($tplidx);
288 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, $outputlangsbis);
289 $pdf->SetFont(
'',
'', $default_font_size - 1);
290 $pdf->MultiCell(0, 3,
'');
291 $pdf->SetTextColor(0, 0, 0);
293 $tab_top = 90 + $top_shift;
294 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
295 $tab_height = 130 - $top_shift;
296 $tab_height_newpage = 150;
298 $tab_height_newpage -= $top_shift;
301 $nexY = $tab_top - 1;
304 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
306 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
307 if (!empty($extranote)) {
311 $pagenb = $pdf->getPage();
315 $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
316 $pageposbeforenote = $pagenb;
323 $pdf->startTransaction();
325 $pdf->SetFont(
'',
'', $default_font_size - 1);
326 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
328 $pageposafternote = $pdf->getPage();
329 $posyafter = $pdf->GetY();
331 if ($pageposafternote > $pageposbeforenote) {
332 $pdf->rollbackTransaction(
true);
335 while ($pagenb < $pageposafternote) {
338 if (!empty($tplidx)) {
339 $pdf->useTemplate($tplidx);
345 $pdf->setTopMargin($tab_top_newpage);
347 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
351 $pdf->setPage($pageposbeforenote);
352 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
353 $pdf->SetFont(
'',
'', $default_font_size - 1);
354 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
355 $pageposafternote = $pdf->getPage();
357 $posyafter = $pdf->GetY();
359 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
360 $pdf->AddPage(
'',
'',
true);
363 $pdf->setPage($pageposafternote);
364 $pdf->setTopMargin($tab_top_newpage);
366 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
372 $i = $pageposbeforenote;
373 while ($i < $pageposafternote) {
377 $pdf->SetDrawColor(128, 128, 128);
379 if ($i > $pageposbeforenote) {
380 $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
381 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
383 $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
384 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
388 $pdf->setPageOrientation(
'',
true, 0);
395 $pdf->setPage($pageposafternote);
396 if (!empty($tplidx)) {
397 $pdf->useTemplate($tplidx);
402 $height_note = $posyafter - $tab_top_newpage;
403 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
405 $pdf->commitTransaction();
406 $posyafter = $pdf->GetY();
407 $height_note = $posyafter - $tab_top;
408 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
411 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
413 $pdf->AddPage(
'',
'',
true);
416 $pdf->setPage($pageposafternote);
417 if (!empty($tplidx)) {
418 $pdf->useTemplate($tplidx);
424 $posyafter = $tab_top_newpage;
428 $tab_height -= $height_note;
429 $tab_top = $posyafter + 6;
438 $pdf->startTransaction();
439 $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
440 $pdf->rollbackTransaction(
true);
442 $nexY = $tab_top + $this->tabTitleHeight;
445 $pageposbeforeprintlines = $pdf->getPage();
446 $pagenb = $pageposbeforeprintlines;
447 for ($i = 0; $i < $nblines; $i++) {
449 $pdf->SetFont(
'',
'', $default_font_size - 1);
450 $pdf->SetTextColor(0, 0, 0);
452 $pdf->setTopMargin($tab_top_newpage);
453 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext + $heightforinfotot);
454 $pageposbefore = $pdf->getPage();
456 $showpricebeforepagebreak = 1;
461 $pdf->startTransaction();
464 $pageposafter = $pdf->getPage();
466 if ($pageposafter > $pageposbefore) {
467 $pdf->rollbackTransaction(
true);
468 $pdf->setPageOrientation(
'',
true, $heightforfooter);
472 $pageposafter = $pdf->getPage();
473 $posyafter = $pdf->GetY();
475 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
476 if ($i == ($nblines - 1)) {
477 $pdf->AddPage(
'',
'',
true);
478 if (!empty($tplidx)) {
479 $pdf->useTemplate($tplidx);
481 $pdf->setPage($pageposafter + 1);
487 $showpricebeforepagebreak = 1;
489 $showpricebeforepagebreak = 0;
493 $pdf->commitTransaction();
497 $nexY = $pdf->GetY();
498 $pageposafter = $pdf->getPage();
499 $pdf->setPage($pageposbefore);
500 $pdf->setTopMargin($this->marge_haute);
501 $pdf->setPageOrientation(
'',
true, 0);
504 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
505 $pdf->setPage($pageposafter);
506 $curY = $tab_top_newpage;
509 $pdf->SetFont(
'',
'', $default_font_size - 1);
516 $nexY = max($pdf->GetY(), $nexY);
520 if (!empty(
$object->lines[$i]->array_options)) {
521 foreach (
$object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) {
525 $nexY = max($pdf->GetY(), $nexY);
537 'outputlangs' => $outputlangs,
538 'hidedetails' => $hidedetails
540 $reshook = $hookmanager->executeHooks(
'printPDFline', $parameters, $this);
545 $prev_progress =
$object->lines[$i]->get_prev_progress(
$object->id);
546 if ($prev_progress > 0 && !empty(
$object->lines[$i]->situation_percent)) {
548 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
550 $tvaligne = $sign *
$object->lines[$i]->total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
554 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva;
556 $tvaligne = $sign *
$object->lines[$i]->total_tva;
560 $localtax1ligne =
$object->lines[$i]->total_localtax1;
561 $localtax2ligne =
$object->lines[$i]->total_localtax2;
562 $localtax1_rate =
$object->lines[$i]->localtax1_tx;
563 $localtax2_rate =
$object->lines[$i]->localtax2_tx;
564 $localtax1_type =
$object->lines[$i]->localtax1_type;
565 $localtax2_type =
$object->lines[$i]->localtax2_type;
567 $vatrate = (string)
$object->lines[$i]->tva_tx;
570 if ((!isset($localtax1_type) || $localtax1_type ==
'' || !isset($localtax2_type) || $localtax2_type ==
'')
571 && (!empty($localtax1_rate) || !empty($localtax2_rate))) {
573 $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] :
'';
574 $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] :
'';
578 if ($localtax1_type && $localtax1ligne != 0) {
579 if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
580 $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
582 $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
585 if ($localtax2_type && $localtax2ligne != 0) {
586 if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
587 $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
589 $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
593 if (((
int)
$object->lines[$i]->info_bits & 0x01) == 0x01) {
596 if (!isset($this->tva[$vatrate])) {
597 $this->tva[$vatrate] = 0;
599 $this->tva[$vatrate] += $tvaligne;
601 $nexY = max($nexY, $posYAfterImage);
605 $pdf->setPage($pageposafter);
606 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
608 $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
609 $pdf->SetLineStyle(array(
'dash' => 0));
613 while ($pagenb < $pageposafter) {
614 $pdf->setPage($pagenb);
615 if ($pagenb == $pageposbeforeprintlines) {
616 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
618 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
622 $pdf->setPage($pagenb);
623 $pdf->setPageOrientation(
'',
true, 0);
629 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
630 if ($pagenb == $pageposafter) {
631 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
633 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
638 if (!empty($tplidx)) {
639 $pdf->useTemplate($tplidx);
649 if ($pagenb == $pageposbeforeprintlines) {
650 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
651 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
653 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
654 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
673 if (method_exists($pdf,
'AliasNbPages')) {
674 $pdf->AliasNbPages();
679 $pdf->Output($file,
'F');
682 $hookmanager->initHooks(array(
'pdfgeneration'));
683 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
685 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
686 $this->warnings = $hookmanager->warnings;
688 $this->error = $hookmanager->error;
689 $this->errors = $hookmanager->errors;
696 $this->result = array(
'fullpath' => $file);
700 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
704 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"ASSET_OUTPUTDIR");
795 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
797 global
$conf, $langs;
800 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
808 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm',
$conf->global->FACTURE_DRAFT_WATERMARK);
811 $pdf->SetTextColor(0, 0, 60);
812 $pdf->SetFont(
'',
'B', $default_font_size + 3);
816 $posy = $this->marge_haute;
817 $posx = $this->page_largeur - $this->marge_droite - $w;
819 $pdf->SetXY($this->marge_gauche, $posy);
822 $logodir =
$conf->mycompany->dir_output;
823 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
826 pdf_writeLogoOrCompanyName($pdf, $outputlangs, $this->emetteur, $logodir, $this->marge_gauche, $posy, $w, $default_font_size,
'L');
828 $pdf->SetFont(
'',
'B', $default_font_size + 3);
829 $pdf->SetXY($posx, $posy);
830 $pdf->SetTextColor(0, 0, 60);
831 $title = $outputlangs->transnoentities(
"PdfTitle");
834 $title .= $outputlangsbis->transnoentities(
"PdfTitle");
836 $pdf->MultiCell($w, 3, $title,
'',
'R');
838 $pdf->SetFont(
'',
'B', $default_font_size);
841 $pdf->SetXY($posx, $posy);
842 $pdf->SetTextColor(0, 0, 60);
843 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
844 if (
$object->status == $object::STATUS_DRAFT) {
845 $pdf->SetTextColor(128, 0, 0);
846 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
848 $pdf->MultiCell($w, 4, $textref,
'',
'R');
851 $pdf->SetFont(
'',
'', $default_font_size - 2);
862 if (!empty(
$object->project->ref)) {
864 $pdf->SetXY($posx, $posy);
865 $pdf->SetTextColor(0, 0, 60);
866 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
pdf_truncate_text($pdf,
$object->project->title, 50)),
'',
'R');
872 if (!empty(
$object->project->ref)) {
873 $outputlangs->load(
"projects");
875 $pdf->SetXY($posx, $posy);
876 $pdf->SetTextColor(0, 0, 60);
877 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
882 $pdf->SetXY($posx, $posy);
883 $pdf->SetTextColor(0, 0, 60);
885 $title = $outputlangs->transnoentities(
"Date");
887 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
889 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_acquisition,
"day",
false, $outputlangs),
'',
'R');
891 if (is_object(
$object->thirdparty) &&
$object->thirdparty->code_client) {
893 $pdf->SetXY($posx, $posy);
894 $pdf->SetTextColor(0, 0, 60);
895 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_client),
'',
'R');
900 $arrayidcontact =
$object->getIdContact(
'internal',
'SALESREPFOLL');
901 if (count($arrayidcontact) > 0) {
902 $usertmp =
new User($this->db);
903 $usertmp->fetch($arrayidcontact[0]);
905 $pdf->SetXY($posx, $posy);
906 $pdf->SetTextColor(0, 0, 60);
907 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"SalesRepresentative").
" : ".$usertmp->getFullName($langs),
'',
'R');
915 $current_y = $pdf->getY();
917 if ($current_y < $pdf->getY()) {
918 $top_shift = $pdf->getY() - $current_y;
928 $posx = $this->marge_gauche;
930 $posx = $this->page_largeur - $this->marge_droite - 80;
938 $pdf->SetTextColor(0, 0, 0);
939 $pdf->SetFont(
'',
'', $default_font_size - 2);
940 $pdf->SetXY($posx, $posy - 5);
941 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
942 $pdf->SetXY($posx, $posy);
943 $pdf->SetFillColor(230, 230, 230);
944 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
945 $pdf->SetTextColor(0, 0, 60);
948 $pdf->SetXY($posx + 2, $posy + 3);
949 $pdf->SetFont(
'',
'B', $default_font_size);
950 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset((
string) $this->emetteur->name), 0,
'L');
951 $posy = $pdf->getY();
954 $pdf->SetXY($posx + 2, $posy);
955 $pdf->SetFont(
'',
'', $default_font_size - 1);
956 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
960 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
961 if (count($arrayidcontact) > 0) {
963 $result =
$object->fetch_contact($arrayidcontact[0]);
968 $thirdparty =
$object->contact;
970 $thirdparty =
$object->thirdparty;
973 if (is_object($thirdparty)) {
976 $carac_client_name =
'';
983 if ($this->page_largeur < 210) {
988 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
990 $posx = $this->marge_gauche;
994 $pdf->SetTextColor(0, 0, 0);
995 $pdf->SetFont(
'',
'', $default_font_size - 2);
996 $pdf->SetXY($posx + 2, $posy - 5);
997 $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities(
"BillTo").
":", 0,
'L');
998 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
1001 $pdf->SetXY($posx + 2, $posy + 3);
1002 $pdf->SetFont(
'',
'B', $default_font_size);
1003 $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0,
'L');
1005 $posy = $pdf->getY();
1008 $pdf->SetFont(
'',
'', $default_font_size - 1);
1009 $pdf->SetXY($posx + 2, $posy);
1010 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0,
'L');
1013 $pdf->SetTextColor(0, 0, 0);