Skip to content

Commit

Permalink
Merge pull request #4142 from LibreSign/backport/4140/stable30
Browse files Browse the repository at this point in the history
[stable30] fix: footer in pages with different sizes
  • Loading branch information
vitormattos authored Dec 13, 2024
2 parents d068e68 + b89c2fc commit 4fe5198
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
31 changes: 15 additions & 16 deletions lib/Handler/FooterHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FooterHandler {
private File $file;
private FileEntity $fileEntity;
private const MIN_QRCODE_SIZE = 100;
private const PIXEL_TO_CENTIMETER = 0.264583333;
private const POINT_TO_MILIMETER = 0.3527777778;
private array $templateVars = [];

public function __construct(
Expand All @@ -51,35 +51,34 @@ public function getFooter(File $file, FileEntity $fileEntity): string {
return '';
}

$htmlFooter = $this->getRenderedHtmlFooter();
$metadata = $this->getMetadata();
foreach ($metadata['d'] as $dimension) {
$orientation = $dimension['w'] > $dimension['h'] ? 'L' : 'P';
if (!isset($pdf)) {
$pdf = new Mpdf([
'tempDir' => $this->tempManager->getTempBaseDir(),
'orientation' => $orientation,
'orientation' => 'P',
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
'margin_header' => 0,
'margin_footer' => 0,
'format' => [
$dimension['w'] * self::PIXEL_TO_CENTIMETER,
$dimension['h'] * self::PIXEL_TO_CENTIMETER,
$dimension['w'] * self::POINT_TO_MILIMETER,
$dimension['h'] * self::POINT_TO_MILIMETER,
],
]);
} else {
$pdf->AddPage(
orientation: $orientation,
newformat: [
$dimension['w'] * self::PIXEL_TO_CENTIMETER,
$dimension['h'] * self::PIXEL_TO_CENTIMETER,
],
);
}

$pdf->SetHTMLFooter($this->getRenderedHtmlFooter());
$pdf->AddPage(
orientation: 'P',
newformat: [
$dimension['w'] * self::POINT_TO_MILIMETER,
$dimension['h'] * self::POINT_TO_MILIMETER,
],
);

$pdf->SetHTMLFooter($htmlFooter);
}

return $pdf->Output('', 'S');
Expand All @@ -94,7 +93,7 @@ private function getMetadata(): array {
}

private function getRenderedHtmlFooter(): string {
$tempFile = $this->tempManager->getTemporaryFile('.php');
$tempFile = $this->tempManager->getTemporaryFile('footerTemplate.php');
file_put_contents($tempFile, $this->getTemplate());
$templates = new Engine($this->tempManager->getTempBaseDir());
return $templates->render(pathinfo($tempFile, PATHINFO_FILENAME), $this->getTemplateVars());
Expand Down
12 changes: 6 additions & 6 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,11 +553,11 @@ private function getPdfToSign(FileEntity $fileData, File $originalFile): File {
}, $this->getSigners()))
->getFooter($originalFile, $fileData);
if ($footer) {
$background = $this->tempManager->getTemporaryFile('signed.pdf');
file_put_contents($background, $footer);
$stamp = $this->tempManager->getTemporaryFile('stamp.pdf');
file_put_contents($stamp, $footer);

$dest = $this->tempManager->getTemporaryFile('signed.pdf');
file_put_contents($dest, $originalFile->getContent());
$input = $this->tempManager->getTemporaryFile('input.pdf');
file_put_contents($input, $originalFile->getContent());

$javaPath = $this->appConfig->getAppValue('java_path');
$pdftkPath = $this->appConfig->getAppValue('pdftk_path');
Expand All @@ -568,8 +568,8 @@ private function getPdfToSign(FileEntity $fileData, File $originalFile): File {
$command = new Command();
$command->setCommand($javaPath . ' -jar ' . $pdftkPath);
$pdf->setCommand($command);
$pdf->addFile($dest);
$buffer = $pdf->stamp($background)
$pdf->addFile($input);
$buffer = $pdf->multiStamp($stamp)
->toString();
if (!is_string($buffer)) {
throw new LibresignException('Failed to merge the PDF with the footer. The PDF was not successfully created with the footer.');
Expand Down

0 comments on commit 4fe5198

Please sign in to comment.