'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.
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.
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 1:[1]
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\">");
Solution 2:[2]
If you have the GD library (which it looks like you do), roll your own logo/qr combo using getBarcodePNGData(), and applying your logo on top of it using imagecopymerge
$barcodeObj = new TCPDF2DBarcode('http://www.tcpdf.org', 'QRCODE,H');
$qrCode = $barcodeObj->getBarcodePNGData(3, 3, 'black');
$logo = imagecreatefrompng('logo.png');
//center in qr code
$logoX = imagesx($qrCode)/2 - imagesx($logo)/2;
$logoY = imagesy($qrCode)/2 - imagesy($logo)/2;
imagecopymerge($qrCode, $logo, 0, 0, $logoX, $logoY, imagesx($qrCode), imagesy($qrCode), 75);
imagepng($qrCode, null, 100); // this will stream the image out directly
Solution 3:[3]
If you use endroid/qr-code
library as "Prof" suggest above you can also use $pdf->Image
from data stream:
// Render to data as string
$data = $qr->getString();
// The '@' character is used to indicate that follows an image data stream and not an image file name
$pdf->Image('@'.$data);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Prof |
Solution 2 | Kinglish |
Solution 3 | Gagantous |