Estás leyendo:
Informática

Grails: print QRCodes with qrcode 0.3 plugin nested into rendering 0.4.3 plugin


The output app side all ways forever scared me: since building a printer output “bit by bit” with escape codes into a MS-DOS app, to make a complex print form helped by tools like Cristal reports, It’s evident for me the output is a boooring task.

But all has changed when I had discovered any of the output plugins set of grails environment: export, rendering and the fantastic qrcode. With those tools the making of a output is a easily and quickly thing.

Following the dependence injection of the Grails paradigm, We have only declare the  instance plugin into our controller and Springs make it the rest. (qrcode and rendering only have it to make the plugin installation into the project context)

But stop us the theory and go to the cake: the render of a QRCode is whatever a simply question: only we need the well installed plugins, one action which will be called from the view:

   def printFragmentosQrCode = {
             def entradaInstance = Entradas.get(params.id)         
             renderPdf(template: "/printTest",model:[Objects2QRList:entradaInstance.fragmentos,entradaInstance:entradaInstance],filename: "fragsQRCodes.pdf")
        }

and a GSP template wich has the rendering responsibility:

<%@ page contentType="text/html;charset=UTF-8" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Sample title</title>
    </head>
    <body>
        <g:each in="${Objects2QRList}" var="o" >
            <qrcode:image width='o?.size' text="${o?.encodeAsHTML()}" />
        </g:each>
    </body>
</html>

If we analyze the code, It looks simple; the action take a objects collection (entradaInstance.fragmentos) and send it to the template with a name of output pdf file (also may be a png). Into template, It generate the plugin call by the insert of the specific tags, the <qrcode> tag;

<qrcode:image width="90" text="${o?.referencia.encodeAsHTML()},${o?.tipoFragmento.nombreProcesado}" />

The rendering plugin have to create a xml file and finally transform it in a pdf with a serie of png 2D codes. Unfortunately, the nesting plugins generate a xml ‘not well formed’ error with s entity:

"The reference to entity “s” must end with the ';' delimiter exception"

Googling I could see the xml error was a problem relates with the bad resolving by xml parser of the ‘&’ special character. Anyway, my problem don’t was a self code problem but was a plugin code ‘bug’ (at least a not well formed XML disfunction)

The advantage of free software let me to see the source code of qrcode plugin files. A plugin has a similar structure that whichever grails project: controller(s), view(s),  tag library(s) an more…It was easy to find the origin of my problem:

It was a ‘bug’ in the building of qrcode image url. The ‘s’ entity it’s really the size property of the image (and the size attribute of  <qrcode> tag) as I could see in the ‘QRCodeTagLib.groovy’ file:

  def image = { attrs ->
    def size = attrs.height?:attrs.width
    String text = attrs.text
    String src = createLink(controller:'qrcode',action:'text',params:[text:text,s:size])
    out << """ <img class="qrcode" src="${src}" alt="${text}" />"""
}

The src attribute of the <img> tag was valued by $src variable, wich was generated into the own closure by a call to ‘text’ action of controller (with the createLink() groovy function). Then, there was the problem.

I suppose the bug could be a bad generation of url image string, more exactly with the ‘&’ characters of the concatenation of the GET params request. Then, the solution was  replace those with the appropiate equivalence ‘&amp;’.

For this, I used the replaceAll() groovy function and the result was:

def image = { attrs ->
    def size = attrs.height?:attrs.width
    String text = attrs.text
    String src = createLink(controller:'qrcode',action:'text',params:[text:text,s:size])
     String src2=src.replaceAll(/&/,'&')
    out << """ <img class="qrcode" src="${src2}" alt="${text}" />"""
    }

Yeah! I did!

Don’t remember to modificate the others tags into tagLib plugin.

Regards squids.

Discussion

One thought on “Grails: print QRCodes with qrcode 0.3 plugin nested into rendering 0.4.3 plugin

  1. Great post but I was wanting to know if you could write a litte more on this subject?

    I’d be very thankful if you could elaborate a little bit further.
    Many thanks!

    Posted by Bee Removal Spray Lenasia | 1 May, 2014, 4:44

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Lo leyeron y comentaron

Top Clicks

  • None

Flickr Photos







More Photos
%d bloggers like this: