DearS
DearS

Reputation: 192

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!

Upvotes: 3

Views: 12030

Answers (3)

Veve
Veve

Reputation: 6748

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();

Upvotes: 4

Matt Raines
Matt Raines

Reputation: 4218

Presumably the QR code is embedded in the PDF as an image. You could use a command-line tool such as pdfimages to extract the image first, then run your QRReader library on the extracted image. You might need a bit of trial and error to establish which image is the QR code if there is more than one image in the PDF.

See extract images from PDF with PHP for more detail.

Upvotes: 0

David J Eddy
David J Eddy

Reputation: 2037

You would want to convert the PDF to a supported image type, OR find a QR code reading library that supports PDFs. IMO the first option is easier. A quick search leads me to http://www.phpgang.com/how-to-convert-pdf-to-jpeg-in-php_498.html for a PDF -> img converter.

Upvotes: 0

Related Questions