PetroGromovo's avatar

How to open *.docx file with editor of the OS?

On laravel 10 / filament 3 site I create and save *.docx file with "Word2007" format using phpoffice/phpword library under site storage(I keep path of the generated file). After saving of the file I need to open the file with editor which is used for this type of file in the OS of the user. I search OS independeble desicion . How can I do it ?

"php": "^8.2",
"filament/filament": "^3.2-stable",
"laravel/framework": "^10.48.14",
"phpoffice/phpword": "^1.2"

Thanks in advance!

0 likes
5 replies
Tray2's avatar

Why not just let the user worry about that? Either they have a browser that supports opening the files and displaying the content, or they download the file, and then open it with some kind of Office app.

2 likes
PetroGromovo's avatar

@Tray2 That have sense, anyway I would like to have such functionality and not to force user search file which is under storage subdirectory where file is saved. Maybe I need to save file under other location ?

Snapey's avatar

@PetroGromovo your storage folder should be private ant never 'searchable'

But this seems a different request than your original post

PetroGromovo's avatar

@Snapey But in which way can I make that user can see/save this generated file ? Maybe from the start to save not under "Storage", but under "Downloads" of user OS? Any plugins for this?

PetroGromovo's avatar

I have installed this https://github.com/pxlrbt/filament-excel plugin which generate/upload a file in browser's download functionality. I my app I generate my document in action of file EditEmployee.php:

protected function getHeaderActions(): array
{
    return [
        Actions\DeleteAction::make(),
        Action::make('GenerateWordCard')
            ->label('Generate card in word-file')
            ->action(function (array $data, array $arguments): void {
                $employeeCard = new GenerateEmployeeCard;
                $employeeCard->setEmployee(employee: $this->record, showHireInfo: true);
                $employeeCard->generate();
                $ret = $employeeCard->download();
                $destFilename = $employeeCard->getDestFilename();
                Notification::make()
                    ->title('Employee card was successfully generated and downloaded as "' . $destFilename . '" file!')
                    ->success()
                    ->send();
            }), // Action::make('GenerateWordCard')

    ];
}

I know how to download generated file in pure laravel :

public function download(): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($this->phpWord, $this->outputFileFormat);
    $filename = 'subscriptions-of-the-app.docx';
    $objWriter->save(storage_path($filename));

    return response()->download(storage_path($filename));
}

and file is generated and dowloaded. user can open it.

But in this app action is in getHeaderActions and it returns array.

and I do not see how can I response to download the file ?

Please or to participate in this conversation.