How To To Merge Multiple PDF Files In Laravel

Hello Geek, The lara-pdf-merger package is a Laravel package that allows you to merge multiple PDF files into a single PDF file. In this guide, we’ll walk through the steps necessary to use this package to merge PDF files.

Merge Multiple PDF Files In Laravel

Step 1: Install the Package

First, you need to install the lara-pdf-merger package. You can do this by running the following command in your terminal:

composer require arielmejiadev/larapdfmerger

This will install the package and all of its dependencies.

Step 2: Create a Route

Next, you need to create a route that will handle the merging of PDF files. In your routes file, add the following code:

Route::get('/merge-pdfs', function () {
    // Code to merge PDFs will go here
});

This creates a route that will be accessible at ‘http://yourdomain.com/merge-pdfs.

Step 3: Write the Code to Merge PDFs

Now that you have a route in place, you need to write the code that will handle the merging of PDF files. In the route callback, add the following code:

use ArielMejiaDev\LarapdfMerger\Facades\PdfMerger;

$merger = PDFMerger::init();

$merger->addPDF(public_path('pdfs/file1.pdf'))
    ->addPDF(public_path('pdfs/file2.pdf'))
    ->merge('file', 'public');

This code uses the PdfMerger facade to initialize a new ‘PDFMerger‘ object. The ‘addPDF‘ method is used to add each of the PDF files that you want to merge. In this example, we’re adding ‘file1.pdf‘ and ‘file2.pdf‘, which are both located in the ‘public/pdfs‘ directory.

Once all of the PDF files have been added, the ‘merge‘ method is used to merge them into a single PDF file. The first argument is the name that you want to give to the merged PDF file, and the second argument specifies the directory where you want to save the merged PDF file. In this example, we’re saving the merged PDF file as ‘file.pdf ‘ in the ‘public‘ directory.

Step 4: Test the Route

Now that you’ve written the code to merge PDF files, you can test the route by visiting ‘http://yourdomain.com/merge-pdfs‘ in your browser. If everything is working correctly, a new file named ‘file.pdf‘ should be created in the ‘public‘ directory, and it should contain the contents of both ‘file1.pdf‘ and ‘file2.pdf‘.

Step 5: Add Error Handling

Finally, it’s a good idea to add some error handling to your code. If any errors occur during the PDF merging process, you’ll want to be able to catch them and handle them appropriately. Here’s an example of how you can add error handling to the code:

use ArielMejiaDev\LarapdfMerger\Exceptions\PdfMergerException;

try {
    $merger = PDFMerger::init();

    $merger->addPDF(public_path('pdfs/file1.pdf'))
        ->addPDF(public_path('pdfs/file2.pdf'))
        ->merge('file', 'public');
} catch (PdfMergerException $e) {
    // Handle the exception here
}

This code wraps the PDF merging code in a try-catch block. If any exceptions are thrown during the PDF merging process, they will be caught and handled by the catch block. In this example, we’re simply outputting an error message, but you can customize this code to handle errors in whatever way makes sense for you.

All the best nerd!

Leave a Reply