Search code examples
phppdfqr-code

PHP QR Code scan from PDF file


I have a PDF file with a QR Code. Iuploaded the file on the server folder named "tmp" and now I need to scan and convert this QR via PHP.

I found this library:

include_once('lib/QrReader.php');
$qrcode = new QrReader('/var/tmp/qrsample.png');

$text = $qrcode->text(); //return decoded text from QR Code
print $text;

But this works only for png/jpeg files. Is there any way to scan PDF ? Is there any way to convert PDF to png only the time that I need ?

Thank you!


Solution

  • First, transform your PDF into an image with Imagick, then use your library to decode the QRcode from it:

    include_once('lib/QrReader.php');
    //the PDF file
    $pdf = 'mypdf.pdf';
    //retrieve the first page of the PDF as image
    $image = new imagick($pdf.'[0]');
    //pass it to the QRreader library
    $qrcode = new QrReader($image, QrReader::SOURCE_TYPE_RESOURCE);
    
    $text = $qrcode->text(); //return decoded text from QR Code
    print $text;
    //clear the resources used by Imagick
    $image->clear();