Skip to content

Commit

Permalink
Merge pull request #79 from akr4m/patch-1
Browse files Browse the repository at this point in the history
Add Default Binary Path Detection for pdftotext in Constructor
  • Loading branch information
Nielsvanpach authored Oct 18, 2024
2 parents 286908e + 2f56463 commit 06dc931
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Exceptions/BinaryNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\PdfToText\Exceptions;

use Exception;

class BinaryNotFoundException extends Exception
{
}
22 changes: 21 additions & 1 deletion src/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\PdfToText;

use Closure;
use Spatie\PdfToText\Exceptions\BinaryNotFoundException;
use Spatie\PdfToText\Exceptions\CouldNotExtractText;
use Spatie\PdfToText\Exceptions\PdfNotFound;
use Symfony\Component\Process\Process;
Expand All @@ -21,7 +22,26 @@ class Pdf

public function __construct(?string $binPath = null)
{
$this->binPath = $binPath ?? '/usr/bin/pdftotext';
$this->binPath = $binPath ?? $this->findPdfToText();
}

protected function findPdfToText(): string
{
$commonPaths = [
'/usr/bin/pdftotext', // Common on Linux
'/usr/local/bin/pdftotext', // Common on Linux
'/opt/homebrew/bin/pdftotext', // Homebrew on macOS (Apple Silicon)
'/opt/local/bin/pdftotext', // MacPorts on macOS
'/usr/local/bin/pdftotext', // Homebrew on macOS (Intel)
];

foreach ($commonPaths as $path) {
if (is_executable($path)) {
return $path;
}
}

throw new BinaryNotFoundException("The required binary was not found or is not executable.");
}

public function setPdf(string $pdf): self
Expand Down

0 comments on commit 06dc931

Please sign in to comment.