Symfony 6 QR Code Generator Tutorial

Symfony 6 QR Code Generator Tutorial

Avatar photoPosted by

Hello there my fellow dev, welcome to this blog. In this blog tutorial, I will teach you to create Qr Codes in Symfony 6. This tutorial teaches you the step-by-step procedure in order for the reader to easily understand.

What is a QR Code? QR stands for Quick Response. A Qr Code is a type of barcode that is a machine-readable optical label that contains information about the item to which it is attached. Qr Code may contain a locator, identifier, or a link to a website and etc. QR Code is one of the most used types of two-dimensional code.

What is Symfony? Symfony is a PHP framework used to develop web applications, APIs, microservices, and web services. Symfony is one of the leading PHP frameworks for creating websites and web applications.

Before we proceed, please add an image in this directory “public/img/logo.png”, it will be used for inserting images on QR codes.

Step 1: Install Symfony 6

First, select a folder where you want Symfony to be installed then execute this command on Terminal or CMD to install:

Install via composer:

 composer create-project symfony/website-skeleton symfony-6-qr-code-generator

Install via Symfony CLI:

symfony new symfony-6-qr-code-generator  --full

Step 2: Install Endroid QR Code Bundle Package

We will then install this package endroid/qr-code-bundle, this package will be used for generating QR Codes.

composer require endroid/qr-code-bundle

Once done installing the package, we are set to go in generating QR codes.

You can read the more detailed documentation of endroid/qr-code-bundle package by clicking this link.

Step 3: Create A Controller

We will now create our controller, run this command to create a controller:

php bin/console make:controller QrCodeGeneratorController

After creating a controller, add these lines of codes to the src\Controller\QrCodeGeneratorController.php file:



namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Label\Font\NotoSans;

class QrCodeGeneratorController extends AbstractController
    #[Route('/qr-codes', name: 'app_qr_codes')]
    public function index(): Response
        $writer = new PngWriter();
        $qrCode = QrCode::create('')
            ->setEncoding(new Encoding('UTF-8'))
            ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
            ->setForegroundColor(new Color(0, 0, 0))
            ->setBackgroundColor(new Color(255, 255, 255));
        $logo = Logo::create('img/logo.png')
        $label = Label::create('')->setFont(new NotoSans(8));

        $qrCodes = [];
        $qrCodes['img'] = $writer->write($qrCode, $logo)->getDataUri();
        $qrCodes['simple'] = $writer->write(

        $qrCode->setForegroundColor(new Color(255, 0, 0));
        $qrCodes['changeColor'] = $writer->write(
            $label->setText('Color Change')

        $qrCode->setForegroundColor(new Color(0, 0, 0))->setBackgroundColor(new Color(255, 0, 0));
        $qrCodes['changeBgColor'] = $writer->write(
            $label->setText('Background Color Change')

        $qrCode->setSize(200)->setForegroundColor(new Color(0, 0, 0))->setBackgroundColor(new Color(255, 255, 255));
        $qrCodes['withImage'] = $writer->write(
            $label->setText('With Image')->setFont(new NotoSans(20))

        return $this->render('qr_code_generator/index.html.twig', $qrCodes);

Step 4: Update The View

We will now update the view file that was created when we create a controller. Open the file /templates/qr_code_generator/index.html.twig and remove the existing codes and change it into these:


<!doctype html>
<html lang="en">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Qr Code</title>
    <link href="" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
    <div class="container text-center">
        <div class="row">
            <div class="col">
                <img src="{{ simple }}" />
            <div class="col">
                <img src="{{ changeColor }}" />
            <div class="col">
                <img src="{{ changeBgColor }}" />
        <div class="row mt-5">
            <div class="col">
                <img src="{{ withImage }}" />
    <script src="" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>

Step 5: Run the Application

After finishing the steps above, you can now run your application by executing the command below:

symfony server:start

After successfully running your app, open this URL in your browser:


QR Codes