Generate QR Code PHP
August 7, 2025 · AI Conversations
### ✅ Method: Using `endroid/qr-code` (Most Popular)
#### Step 1: Install via Composer
composer require endroid/qr-code
#### Step 2: Generate QR Code in PHP
<?php
require 'vendor/autoload.php';
use EndroidQrCodeBuilderBuilder;
use EndroidQrCodeEncodingEncoding;
use EndroidQrCodeWriterPngWriter;
header('Content-Type: image/png');
$result = Builder::create()
->writer(new PngWriter())
->data('https://example.com') // Replace with your content
->encoding(new Encoding('UTF-8'))
->size(300)
->margin(10)
->build();
echo $result->getString();
—
### ✅ Alternative: Using `phpqrcode` (No Composer Required)
#### Step 1: Download the library
Get `phpqrcode` from: [https://sourceforge.net/projects/phpqrcode/](https://sourceforge.net/projects/phpqrcode/)
#### Step 2: Generate QR Code
<?php
include "phpqrcode/qrlib.php";
// Set content and output file
$data = "https://example.com"; // Replace with your content
QRcode::png($data);
—
### ✅ Output as Base64 (Optional)
If you want to display it in HTML without saving a file:
ob_start();
QRcode::png($data);
$imageString = base64_encode(ob_get_clean());
echo '<img src="data:image/png;base64,' . $imageString . '">';
Let me know if you want to save it to a file, customize the style, or generate multiple at once.
—
### ✅ Step-by-Step Guide:
#### ✅ Step 1: Install Required Libraries via Composer
composer require endroid/qr-code
composer require dompdf/dompdf
—
#### ✅ Step 2: Generate and Save QR Code to Folder (e.g., `storage/qrcodes/`)
<?php
require 'vendor/autoload.php';
use EndroidQrCodeBuilderBuilder;
use EndroidQrCodeEncodingEncoding;
use EndroidQrCodeWriterPngWriter;
$qrText = 'https://example.com'; // Your QR content
$qrFilePath = __DIR__ . '/storage/qrcodes/my_qr.png';
$result = Builder::create()
->writer(new PngWriter())
->data($qrText)
->encoding(new Encoding('UTF-8'))
->size(300)
->margin(10)
->build();
// Save to file
file_put_contents($qrFilePath, $result->getString());
—
#### ✅ Step 3: Load the QR Code into a Dompdf PDF
<?php
require 'vendor/autoload.php';
use DompdfDompdf;
use DompdfOptions;
$qrFilePath = __DIR__ . '/storage/qrcodes/my_qr.png';
// Configure Dompdf
$options = new Options();
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
// Create HTML with embedded image
$html = '
<h1>Your QR Code</h1>
<img src="' . $qrFilePath . '" width="150" height="150">
';
// Load and render
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
// Output to browser
$dompdf->stream("qr-code-pdf.pdf", ["Attachment" => false]);
—
### 📁 Folder Structure Example
project/
├── vendor/
├── storage/
│ └── qrcodes/
│ └── my_qr.png
├── generate_qr.php
├── create_pdf.php
└── composer.json
—
Let me know if you want to:
– Automatically delete QR codes after PDF creation
– Use inline base64 image instead of a file
– Customize QR style (color, logo, etc.)
Fatal error: Uncaught Error: Call to undefined method EndroidQrCodeBuilderBuilder::create()
means you're likely using **an older version** of the `endroid/qr-code` library that **does not support the `Builder::create()` syntax**, which was introduced in **v4+**.
—
### ✅ ✅ Fix Options:
#### 🔧 Option 1: **Upgrade the package to v4+** (Recommended)
Run this in your terminal to install the latest version:
composer require endroid/qr-code:^4.0
Then the code with `Builder::create()` will work as expected.
—
#### 🔧 Option 2: **Use compatible code for version 3.x**
If you're unable to upgrade the package, use this code instead:
require 'vendor/autoload.php';
use EndroidQrCodeQrCode;
$qrCode = new QrCode('https://example.com'); // Your QR content
$qrCode->setSize(300);
// Save to a file
$qrFilePath = __DIR__ . '/storage/qrcodes/my_qr.png';
file_put_contents($qrFilePath, $qrCode->writeString());
Then continue to include that image path in your Dompdf as shown earlier.
—
### 🧠 Summary
– ✅ `Builder::create()` → only for `endroid/qr-code` v4+
– ❌ If you're using v3.x, stick to `new QrCode(…)`
—
Would you like me to check which version you’re using based on your `composer.json` or `composer.lock`? Or help you switch to base64 embedding instead of saving the file?