Created
January 18, 2023 12:59
-
-
Save alphaolomi/f024a905ffbd833e134cc7e61d8018db to your computer and use it in GitHub Desktop.
Extending spatie/pdf-to-text Pdf Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Services; | |
class Pdf extends \Spatie\PdfToText\Pdf | |
{ | |
public function __construct(?string $binPath = '/usr/bin/pdftotext') | |
{ | |
if (!file_exists($binPath)) { | |
$binPath = '/usr/local/bin/pdftotext'; | |
} | |
if (!file_exists($binPath)) { | |
throw new \Exception( | |
sprintf('pdftotext not found. Please install it or set the path manually. See %s', 'https://github.com/spatie/pdf-to-text#requirements') | |
); | |
} | |
parent::__construct($binPath); | |
} | |
public function setPdf(string $pdf): self | |
{ | |
if (!file_exists($pdf)) { | |
throw new \Spatie\PdfToText\Exceptions\PdfNotFound("File `{$pdf}` not found"); | |
} | |
if (!is_readable($pdf)) { | |
throw new \Spatie\PdfToText\Exceptions\PdfNotFound("Could not read `{$pdf}`"); | |
} | |
$this->pdf = $pdf; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment