Skip to content

Instantly share code, notes, and snippets.

@alphaolomi
Created January 18, 2023 12:59
Show Gist options
  • Save alphaolomi/f024a905ffbd833e134cc7e61d8018db to your computer and use it in GitHub Desktop.
Save alphaolomi/f024a905ffbd833e134cc7e61d8018db to your computer and use it in GitHub Desktop.
Extending spatie/pdf-to-text Pdf Class
<?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