Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jakubjv's avatar

How implement barryvdh(create multipage PDF) into job in Laravel?

I have a problem and need some help on how to implement the Barryvdh DomPDF library or more specifically, how to use this library to create a multi-page PDF. My goal is to generate a PDF that consolidates multiple documents into one single multi-page PDF. Currently, the job ensures that multiple PDFs can be printed or generated sequentially, but each one is generated individually. However, I need to ensure that all these PDF files are generated into one single multi-page PDF. I have no idea how to achieve this in the job. Could someone assist me?

This is Laravel job for generating PDF

<?php

namespace Modules\Store\Jobs\ItemProduct;

use App\Models\User;
use App\Jobs\Nodes\CreatePDF;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use App\Jobs\Nodes\UserNotice;
use Illuminate\Support\Facades\Bus;
use App\Libraries\WebDocumentsLibrary;
use Illuminate\Queue\InteractsWithQueue;
use Modules\Store\Models\StoreItemProduct;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Barryvdh\DomPDF\Facade\Pdf;


class StoreViewItemProductLabelJob extends WebDocumentsLibrary implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable;

    /** @var array */
    private $data;

    /** @var User */
    private $user;

    private $appID;
    private $options;
    private $template;
    private $download;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data, $user, $appID, $download = true)
    {
        $this->data = $data;
        $this->user = $user;
        $this->appID = $appID;
        $this->download = $download;

    }

    public function handle(): void
    {
            /** Data by id from Item product */
            $this->data['store_item_product'] = StoreItemProduct::where('id', $this->data['id'])->first();
            $this->data['store_item'] = $this->data['store_item_product']->store_item()->first();

            $this->data['supplier'] = $this->data['store_item']->supplier()->first();
            $this->data['materialproduct'] = $this->data['store_item_product']->materialproduct()->first();
            $this->data['material'] = $this->data['materialproduct'] ? $this->data['materialproduct']->material()->first() : null;

            /** Date and time */
            $this->data['date'] = date('d/m/Y');
            $this->data['time'] = date('H:i:s');

            if (!empty($this->data)) {
                $pathInfo = pathinfo($this->data['path']);
                $filename = $pathInfo['filename'];
                $pathInfo['filename'] = $filename;

                $this->data['path'] = $pathInfo['dirname'] . '/' . $filename . '.' . $pathInfo['extension'];

                /** Set template */
                $this->setOptionsAndTemplate();

                /** Create PDF with the unique filename */
                Bus::chain([
                    new CreatePDF($this->data['path'], $this->data, $this->template, $this->options, 'hobs'),
                    new UserNotice(
                        $this->user,
                        "success",
                        __("purchases.document_was_generated", ["Object" => $filename]),
                        null,
                        ["object" => "purchase", "download" => $this->download ? $this->data['path'] : null, "appID" => ($this->appID ?? null)]
                    )
                ])->dispatch();

            }
    }


    /**
     * Handle a job failure.
     *
     * @param \Throwable $th
     * @return void
     */
    public function failed(\Throwable $th): void
    {
        UserNotice::dispatch($this->user, "error",  __("general.error"), $th->getMessage());
    }

    /** set and get template for eancode PDF */
    public function setOptionsAndTemplate()
    {
        $this->options = [
            'format' => ['101', '151'],
            'margin_left' => 5,
            'margin_right' => 5,
            'margin_top' => 5,
            'margin_bottom' => 5,
            'margin_header' => 0,
            'margin_footer' => 0,
            'orientation' => 'L',
        ];
        $this->template = "store::documents/store/item_product";


        return $this->template;
    }
}

I need to explain how to create multipage PDF in Laravel job :/ Thanks for every advice!

0 likes
0 replies

Please or to participate in this conversation.