Forum How do I...?

QR Code

lfiszer
Hello,

I need to include QR Code in PDF generated with Prince. The information to be encoded is present in HTML as an data-* attribute.

I tried to achieve it with a Javascript library (like this one http://d-project.googlecode.com/svn/trunk/misc/qrcode/js/) - unfortunately it turned out to be extremely slow. When run in the browser, the script takes around 20ms to generate a QRCode, but when run in Prince it takes about 6-7s (!). I tried many other libraries, as well as going for Datamatrix instead of QRCode (which is in theory faster to generate) but never been able to get times shorter than 2s.

Any ideas why it takes so long to calculate a QRCode using Prince? What Javascript engine are you using?

Also, I would appreciate any other idea for generating a QRCode inside a PDF document.

I know one could simply do:
<img src="qrcode.php?code=1234567890">
but in my case it is not possible.
mikeday
Yikes, QR code calculation looks complicated. We'll run some profiling tests with Prince and see if we can speed it up. Our JavaScript engine is not very optimised at the moment.
mikeday
With some work we can get it from 6 seconds down to 4.8 seconds; with more work we should be able to halve that before we start running into new roadblocks.
lfiszer
Thank you for your response. However, it's still to slow for me, as generating a complete document without the QRCode take me less than 1 sec. I will have to find some another solution to this problem.
mikeday
By changing the way we handle JavaScript arrays we have got it down to 1.4 seconds, which is better than I originally expected. It may be possible to get it down to just 1 second, we'll see how it goes. It's fun trying, anyway. :D
lfiszer
That sounds interesting! Do you plan to include these improvements in the next update of Prince?

Anyway, I was wondering, is there any particular reason that pushed your to create your own Javascript engine instead of using some open-source one like V8 or SpiderMonkey?
mikeday
The JavaScript updates will not be included in 8.1 maintenance releases, which are focused on bug fixing and stability. But we may release an early test version of Prince 9.0 before the end of the year.

Since we need to write our own layout engine and Document Object Model, it is convenient to have our own JavaScript interpreter that can be closely integrated with it. It is true the others have a head start, but we will catch up. :)
mikeday
We have made further optimisations to JavaScript execution and can now generate the QR code PDF in 0.99 seconds, which is probably as fast as it's going to get, for this year at least. :D
lfiszer
Great news ! Please let me know when the test version of Prince 9.0 is available.
mikeday
Prince 9 is available now, and includes faster JavaScript and faster DOM as well. The QR code test case now takes less than 0.5 seconds, and we have had a lot of fun with this test case. :D