1

This script works fine. The issue I'm having is trying to get it to generate multiple barcodes per page. It will only generate the first one. the rest are blank images.

<cfoutput>
<cfset x = 0>
<cfloop index="price_tag" from="1" to="#arrayLen( session.tags )#">
 <cfset x = x + 1>
<cfscript>
code128= createobject("java","com.lowagie.text.pdf.Barcode128");
code128.setCodeType(code128.CODE128);
/* Set the code to generate */
code128.setCode("#Session.tags[price_tag].itemnum#");
color =  createobject("java","java.awt.Color");
image = code128.createAwtImage(color.black, color.white);
bufferedImage = createObject("java", "java.awt.image.BufferedImage");
bufferedImageType = bufferedImage.TYPE_BYTE_GRAY;
bufferedImage = bufferedImage.init(image.getWidth(JavaCast("null", "")),image.getHeight(JavaCast("null", "")), bufferedImageType);
graphics2D = bufferedImage.createGraphics();
graphics2D.drawImage(image,0,0,JavaCast("null", ""));
barcodeImage = imageNew(bufferedImage);
</cfscript>   


         <div style="margin:0px; padding:0px;">
            <div style="font-size:42px; font-weight:bold; margin-bottom:0px; margin-right: 10px; margin-top: -5px;">#Session.tags[price_tag].item_name#</div>
            <div style="font-size:32px; margin-bottom:0px; margin-top:-7px; margin-right: 10px;">#Session.tags[price_tag].item_brand#</div>
           <div style="font-size:24px; margin-bottom:0px; margin-top:-7px;">#Session.tags[price_tag].item_unit_size# #Session.tags[price_tag].item_unit_type# </div>
            <div style="font-size:120px; font-weight:bolder; margin-top:-55px; margin-right: 10px;" align="right">#DollarFormat(Session.tags[price_tag].item_retail)#</div>
            <div style="font-size:30px; font-weight:bolder; margin-top:-30px; margin-right: 10px;" align="right"> #Session.tags[price_tag].item_sold_by#</div>
            <div style="font-size:30px; margin-top:-35px; margin-left 10px;" align="left">#Session.tags[price_tag].itemnum#</div>
            <div style=" position:absolute; margin-left: 10px; margin-top: 100px;"><cfimage action="writeToBrowser" source="#barcodeImage#" format="png" quality="1" width="180px" overwrite = "yes"></div>
         </div>   
   <cfif #arrayLen( session.tags )# / #x# NEQ 1>                            
   <cfdocumentitem type="pagebreak"/> 
   </cfif>
</cfloop>
</cfoutput>
  • Output various variables such as x and price tag inside the script. That will help you determine what is happening. Probably not related to your problem, but you can create your java objects before you start your loop and use them inside the loop. – Dan Bracuk Feb 03 '17 at 12:39
  • Instead of using ``, track that as `#price_tag#`. It's one less variable to deal with. – Jules Feb 03 '17 at 16:05
  • Try taking out `overwrite = "yes"`. I don't think you need that when you `action="writeToBrowser"`. – Jules Feb 03 '17 at 16:08
  • CFDocument has always been a little quirky. I suspect is may be related to this: `
    – Leigh Feb 03 '17 at 16:14

1 Answers1

0

It's probably because you are using action="writeToBrowser" in a loop within cfdocument. It's not going to a browser.

Try writing real files, then linking to them. I've done that and know it works. Make sure you use a distinct file name in that loop.

If you plan to run this routine over and over, it's safe to use the same file names. But in your code disable caching of the image src with a url var:

<cfoutput>
<img src="/media/cms/code-#price_tag#.png?rand=#randRange(0,1000)#" width="180"/>
</cfoutput>
Jules
  • 1,941
  • 15
  • 18
  • *It's not going to a browser.* That really should not make a difference. Ultimately `action=writeToBrowser` does exactly what you are suggesting: creates a physical file and outputs a plain tag with a URL pointing to that file. So it *should* work as is. – Leigh Feb 03 '17 at 16:43
  • Just reading through this:. http://stackoverflow.com/questions/17372320/cfimage-from-binary-in-cfdocument maybe it has something to do with the path name that gets generated. Creating a true file and linking to is explicitly would get around that possible issue. – Jules Feb 03 '17 at 16:48
  • Yes, if it were a path problem, you usually see a red "X". In this case, the result is nothing at all. So it is probably not the same issue. A seemingly blank image suggests either the image is positioned in the wrong place, or *possibly* it isn't fully rendered. Since removing the `position: absolute` seems to work in a few quick tests, my guess is this is "quirk" with how cfdocument parses the html. (Side note, re: *it's safe to use the same file names* - It is not safe to re-use file names in multi-threaded apps, as one thread could overwrite a file being used by another). – Leigh Feb 03 '17 at 17:03