Laravel PHPoffice PHPWord Download Doc File

Making doc file using laravel is not so much tough at all. We can use composer package phpoffice/phpword for making word document. Here in this blog we will show how to make word document file using phpoffice/phpword based on laravel framework. Let's start it

Install Composer Package phpoffice/phpword

composer require phpoffice/phpword

Make WordDocumentController

php artisan make:controller WordDocumentController

Read also: Laravel export collection to xlsx file download

Read also: Laravel collection and common collection methods

Read also: Laravel 11 sanctum api login registration

WordDocumentController

namespace App\Http\Controllers;

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;

class WordDocumentController extends Controller
{
    public function createWordDocument()
    {
        // Create a new PHPWord object
        $phpWord = new PhpWord();

        // Add a new section to the document
        $section = $phpWord->addSection();

        // Add a title
        $section->addTitle('Sample Word Document', 1);


        // Add text
        $section->addText(fake()->text(1000));
        $section->addTextBreak();

        // Add a table
        $table = $section->addTable();

        // Define table styles
        $boldStyle = ['bold' => true];
        $cellRowSpan = ['vMerge' => 'restart'];
        $cellRowContinue = ['vMerge' => 'continue'];
        $cellColSpan = ['gridSpan' => 3];
        $cellHCentered = ['alignment' => 'center'];
        $cellVCentered = ['valign' => 'center'];

        // Add a row for the header
        $table->addRow();
        $table->addCell(3000, $cellVCentered)->addText('Name', $boldStyle);
        $table->addCell(3000, $cellVCentered)->addText('Location', $boldStyle);
        $table->addCell(3000, $cellVCentered)->addText('Contact', $boldStyle);

        // Add data rows
        $table->addRow();
        $table->addCell(3000)->addText('John Doe');
        $table->addCell(3000)->addText('New York');
        $table->addCell(3000)->addText('[email protected]');

        $table->addRow();
        $table->addCell(3000)->addText('Jane Smith');
        $table->addCell(3000)->addText('Los Angeles');
        $table->addCell(3000)->addText('[email protected]');

        // Save the document
        $fileName = 'SampleDocument.docx';
        $filePath = storage_path($fileName);
        $writer = IOFactory::createWriter($phpWord, 'Word2007');
        $writer->save($filePath);

        // Return the document as a response for download
        return response()->download($filePath)->deleteFileAfterSend(true);
    }
}

Define Necessary Route

use App\Http\Controllers\WordDocumentController;
use Illuminate\Support\Facades\Route;

Route::get('/make-word-file', [WordDocumentController::class, 'createWordDocument']);

Read also: Laravel pdf download and render using dompdf

Read also: Laravel php flash message functionality integration

Access to Route in Browser

Now access route http://127.0.0.1:8000/make-word-file and SampleDocument.docx will start downloading

Here is the expected downloaded docx file


laravel php office word download programmingmindset.com

Hope that, this blog will help you to create your document file using phpoffice/phpword. If you think this blog helps you a lot, please visit regularly and keep in touch

Tags: