Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD setEnv to Pdf #78

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Pdf

protected int $timeout = 60;

protected array $env = [];

public function __construct(?string $binPath = null)
{
$this->binPath = $binPath ?? '/usr/bin/pdftotext';
Expand Down Expand Up @@ -70,9 +72,16 @@ public function setTimeout($timeout) {
return $this;
}

public function setEnv(array $env)
{
$this->env = $env;
return $this;
}

public function text(): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of introducing a seperate method for this, I'd rather have a callable optionally being passed to text. If a callable is given, it should receive the $process. This way people can customize the process however they want.

Could you adjust the PR to support this behaviour?

{
$process = new Process(array_merge([$this->binPath], $this->options, [$this->pdf, '-']));
$process->setEnv($this->env);
$process->setTimeout($this->timeout);
$process->run();
if (!$process->isSuccessful()) {
Expand Down
11 changes: 11 additions & 0 deletions tests/PdfToTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@
->setTimeout(-1)
->text()
)->throws(InvalidArgumentException::class);

it('can handle symfony process env', function () {
$text = (new Pdf('pdftotext'))
->setEnv([
'PATH' => substr($this->pdftotextPath, 0, strpos($this->pdftotextPath, '/pdftotext'))
])
->setPdf($this->dummyPdf)
->text();

expect($text)->toBe($this->dummyPdfText);
});
Loading