Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF QR Code is different all the time

I've got TCPDF installed in CakePHP 1.3 and am using it to generate a PDF of a QR code. It's a very simple function that creates a A9 sized page, two strings and one QR code. The QR code is for a simple url (ie. 'example.com/12345').

I can load the page in a browser (Safari using it's built in PDF reader.) and each time I reload it, the QR code changes slightly. The module dimensions are the same but the data elements change significantly. I'm using the "L" data redundancy setting. The string is exactly the same each call but the QR changes. After watching a number of reloads I believe it cycles through only 3 or 4 variations.

Now here's the really great part, it scans correctly. Each permutation of the QR results in exactly the same url in the QR Reader (i-Nigma on an iPhone4)

Any ideas? Am I doing or missing something really dumb and obvious? Thanks in advance!

like image 300
Dan Berlyoung Avatar asked Aug 26 '10 00:08

Dan Berlyoung


1 Answers

Seems that the QR Code generation library used by TCPDF uses a slightly 'lazy' method of creating the QR code. It randomly chooses from any of 8 'mask' patterns to see if it makes the code any better. If it meets a certain criteria, it uses the randomly chosen mask. That results in possibly different QR codes each time.

Apparently Google uses a better algorithm to choose the mask. Probably checks all the masks and chooses the best one. This would result in a predictable outcome instead of a randomly selected one.

I'm going to call the Google Chart API to retrieve a PNG and then place that in my PDF instead of using the built in QR generator.

like image 94
Dan Berlyoung Avatar answered Sep 24 '22 13:09

Dan Berlyoung