Question:
How can I display images from my MySQL database to pdf using Dompdf

To display an image from your MySQL database in a PDF using DomPDF in Laravel, you can follow these steps:

Make sure you have the required packages installed:


DomPDF: composer require barryvdh/laravel-dompdf Laravel Collective HTML package: composer require larave lcollective/html Ensure your database table contains a column to store the image data. You can use the BLOB (Binary Large Object) data type to store images.


In your Laravel Blade view (e.g., certificate.blade.php), you can use the Laravel Collective HTML package's image method to display the image.


Here's the updated code for your CertificateController:


namespace App\Http\Controllers;


use App\Models\Certificate;

use App\Models\Term;

use Illuminate\Http\Request;

use PDF;

use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Storage;


class CertificateController extends Controller

{

    public function generateCertificate($termId)

    {

        // Fetch the term and relevant data

        $term = Term::find($termId);


        // Check if the term and necessary data are available


        // Get the authenticated user's name

        $userName = Auth::user()->name;

        $userLastName = Auth::user()->last_name;


        // Fetch the certificate data, including the logo

        $certificate = Certificate::first();


        // Prepare data for the certificate template

        $data = [

            'username' => $userName,

            'userlastname' => $userLastName,

            'course' => $term->course->title,

            'term' => $term->title,

            'date' => $term->finish_date,

            'logo' => Storage::disk('public')->url($certificate->logo), // Use the Storage facade to get the URL

            'position_1' => $certificate->position_1,

            'position_2' => $certificate->position_2,

            'name_1' => $certificate->name_1,

            'name_2' => $certificate->name_2,

        ];


        // Load the certificate template HTML

        $html = view('certificate.certificate', $data)->render();


        // Generate the PDF from the HTML

        $pdf = PDF::loadHTML($html);


        // Set the paper size to certificate size (e.g., A4) and landscape orientation

        $pdf->setPaper('Letter', 'landscape');


        // Set the file name for the PDF

        $fileName = 'certificate_' . $term->id . '.pdf';


        // Optionally, you can store the PDF on the server or force download

        // Example: $pdf->save(storage_path('certificates/' . $fileName));

        // Example: return $pdf->download($fileName);


        // Display the PDF in the browser

        return $pdf->stream($fileName);

    }


    // Method to handle the form submission

    public function index()

    {

        $certificate = Certificate::first(); // Retrieve a single certificate

        return view('contents.admin.badges.index', compact('certificate'));

    }

}


Answered by:> nileshkardate

Credit:> Stack Overflow


Read more:

>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>How to do PHP Decryption from Node.js Encryption

>How to create a line animation in Python?

>Build an Electron application from scratch

>How you can create array with associative array in other array php?

>
>How to change the project in GCP using CLI command

Ritu Singh

Ritu Singh

Submit
0 Answers