CakePHP: download generated QR Code

2019-08-08 22:12发布

问题:

I'm starting a CakePHP Helper based on phpqrcode. My problem is that i can't get the generated png or svg file and force the browser to download it.

i want to when a person submit his text via Ajax, i generate a QR Code for him and force the browser to download it without saving the file on the server.

Here is a short example of the Helper:

App::import('Vendor', 'QRGenerator.phpqrcode'.DS.'qrlib');

class QRHelper extends AppHelper{

    function text($content= '') {

        QRcode::png($content);

    }   
}

In my view file:

<?php $this->QR->text('example text'); ?>

And my layout:

<?php  echo $this->fetch('content'); ?>

Thanks.

回答1:

Try this in your controller:

    $this->response->type('Content-Type: image/png');
    $this->response->download('qrcode.png');


回答2:

Try with return or echo QRcode::png($content); in text()

  function text($content= '') {

       return QRcode::png($content);

    }


回答3:

Try with QR Code Helper:

  • Copy the "QrCodeHelper.php" to your "app/View/Helper" folder.

  • In the Controller add 'QrCode' to your helpers array.

  • In the view do for example:

    <? echo $this->QrCode->text('Hello World'); ?>