简体   繁体   English

如何从 html 响应 PHP 在 qrcode tcpdf 的中心设置徽标?

[英]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.我正在使用 TCPDF 库使用 PHP 生成 pdf 文件。 They also have feature to create qrcode.他们还具有创建 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.这是 output。

在此处输入图像描述

For html output qrcode, i am using this code.对于 html output 二维码,我正在使用此代码。

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

This is the output.这是 output。

在此处输入图像描述

How to make the logo inside the middle of qrcode?如何在二维码中间制作logo? 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?甚至可以在二维码的中心设置标志吗?

I may stand corrected here, but I don't think the TCPDF barcode API can embed logos into QR codes.我可能会在这里纠正,但我不认为 TCPDF 条形码 API 可以将徽标嵌入到二维码中。 I recommend using the endroid/qr-code library.我推荐使用endroid/qr-code库。

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根据示例构建您的 QR 码,然后使用 base 64 编码数据 URI 将结果嵌入到您的 PDF 中,如下所示

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\">");

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如果你有 GD 库(看起来像你),使用getBarcodePNGData()滚动你自己的 logo/qr 组合,并使用imagecopymerge在它上面应用你的 logo

$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
 

If you use endroid/qr-code library as "Prof" suggest above you can also use $pdf->Image from data stream:如果您使用endroid/qr-code库作为上面的“Prof”建议,您还可以使用来自数据 stream 的$pdf->Image

// 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 // '@' 字符用于表示跟随图像数据 stream 而不是图像文件名
$pdf->Image('@'.$data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM