From 61739cc3383b6e7d52b377c92c30f8edffa30d97 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 14 Aug 2022 20:03:13 +0200 Subject: [PATCH] FIX online signature, better error management. --- htdocs/core/ajax/onlineSign.php | 48 ++++++++++++++-------- htdocs/core/class/commonobject.class.php | 3 +- htdocs/public/onlinesign/newonlinesign.php | 7 ++++ 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/htdocs/core/ajax/onlineSign.php b/htdocs/core/ajax/onlineSign.php index 994ba5c42f3..a67f89cc6ef 100644 --- a/htdocs/core/ajax/onlineSign.php +++ b/htdocs/core/ajax/onlineSign.php @@ -122,28 +122,44 @@ if ($action == "importSignature") { } if (!$error) { - $newpdffilename = $upload_dir.$ref."_signed-".$date.".pdf"; + // Defined modele of doc + $directdownloadlink = $object->getLastMainDocLink('proposal'); - $pdf = pdf_getInstance(); - $pdf->Open(); - $pdf->AddPage(); - $pagecount = $pdf->setSourceFile($upload_dir.$ref.".pdf"); // original PDF + if (preg_match('/\.pdf/i', $directdownloadlink)) { + $newpdffilename = $upload_dir.$ref."_signed-".$date.".pdf"; + $sourcefile = $upload_dir.$ref.".pdf"; - for ($i=1;$i<($pagecount+1);$i++) { - if ($i>1) $pdf->AddPage(); - $tppl=$pdf->importPage($i); - $pdf->useTemplate($tppl); + if (dol_is_file($sourcefile)) { + $pdf = pdf_getInstance(); + $pdf->Open(); + $pdf->AddPage(); + $pagecount = $pdf->setSourceFile($sourcefile); // original PDF + + for ($i=1; $i<($pagecount+1); $i++) { + if ($i>1) $pdf->AddPage(); + $tppl=$pdf->importPage($i); + $pdf->useTemplate($tppl); + } + + $pdf->Image($upload_dir.$filename, 129, 239.6, 60, 15); // FIXME Position will be wrong with non A4 format. Use a value from width and height of page minus relative offset. + $pdf->Close(); + $pdf->Output($newpdffilename, "F"); + + // Index the new file and update the last_main_doc property of object. + $object->indexFile($newpdffilename, 1); + } + } elseif (preg_match('/\.odt/i', $directdownloadlink)) { + // Adding signature on .ODT not yet supported + // TODO + } else { + // Document format not supported to insert online signature. + // We should just create an image file with the signature. } + } - $pdf->Image($upload_dir.$filename, 129, 239.6, 60, 15); // FIXME Position will be wrong with non A4 format. Use a value from width and height of page minus relative offset. - $pdf->Close(); - $pdf->Output($newpdffilename, "F"); - + if (!$error) { $db->begin(); - // Index the new file and update the last_main_doc property of object. - $object->indexFile($newpdffilename, 1); - $online_sign_ip = getUserRemoteIP(); $online_sign_name = ''; // TODO Ask name on form to sign diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index bd8ab4c22f4..513717b56de 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -941,7 +941,8 @@ abstract class CommonObject // Add entry into index if ($initsharekey) { require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php'; - // TODO We can't, we dont' have full path of file, only last_main_doc adn ->element, so we must rebuild full path first + + // TODO We can't, we dont' have full path of file, only last_main_doc and ->element, so we must first rebuild full path $destfull /* $ecmfile->filepath = $rel_dir; $ecmfile->filename = $filename; diff --git a/htdocs/public/onlinesign/newonlinesign.php b/htdocs/public/onlinesign/newonlinesign.php index 87aef8163e1..b09006787c4 100644 --- a/htdocs/public/onlinesign/newonlinesign.php +++ b/htdocs/public/onlinesign/newonlinesign.php @@ -321,6 +321,13 @@ if ($source == 'proposal') { print ''.$langs->trans("Designation"); print ''.$text; if ($object->status == $object::STATUS_VALIDATED) { + if (empty($object->last_main_doc) || !dol_is_file(DOL_DATA_ROOT.'/'.$object->last_main_doc)) { + // It seems document has never been generated, or was generated and the deleted. + // So we try to regenerate it with its default template. + $defaulttemplate = ''; // We force the use an empty string instead of $object->model_pdf to be sure to use a "main" default template and not the last one used. + $object->generateDocument($defaulttemplate, $langs); + } + $directdownloadlink = $object->getLastMainDocLink('proposal'); if ($directdownloadlink) { print '
';