Cant download file from storage

I have created a file in a directory using makeDirectroy(). Then Im trying to download it using storage::download(), response->file(), response->download(), I ALSO tried using controller but none of them are working either... Im very stuck and confused... I have a private storage if you're wondering
public function downloadDocument()
{
// setting variables

if (! $storage->exists($recordID . '/compiled')) {
$storage->makeDirectory($recordID . '/compiled');
}

// Build the command
$command = [
// ...
];

// Execute the command using Symfony Process
$process = new Process($command);
$process->setTimeout(30);

try {
$process->mustRun();
} catch (ProcessFailedException $exception) {
throw new RuntimeException('compilation failed: ' . $exception->getMessage());
}

$pdfPath = $recordID . '/compiled/main.pdf';

if (! $storage->exists($pdfPath)) {
throw new RuntimeException('PDF file does not exist after compilation.');
}

return Storage::download($pdfPath);
}
public function downloadDocument()
{
// setting variables

if (! $storage->exists($recordID . '/compiled')) {
$storage->makeDirectory($recordID . '/compiled');
}

// Build the command
$command = [
// ...
];

// Execute the command using Symfony Process
$process = new Process($command);
$process->setTimeout(30);

try {
$process->mustRun();
} catch (ProcessFailedException $exception) {
throw new RuntimeException('compilation failed: ' . $exception->getMessage());
}

$pdfPath = $recordID . '/compiled/main.pdf';

if (! $storage->exists($pdfPath)) {
throw new RuntimeException('PDF file does not exist after compilation.');
}

return Storage::download($pdfPath);
}
Does it have to do with permissions or something?
31 Replies
toeknee
toeknee2w ago
What is calling downloadDocument? In my actions this is usually good:
return response()->download(
$filePath,
$file_name
)->deleteFileAfterSend();
return response()->download(
$filePath,
$file_name
)->deleteFileAfterSend();
Matthew
MatthewOP2w ago
my header action in my page is calling it. downloadDocument() is ina trait I just tried your command as well and didnt work.
toeknee
toeknee2w ago
Do you have something wrong as that works perfectly in all my situations. Are you on windows?
Matthew
MatthewOP2w ago
Linux
toeknee
toeknee2w ago
Doulbe check permissions are set right... but it should be good
Matthew
MatthewOP2w ago
Ok wait. I have to inform you I'm building a plugin The directory has read/write permissions This is the directory
drwx------ 1 mateo mateo 48 Dec 10 03:08 compiled
drwx------ 1 mateo mateo 48 Dec 10 03:08 compiled
Here are the files (and the pdf i want to get)
total 32
-rw-r--r-- 1 mateo mateo 32 Dec 10 12:51 main.aux
-rw-r--r-- 1 mateo mateo 3421 Dec 10 12:51 main.log
-rw-r--r-- 1 mateo mateo 21134 Dec 10 12:51 main.pdf
total 32
-rw-r--r-- 1 mateo mateo 32 Dec 10 12:51 main.aux
-rw-r--r-- 1 mateo mateo 3421 Dec 10 12:51 main.log
-rw-r--r-- 1 mateo mateo 21134 Dec 10 12:51 main.pdf
toeknee
toeknee2w ago
I suspect that mateo doesn't have access to the files then.... the web server will be requesting them
Matthew
MatthewOP2w ago
Im mateo How can I not have permissions should I give it www-data?
toeknee
toeknee2w ago
usually it would be www-data if www-data is the one requesting to send the data yes. who is is the web server running under?
Matthew
MatthewOP2w ago
Its running on localhost at the moment
toeknee
toeknee2w ago
it can run on localhost but the webs erver will be under a user
Matthew
MatthewOP2w ago
so what should I do then? Im still a bit lost
toeknee
toeknee2w ago
try attached. Failing that try addhing this: // Add this before the download attempt logger()->debug('Download attempt', [ 'exists' => $storage->exists($pdfPath), 'size' => $storage->size($pdfPath), 'path' => $storage->path($pdfPath), ]);
Matthew
MatthewOP2w ago
Still no download, but I have this in the logger:
Matthew
MatthewOP2w ago
Im just gonna go ahead and share the whole function:
toeknee
toeknee2w ago
You are telling it it must return a binary response, firstly remove that and test without that strict condition
Matthew
MatthewOP2w ago
I tried, its still the same
[2024-12-10 14:37:28] local.DEBUG: out: This is pdfTeX, Version 3.141592653-2.6-1.40.26 (TeX Live 2024) (preloaded format=pdflatex)
restricted \write18 enabled.

[2024-12-10 14:37:28] local.DEBUG: out: entering extended mode
(/home/mateo/Bureaublad/Projects/HomeServer/storage/app/private/16/main.tex
[2024-12-10 14:37:28] local.DEBUG: out:
LaTeX2e <2024-11-01> patch level 1
L3 programming layer <2024-11-02>
(/opt/texlive/2024/texmf-dist/tex/latex/base/article.cls
[2024-12-10 14:37:28] local.DEBUG: out:
Document Class: article 2024/06/29 v1.4n Standard LaTeX document class
(/opt/texlive/2024/texmf-dist/tex/latex/base/size10.clo
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out:
(/opt/texlive/2024/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out:
(/home/mateo/Bureaublad/Projects/HomeServer/storage/app/private/16/compiled/mai
n.aux)
[2024-12-10 14:37:28] local.DEBUG: out:
[1{/opt/texlive/2024/texmf-var/fonts/map/pdftex/updmap/pdftex.map
[2024-12-10 14:37:28] local.DEBUG: out: }
[2024-12-10 14:37:28] local.DEBUG: out: ]
[2024-12-10 14:37:28] local.DEBUG: out:
(/home/mateo/Bureaublad/Projects/HomeServer/storage/app/private/16/compiled/mai
n.aux
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out: )</opt/texlive/2024/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx10.pfb

[2024-12-10 14:37:28] local.DEBUG: out: ></opt/texlive/2024/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb
[2024-12-10 14:37:28] local.DEBUG: out: >
[2024-12-10 14:37:28] local.DEBUG: out:
Output written on /home/mateo/Bureaublad/Projects/HomeServer/storage/app/privat
e/16/compiled/main.pdf (1 page, 21134 bytes).
Transcript written on /home/mateo/Bureaublad/Projects/HomeServer/storage/app/pr
ivate/16/compiled/main.log.

[2024-12-10 14:37:28] local.DEBUG: out: This is pdfTeX, Version 3.141592653-2.6-1.40.26 (TeX Live 2024) (preloaded format=pdflatex)
restricted \write18 enabled.

[2024-12-10 14:37:28] local.DEBUG: out: entering extended mode
(/home/mateo/Bureaublad/Projects/HomeServer/storage/app/private/16/main.tex
[2024-12-10 14:37:28] local.DEBUG: out:
LaTeX2e <2024-11-01> patch level 1
L3 programming layer <2024-11-02>
(/opt/texlive/2024/texmf-dist/tex/latex/base/article.cls
[2024-12-10 14:37:28] local.DEBUG: out:
Document Class: article 2024/06/29 v1.4n Standard LaTeX document class
(/opt/texlive/2024/texmf-dist/tex/latex/base/size10.clo
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out:
(/opt/texlive/2024/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out:
(/home/mateo/Bureaublad/Projects/HomeServer/storage/app/private/16/compiled/mai
n.aux)
[2024-12-10 14:37:28] local.DEBUG: out:
[1{/opt/texlive/2024/texmf-var/fonts/map/pdftex/updmap/pdftex.map
[2024-12-10 14:37:28] local.DEBUG: out: }
[2024-12-10 14:37:28] local.DEBUG: out: ]
[2024-12-10 14:37:28] local.DEBUG: out:
(/home/mateo/Bureaublad/Projects/HomeServer/storage/app/private/16/compiled/mai
n.aux
[2024-12-10 14:37:28] local.DEBUG: out: )
[2024-12-10 14:37:28] local.DEBUG: out: )</opt/texlive/2024/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx10.pfb

[2024-12-10 14:37:28] local.DEBUG: out: ></opt/texlive/2024/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb
[2024-12-10 14:37:28] local.DEBUG: out: >
[2024-12-10 14:37:28] local.DEBUG: out:
Output written on /home/mateo/Bureaublad/Projects/HomeServer/storage/app/privat
e/16/compiled/main.pdf (1 page, 21134 bytes).
Transcript written on /home/mateo/Bureaublad/Projects/HomeServer/storage/app/pr
ivate/16/compiled/main.log.

Matthew
MatthewOP2w ago
Will do, Sorry for late response. I had some linux issues 😅 Its still nothing What the hell... As I said in the beginning its in a private storage, does that matter?
toeknee
toeknee2w ago
Yes if it's s3? Som the private storage engine will need fetching and storing as a url download Sorry I assumed it was localised out of http scope
Matthew
MatthewOP2w ago
No it's local private storage Not s3
toeknee
toeknee2w ago
should be fine as we ready the file and send it
Matthew
MatthewOP2w ago
okay wth im confused I think the process is causing the issue @toeknee In the action, if I just have
->action(function () {
// return response()->streamDownload(function () {
// echo Pdf::loadHtml(
// Blade::render('filament-latex::latex-pdf')
// )->stream();
// }, 'test.pdf');
$pdfPath = $this->record->id . '/compiled/main.pdf';
$storage = Storage::disk(config('filament-latex.storage'));

return $storage->download($pdfPath);

$this->downloadDocument();
}),
->action(function () {
// return response()->streamDownload(function () {
// echo Pdf::loadHtml(
// Blade::render('filament-latex::latex-pdf')
// )->stream();
// }, 'test.pdf');
$pdfPath = $this->record->id . '/compiled/main.pdf';
$storage = Storage::disk(config('filament-latex.storage'));

return $storage->download($pdfPath);

$this->downloadDocument();
}),
Then it works But how can I overcome this issue? I need to use the pdflatex binary
toeknee
toeknee2w ago
You will need to debug why it is doing that, you should be able to return it
Matthew
MatthewOP2w ago
ufff just discovered something. Before I said that I tried download without process and it wokred. Well I tried it in the ->action() callback. The download function is in a trait. And inside a trait, it doesnt work.
->action(function () {
$recordID = $this->record->id;
$storage = Storage::disk(config('filament-latex.storage'));

$filePath = $storage->path($recordID . '/main.tex');

$pdfDir = $storage->path($recordID . '/compiled');

if (! $storage->exists($recordID . '/main.tex')) {

throw new RuntimeException(sprintf(
'LaTeX file not found at: %s',
$filePath
));

}

if (! $storage->exists($recordID . '/compiled')) {

$storage->makeDirectory($recordID . '/compiled'); // Use Storage to create directory

}

// Build the pdflatex command
$command = [
'/usr/bin/pdflatex',
'-interaction=nonstopmode',
'-output-directory=' . $pdfDir,
$filePath,
];

$cmd = implode(' ', $command);

exec($cmd);

$pdfPath = $recordID . '/compiled/main.pdf';

if ($storage->exists($pdfPath)) {
return $storage->download($pdfPath, 'invoice.pdf', [
'Content-Type' => 'application/pdf',
]);
} else {
throw new RuntimeException('PDF file does not exist after compilation.');
}
}),
->action(function () {
$recordID = $this->record->id;
$storage = Storage::disk(config('filament-latex.storage'));

$filePath = $storage->path($recordID . '/main.tex');

$pdfDir = $storage->path($recordID . '/compiled');

if (! $storage->exists($recordID . '/main.tex')) {

throw new RuntimeException(sprintf(
'LaTeX file not found at: %s',
$filePath
));

}

if (! $storage->exists($recordID . '/compiled')) {

$storage->makeDirectory($recordID . '/compiled'); // Use Storage to create directory

}

// Build the pdflatex command
$command = [
'/usr/bin/pdflatex',
'-interaction=nonstopmode',
'-output-directory=' . $pdfDir,
$filePath,
];

$cmd = implode(' ', $command);

exec($cmd);

$pdfPath = $recordID . '/compiled/main.pdf';

if ($storage->exists($pdfPath)) {
return $storage->download($pdfPath, 'invoice.pdf', [
'Content-Type' => 'application/pdf',
]);
} else {
throw new RuntimeException('PDF file does not exist after compilation.');
}
}),
This while function works inside the ->action() but not in a trait WHat?? 🤣
toeknee
toeknee2w ago
Where are you attaching the trait from? I suspect the trait is outside the action and as such isn't returned directly as part of the action. Maybe use a helper instead of a trait
Matthew
MatthewOP2w ago
OH MY GOD I had to do
->action(function (){
**return** $this->downloadDocument();
})
->action(function (){
**return** $this->downloadDocument();
})
I had to use return?? That made the whole difference
toeknee
toeknee2w ago
Of course! How else does the action know to return? That was in the above though
Matthew
MatthewOP2w ago
good point. When I used the function without the return, the compilation still happened I just never got the download prompt Thank you for sticking with me :D learned smth new
toeknee
toeknee2w ago
That makes sense
Want results from more Discord servers?
Add your server