I was reading a thread in the Brazilian ColdFusion group where Vinícius Guimarães posted an interesting solution for printing barcode from a CF application.

First download the Barbecue Java library from SourceForge. Here is the link for the entire Barbecue Project.

Once you download the library, expand the zip file and copy the barbecue-1.5-beta1.jar to the folder: ColdFusion8\runtime\lib.

Ok, you are now ready to use barcode from ColdFusion, just need to create the component barcode.cfc below:{code type=xhtml}<cfcomponent>
<cffunction name=”getCode128″ returntype=”any” access=”remote” output=”true”>
<cfargument name=”data” type=”string” required=”true” />
<cfsetting showdebugoutput=”false”>
<cfscript>
Barcode = createObject(“java”, “net.sourceforge.barbecue.linear.code39.Code39Barcode”).init(data, false, true);
BarcodeImageHandler = createObject(“java”, “net.sourceforge.barbecue.BarcodeImageHandler”);
Barcode.setDrawingText(false);
Barcode.setBarHeight(50);
Barcode.setBarWidth(2);
context = getPageContext();
context.setFlushOutput(false);
response = context.getResponse().getResponse();
response.setContentType(“image/jpeg”);
BarcodeImageHandler.writeJPEG(Barcode, response.getOutputStream());
response.flush();
response.close();
</cfscript>
<!—Return the information as streaming bytes of type image/jpeg—>
<cfreturn>
</cffunction>
</cfcomponent>{/code}
Then, to call this component, here is a small test template:{code type=xhtml}<cfif structKeyExists(form, “barVar”)>
<cfoutput>
<img src=”barcode.cfc?method=getCode128&data=#form.barVar#” />
</cfoutput>
</cfif>
<form action=”barcodeTest.cfm” method=”post”>
Enter value: <input name=”barVar” type=”Text” size=”15″ />
<input type=”Submit” value=”GO” />
</form>{/code}
#barVar# is the number you want to print as a barcode.
Remember that each barcode reader reads a type of barcode, in this case we are using Code128.

18 thoughts on “Printing BarCode from ColdFusion

  1. How you install that on Coldfusion 8 running JRUN 4?
    Putting the JAR in the C:\JRUN4\lib folder hung everthing up!!!

  2. Geoffrey, to read the barcodes you need to print them and scan them with a barcode scanner. You can get them as cheap as $60 and plug them into to your USB port, prints directly to screen.

  3. Hi, thank you for your work!.
    When you use barbecue on Linux (Centos 5, Ubuntu) is not possible to drawing text below the barcode.
    I set Barcode.setDrawingText(true).
    Have You any idea?
    Thank you
    Angelo

  4. If you are looking to get a code128 barcode using this example, make sure to change the line:

    Barcode = createObject(“java”, “net.sourceforge.barbecue.linear.code39.Code39Barcode”).init(data, false, true);

    to

    Barcode = createObject(“java”, “net.sourceforge.barbecue.linear.code128.Code128Barcode”).init(data);

  5. Hi
    Ive tried this but all im getting is a blank screen after i click on submit
    I’ve put “barbecue-1.5-beta1.jar” in the correct location
    is there anything else in that zipfile that needs to go anywhere?

    any ideas?

    Thanks

  6. I have item codes on a csv file which i upload to a browser using coldfusion, using this item code im trying to generate a bar-code for it using the above code

  7. Ricardo:

    Seems pretty straightforward – put the .jar file in my Lib directory and the Barcode.cfc in the same directory as the demo file, but all I get is a failed graphic box with the red X in it, so it does not look as if the CFC is doing anything.

    Any thoughts?
    Thanks

  8. I was able to print the carcode, but if I want to place anuthing else in the output page, I cannot. Any idea of what need to be done, if the barcode is only one of many elements on the page ?

  9. The barcode part worked fine but I am getting the following error message:

    Any help is appreciated..

    The flush method was not found. Either there are no methods with the specified method name and argument types or the flush method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity. The error occurred on line 16.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.