How to Add a Box Shadow to an Image in Dompdf?

Updated: Mar 04, 2025

How to Add a Box Shadow to an Image in Dompdf?

To add a box shadow to an image in Dompdf, you cannot directly use CSS box-shadow property as Dompdf does not support it. Instead, you need to manipulate the image using external tools or libraries before generating the PDF. Here's a step-by-step guide to achieve this using PHP and the Intervention Image library:

  1. Install Intervention Image library: If you haven't installed the Intervention Image library yet, you can install it using composer by running the following command in your terminal:

    composer require intervention/image
    
  2. Create a new PHP file and include the necessary libraries:

    <?php
    
    use Intervention\Image\ImageManagerStatic as Image;
    use Dompdf\Dompdf;
    
    require 'vendor/autoload.php';
    
  3. Load the image and apply the box shadow using the Intervention Image library:

    $image = Image::make('path/to/your/image.jpg');
    $image->shadow(function ($shadow) {
        $shadow->offset(1, 1)
               ->color('gray', 0.5)
               ->blur(2);
    });
    $image->save('path/to/save/shadowed-image.jpg');
    

    In the code above, we load the image using the Image::make() method and apply the box shadow using the shadow() method. You can customize the offset, color, and blur values to achieve the desired effect.

  4. Generate the PDF using Dompdf:

    $dompdf = new Dompdf();
    $html = '<img src="path/to/save/shadowed-image.jpg" alt="Image with box shadow">';
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    $dompdf->save('path/to/save/pdf-file.pdf');
    

    In the code above, we generate the PDF using the Dompdf class and set the HTML content to display the image with the box shadow.

  5. Customize the PDF:

    You can customize the PDF further by using the Dompdf library to set the page size, margins, fonts, and other styles. For more information, check the Dompdf documentation: https://github.com/dompdf/dompdf.

That's it! With these steps, you should be able to add a box shadow to an image in Dompdf using PHP and the Intervention Image library.