Skip to content

Commit

Permalink
Methode static to add asignature to a pdf
Browse files Browse the repository at this point in the history
  • Loading branch information
wincelau committed Jul 31, 2024
1 parent 0d3849a commit 72e3ee6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 6 additions & 5 deletions app.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function($f3) {
$filename = null;
$tmpfile = tempnam($f3->get('UPLOADS'), 'pdfsignature_sign');
unlink($tmpfile);
$svgFiles = "";
$svgFiles = [];

$files = Web::instance()->receive(function($file,$formFieldName){
if($formFieldName == "pdf" && strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
Expand All @@ -179,7 +179,7 @@ function($f3) {
}

if($formFieldName == "svg") {
$svgFiles .= " ".$tmpfile."_".$fileBaseName;
$svgFiles[] = $tmpfile."_".$fileBaseName;

return basename($tmpfile."_".$fileBaseName);
}
Expand All @@ -189,12 +189,13 @@ function($f3) {
$f3->error(403);
}

if(!$svgFiles) {
if(!count($svgFiles)) {
$f3->error(403);
}

shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $tmpfile.'.svg.pdf', $svgFiles));
shell_exec(sprintf("pdftk %s multistamp %s output %s", $tmpfile.".pdf", $tmpfile.'.svg.pdf', $tmpfile.'_signe.pdf'));
PDFSignature::createPDFFromSvg($svgFiles, $tmpfile.'.svg.pdf');
PDFSignature::addSvgToPDF($tmpfile.'.pdf', $tmpfile.'.svg.pdf', $tmpfile.'_signe.pdf');

Web::instance()->send($tmpfile.'_signe.pdf', null, 0, TRUE, $filename);

if($f3->get('DEBUG')) {
Expand Down
12 changes: 10 additions & 2 deletions lib/PDFSignature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getPDF() {
copy($originalFile, $finalFile);
$bufferFile = $finalFile.".tmp";
foreach($layers as $layerFile) {
shell_exec(sprintf("pdftk %s multistamp %s output %s", $finalFile, $layerFile, $bufferFile));
self::addSvgToPDF($finalFile, $layerFile, $bufferFile);
rename($bufferFile, $finalFile);
}

Expand All @@ -73,14 +73,22 @@ public function addSignature(array $svgFiles, $outputPdfFile) {
$expireFile = $this->pathHash.".expire";
touch($expireFile, date_format(date_modify(date_create(), file_get_contents($expireFile)), 'U'));

shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $outputPdfFile, implode(" ", $svgFiles)));
self::createPDFFromSvg($svgFiles, $outputPdfFile);

if($this->gpg->isEncrypted()) {
$this->gpg->encrypt();
}
$this->toClean = array_merge($this->toClean, $svgFiles);
}

public static function createPDFFromSvg(array $svgFiles, $outputPdfFile) {
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $outputPdfFile, implode(" ", $svgFiles)));
}

public static function addSvgToPDF($pdfOrigin, $pdfSvg, $pdfOutput) {
shell_exec(sprintf("pdftk %s multistamp %s output %s", $pdfOrigin, $pdfSvg, $pdfOutput));
}

public function clean() {
foreach($this->toClean as $path) {
GPGCryptography::hardUnlink($path);
Expand Down

0 comments on commit 72e3ee6

Please sign in to comment.