How To Merge Multiple PDF Files In Laravel 10

How To Merge Multiple PDF Files In Laravel 10

Sure, here are the steps on how to merge multiple PDF files in Laravel 10:

  1. Install Laravel 10.
  2. Install the webklex/laravel-pdfmerger composer package.
  3. Create a controller and a blade file.
  4. In the controller, create a method to merge the PDF files.
  5. In the blade file, create a form to upload the PDF files.
  6. Submit the form and the PDF files will be merged.

Here is an example of the code you need to merge multiple PDF files in Laravel 10:

PHP

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Webklex\PDFMerger\Facades\PDFMerger;

class PDFMergerController extends Controller
{
    public function index()
    {
        return view('merge-pdf');
    }

    public function merge(Request $request)
    {
        $files = $request->file('files');

        $pdf = PDFMerger::merge($files);

        $pdf->save('merged.pdf');

        return redirect()->back()->with('success', 'PDFs merged successfully!');
    }
}

The index() method in the controller renders the blade file merge-pdf.blade.php. The merge() method in the controller merges the PDF files that are uploaded by the user and saves the merged PDF file to the merged.pdf file.

The blade file merge-pdf.blade.php contains the form to upload the PDF files.

For more examples click here.

Leave a Reply