195 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
198 global $user, $langs, $conf,
$mysoc, $db, $hookmanager, $nblines;
201 dol_syslog(
"write_file outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang :
'null'));
203 if (!is_object($outputlangs)) {
204 $outputlangs = $langs;
208 $outputlangs->charset_output =
'ISO-8859-1';
212 $outputlangs->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
214 global $outputlangsbis;
215 $outputlangsbis =
null;
217 $outputlangsbis =
new Translate(
'', $conf);
219 $outputlangsbis->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
222 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
235 $file = $dir.
"/SPECIMEN.pdf";
238 $dir .=
'/'.$objectref;
239 $file = $dir.
"/".$objectref.
".pdf";
241 if (!file_exists($dir)) {
243 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
248 if (file_exists($dir)) {
250 $hookmanager->initHooks(array(
'pdfgeneration'));
251 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
252 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
255 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
260 $pdf->setAutoPageBreak(
true, 0);
262 $heightforinfotot = 50;
264 $heightforfooter = $this->marge_basse + (!
getDolGlobalString(
'MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS') ? 12 : 22);
266 if (class_exists(
'TCPDF')) {
267 $pdf->setPrintHeader(
false);
268 $pdf->setPrintFooter(
false);
274 $logodir = $conf->mycompany->dir_output;
275 if (!empty($conf->mycompany->multidir_output[
$object->entity ?? $conf->entity])) {
276 $logodir = $conf->mycompany->multidir_output[
$object->entity ?? $conf->entity];
278 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
279 $tplidx = $pdf->importPage(1);
284 $pdf->SetDrawColor(128, 128, 128);
286 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
287 $pdf->SetSubject(
$object->label);
288 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
289 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
290 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".
$object->label.
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
292 $pdf->SetCompression(
false);
296 $cert = empty($user->conf->CERTIFICATE_CRT) ?
'' : $user->conf->CERTIFICATE_CRT;
304 'Name' => $this->emetteur->name,
305 'Location' =>
getCountry($this->emetteur->country_code,
''),
306 'Reason' =>
'MYOBJECT',
307 'ContactInfo' => $this->emetteur->email
309 $pdf->setSignature($cert, $cert, $this->emetteur->name,
'', 2, $info);
313 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
317 if (!empty($tplidx)) {
318 $pdf->useTemplate($tplidx);
322 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, $outputlangsbis);
323 $pdf->SetFont(
'',
'', $default_font_size - 1);
324 $pdf->MultiCell(0, 3,
'');
325 $pdf->SetTextColor(0, 0, 0);
327 $tab_top = 90 + $top_shift;
328 $tab_top_newpage = (!
getDolGlobalString(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
329 $tab_height = 130 - $top_shift;
330 $tab_height_newpage = 150;
332 $tab_height_newpage -= $top_shift;
335 $nexY = $tab_top - 1;
338 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
340 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
341 if (!empty($extranote)) {
345 $pagenb = $pdf->getPage();
349 $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
350 $pageposbeforenote = $pagenb;
357 $pdf->startTransaction();
359 $pdf->SetFont(
'',
'', $default_font_size - 1);
360 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
362 $pageposafternote = $pdf->getPage();
363 $posyafter = $pdf->GetY();
365 if ($pageposafternote > $pageposbeforenote) {
366 $pdf->rollbackTransaction(
true);
369 while ($pagenb < $pageposafternote) {
372 if (!empty($tplidx)) {
373 $pdf->useTemplate($tplidx);
379 $pdf->setTopMargin($tab_top_newpage);
381 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
385 $pdf->setPage($pageposbeforenote);
386 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
387 $pdf->SetFont(
'',
'', $default_font_size - 1);
388 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
389 $pageposafternote = $pdf->getPage();
391 $posyafter = $pdf->GetY();
393 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
394 $pdf->AddPage(
'',
'',
true);
397 $pdf->setPage($pageposafternote);
398 $pdf->setTopMargin($tab_top_newpage);
400 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
406 $i = $pageposbeforenote;
407 while ($i < $pageposafternote) {
411 $pdf->SetDrawColor(128, 128, 128);
413 if ($i > $pageposbeforenote) {
414 $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
415 $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
417 $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
418 $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
422 $pdf->setPageOrientation(
'',
true, 0);
429 $pdf->setPage($pageposafternote);
430 if (!empty($tplidx)) {
431 $pdf->useTemplate($tplidx);
436 $height_note = $posyafter - $tab_top_newpage;
437 $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
439 $pdf->commitTransaction();
440 $posyafter = $pdf->GetY();
441 $height_note = $posyafter - $tab_top;
442 $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
444 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
446 $pdf->AddPage(
'',
'',
true);
449 $pdf->setPage($pageposafternote);
450 if (!empty($tplidx)) {
451 $pdf->useTemplate($tplidx);
457 $posyafter = $tab_top_newpage;
461 $tab_height -= $height_note;
462 $tab_top = $posyafter + 6;
471 $pdf->startTransaction();
472 $this->
pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
473 $pdf->rollbackTransaction(
true);
475 $nexY = $tab_top + $this->tabTitleHeight;
478 $pageposbeforeprintlines = $pdf->getPage();
479 $pagenb = $pageposbeforeprintlines;
480 for ($i = 0; $i < $nblines; $i++) {
482 $pdf->SetFont(
'',
'', $default_font_size - 1);
483 $pdf->SetTextColor(0, 0, 0);
485 $pdf->setTopMargin($tab_top_newpage);
486 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext + $heightforinfotot);
487 $pageposbefore = $pdf->getPage();
489 $showpricebeforepagebreak = 1;
493 $pdf->startTransaction();
496 $pageposafter = $pdf->getPage();
498 if ($pageposafter > $pageposbefore) {
499 $pdf->rollbackTransaction(
true);
500 $pdf->setPageOrientation(
'',
true, $heightforfooter);
504 $pageposafter = $pdf->getPage();
505 $posyafter = $pdf->GetY();
507 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
508 if ($i == ($nblines - 1)) {
509 $pdf->AddPage(
'',
'',
true);
510 if (!empty($tplidx)) {
511 $pdf->useTemplate($tplidx);
513 $pdf->setPage($pageposafter + 1);
519 $showpricebeforepagebreak = 1;
521 $showpricebeforepagebreak = 0;
525 $pdf->commitTransaction();
529 $nexY = $pdf->GetY();
530 $pageposafter = $pdf->getPage();
531 $pdf->setPage($pageposbefore);
532 $pdf->setTopMargin($this->marge_haute);
533 $pdf->setPageOrientation(
'',
true, 0);
536 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
537 $pdf->setPage($pageposafter);
538 $curY = $tab_top_newpage;
541 $pdf->SetFont(
'',
'', $default_font_size - 1);
549 'outputlangs' => $outputlangs,
550 'hidedetails' => $hidedetails
552 $reshook = $hookmanager->executeHooks(
'printPDFline', $parameters, $this);
555 while ($pagenb < $pageposafter) {
556 $pdf->setPage($pagenb);
557 if ($pagenb == $pageposbeforeprintlines) {
558 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
560 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
564 $pdf->setPage($pagenb);
565 $pdf->setPageOrientation(
'',
true, 0);
569 if (!empty($tplidx)) {
570 $pdf->useTemplate($tplidx);
574 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
575 if ($pagenb == $pageposafter) {
576 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
578 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
583 if (!empty($tplidx)) {
584 $pdf->useTemplate($tplidx);
594 if ($pagenb == $pageposbeforeprintlines) {
595 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
596 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
598 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
599 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
618 if (method_exists($pdf,
'AliasNbPages')) {
619 $pdf->AliasNbPages();
624 $pdf->Output($file,
'F');
627 $hookmanager->initHooks(array(
'pdfgeneration'));
628 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
630 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
631 $this->warnings = $hookmanager->warnings;
633 $this->error = $hookmanager->error;
634 $this->errors = $hookmanager->errors;
641 $this->result = array(
'fullpath' => $file);
645 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
649 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"FAC_OUTPUTDIR");
729 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
734 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
741 if (
$object->status == $object::STATUS_DRAFT &&
getDolGlobalString(
'RECRUITMENT_RECRUITMENTJOBPOSITION_DRAFT_WATERMARK')) {
742 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm',
getDolGlobalString(
'RECRUITMENT_RECRUITMENTJOBPOSITION_DRAFT_WATERMARK'));
745 $pdf->SetTextColor(0, 0, 60);
746 $pdf->SetFont(
'',
'B', $default_font_size + 3);
750 $posy = $this->marge_haute;
751 $posx = $this->page_largeur - $this->marge_droite - $w;
753 $pdf->SetXY($this->marge_gauche, $posy);
757 if ($this->emetteur->logo) {
758 $logodir = $conf->mycompany->dir_output;
759 if (!empty($conf->mycompany->multidir_output[
$object->entity ?? $conf->entity])) {
760 $logodir = $conf->mycompany->multidir_output[
$object->entity ?? $conf->entity];
763 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
765 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
767 if (is_readable($logo)) {
769 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
771 $pdf->SetTextColor(200, 0, 0);
772 $pdf->SetFont(
'',
'B', $default_font_size - 2);
773 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
774 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
777 $text = $this->emetteur->name;
778 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0,
'L');
782 $pdf->SetFont(
'',
'B', $default_font_size + 3);
783 $pdf->SetXY($posx, $posy);
784 $pdf->SetTextColor(0, 0, 60);
786 $pdf->MultiCell($w, 3, $title,
'',
'R');
788 $pdf->SetFont(
'',
'B', $default_font_size);
791 $pdf->SetXY($posx, $posy);
792 $pdf->SetTextColor(0, 0, 60);
793 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
794 if (
$object->status == $object::STATUS_DRAFT) {
795 $pdf->SetTextColor(128, 0, 0);
796 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
798 $pdf->MultiCell($w, 4, $textref,
'',
'R');
801 $pdf->SetFont(
'',
'', $default_font_size - 2);
805 $pdf->SetXY($posx, $posy);
806 $pdf->SetTextColor(0, 0, 60);
807 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefCustomer").
" : ".
dol_trunc($outputlangs->convToOutputCharset(
$object->ref_client), 65),
'',
'R');
812 if (!empty(
$object->project->ref)) {
814 $pdf->SetXY($posx, $posy);
815 $pdf->SetTextColor(0, 0, 60);
816 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->project->title),
'',
'R');
822 if (!empty(
$object->project->ref)) {
824 $pdf->SetXY($posx, $posy);
825 $pdf->SetTextColor(0, 0, 60);
826 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->project->title),
'',
'R');
832 if (!empty(
$object->project->ref)) {
833 $outputlangs->load(
"projects");
835 $pdf->SetXY($posx, $posy);
836 $pdf->SetTextColor(0, 0, 60);
837 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
842 $pdf->SetXY($posx, $posy);
843 $pdf->SetTextColor(0, 0, 60);
845 $title = $outputlangs->transnoentities(
"Date");
847 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
849 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_creation,
"day",
false, $outputlangs),
'',
'R');
851 if (
$object->thirdparty->code_client) {
853 $pdf->SetXY($posx, $posy);
854 $pdf->SetTextColor(0, 0, 60);
855 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_client),
'',
'R');
862 $current_y = $pdf->getY();
864 if ($current_y < $pdf->getY()) {
865 $top_shift = $pdf->getY() - $current_y;
875 $posx = $this->marge_gauche;
877 $posx = $this->page_largeur - $this->marge_droite - 80;
885 $pdf->SetTextColor(0, 0, 0);
886 $pdf->SetFont(
'',
'', $default_font_size - 2);
887 $pdf->SetXY($posx, $posy - 5);
888 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
889 $pdf->SetXY($posx, $posy);
890 $pdf->SetFillColor(230, 230, 230);
891 $pdf->MultiCell($widthrecbox, $hautcadre,
"", 0,
'R',
true);
892 $pdf->SetTextColor(0, 0, 60);
895 $pdf->SetXY($posx + 2, $posy + 3);
896 $pdf->SetFont(
'',
'B', $default_font_size);
897 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
898 $posy = $pdf->getY();
901 $pdf->SetXY($posx + 2, $posy);
902 $pdf->SetFont(
'',
'', $default_font_size - 1);
903 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
907 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
908 if (count($arrayidcontact) > 0) {
910 $result =
$object->fetch_contact($arrayidcontact[0]);
955 $pdf->SetTextColor(0, 0, 0);