Skip to content

Commit

Permalink
Use jar version of pdftk
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <vitor@php.rio>
  • Loading branch information
vitormattos committed Feb 22, 2024
1 parent ee67c8f commit 69d481b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 43 deletions.
3 changes: 2 additions & 1 deletion lib/Service/ConfigureCheckService.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public function checkPdftk(): array {
$pdftkPath = $this->appConfig->getAppValue('pdftk_path');
if ($pdftkPath) {
if (file_exists($pdftkPath)) {
\exec($pdftkPath . " --version 2>&1", $version);
$javaPath = $this->appConfig->getAppValue('java_path');
\exec($javaPath . ' -jar ' . $pdftkPath . " --version 2>&1", $version);
if (isset($version[0])) {
preg_match('/pdftk port to java (?<version>.*) a Handy Tool/', $version[0], $matches);
if (isset($matches['version'])) {
Expand Down
47 changes: 6 additions & 41 deletions lib/Service/InstallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,56 +448,21 @@ public function installPdftk(?bool $async = false): void {
$this->runAsync();
return;
}
$architecture = php_uname('m');
if ($architecture === 'x86_64') {
$fullPath = $this->installPdfTk64();
} elseif ($architecture === 'aarch64') {
$fullPath = $this->installPdfTkArm();
} else {
throw new RuntimeException(sprintf('Architecture %s is incompatible with LibreSign.', $architecture));
}
chmod($fullPath, 0700);
$this->appConfig->setAppValue('pdftk_path', $fullPath);
$this->removeDownloadProgress();
}

private function installPdfTk64():string {
try {
$file = $this->getFolder()->getFile('pdftk');
$file = $this->getFolder()->getFile('pdftk.jar');
} catch (\Throwable $th) {
$file = $this->getFolder()->newFile('pdftk');
$file = $this->getFolder()->newFile('pdftk.jar');
}
$fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $this->getInternalPathOfFile($file);
$url = 'https://gitlab.com/api/v4/projects/5024297/packages/generic/pdftk-java/v' . self::PDFTK_VERSION . '/pdftk';
$url = 'https://gitlab.com/api/v4/projects/5024297/packages/generic/pdftk-java/v' . self::PDFTK_VERSION . '/pdftk-all.jar';
/** WHEN UPDATE version: generate this hash handmade and update here */
$hash = 'dc5abe9885b26c616821ba1f24f03195';
$hash = '59a28bed53b428595d165d52988bf4cf';

$this->download($url, 'pdftk', $fullPath, $hash);
return $fullPath;
}

private function installPdfTkArm():string {
if (!extension_loaded('zip')) {
throw new RuntimeException('Zip extension is not available');
}

$extractDir = $this->getFullPath();
$compressedFileName = 'pdftk-' . self::PDFTK_VERSION . '-aarch64.zip';
try {
$file = $this->getFolder()->getFile($compressedFileName);
} catch (\Throwable $th) {
$file = $this->getFolder()->newFile($compressedFileName);
}
$fullPath = $this->getDataDir() . DIRECTORY_SEPARATOR . $this->getInternalPathOfFile($file);
$url = 'https://gitlab.com/pdftk-java/pdftk/uploads/2b32d1c2f855f92cea379f566a882ac3/native-image.zip';
/** WHEN UPDATE version: generate this hash handmade and update here */
$hash = '3a60ab7d631e2e2cd155fbe73bfa126d';

$this->download($url, 'pdftk', $fullPath, $hash);

$zip = new ZIP($extractDir . DIRECTORY_SEPARATOR . $compressedFileName);
$zip->extractFile('native-image/pdftk', $extractDir . DIRECTORY_SEPARATOR . 'pdftk');
return $fullPath;
$this->appConfig->setAppValue('pdftk_path', $fullPath);
$this->removeDownloadProgress();
}

public function uninstallPdftk(): void {
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,11 @@ private function getPdfToSign(FileEntity $fileData, File $originalFile): File {
$dest = $this->tempManager->getTemporaryFile('signed.pdf');
file_put_contents($dest, $originalFile->getContent());

$javaPath = $this->appConfig->getAppValue('java_path');
$pdftkPath = $this->appConfig->getAppValue('pdftk_path');
$pdf = new Pdf();
$command = new Command();
$command->setCommand($pdftkPath);
$command->setCommand($javaPath . ' -jar ' . $pdftkPath);
$pdf->setCommand($command);
$pdf->addFile($dest);
$buffer = $pdf->stamp($background)
Expand Down

0 comments on commit 69d481b

Please sign in to comment.