Search code examples
phphtmlpdfqr-codetcpdf

How to set logo on the center of qrcode tcpdf from html response PHP?


I am using TCPDF library to generate pdf file using PHP. They also have feature to create qrcode.

This is my syntax

$style = array(
    'border' => 0,
    'vpadding' => 'auto',
    'hpadding' => 'auto',
    'fgcolor' => array(0, 0, 0),
    'bgcolor' => false, //array(255,255,255)
    'module_width' => 1, // width of a single module in points
    'module_height' => 1 // height of a single module in points
 );

$this->cetak->AddPage('P', 'A4');
$this->cetak->write2DBarcode("aaaaa", 'QRCODE,L', 155, $this->cetak->getY(), 30, 30, $style);
$this->cetak->Output('PKKPR.pdf', 'I');
die;

This is the output.

enter image description here

For html output qrcode, i am using this code.

    $barcodeobj = new TCPDF2DBarcode('http://www.tcpdf.org', 'QRCODE,H');
    print_r($barcodeobj->getBarcodeHTML(3, 3, 'black'));
    die;

This is the output.

enter image description here

How to make the logo inside the middle of qrcode? I tried to search for documentation but couldn't find about that. Is it even possible to set logo on the center of the qrcode ?


Solution

  • I may stand corrected here, but I don't think the TCPDF barcode API can embed logos into QR codes. I recommend using the endroid/qr-code library.

    Build your QR code as per the examples, then embed the result into your PDF using a base 64 encoded data URI, something like this

    use Endroid\QrCode\QrCode;
    
    // Compile your QR code
    $qr = QrCode::create('Data');
    
    // Render to data URI
    $data = $qr->getDataUri();
    
    // Add to PDF page (WriteHTML example)
    $pdf->writeHTML("<img src=\"$data\" width=\"200\" height=\"200\">");