Check me out on Twitter!
Intelligent Mail barcode – iText and barcode4j
•January 6, 2009 • 2 CommentsI use a java library called iText to create PDFs and do other PDF related tasks when I am building web applications, and i embed a lot of barcodes for various purposes. iText handles most of these very well, but I recently needed to replace the old USPS barcode (postnet) with the new barcode (intelligent mail). iText doesn’t support it, but with some helpful direction I implemented it using another library called barcode4j. Here’s the method I wrote that you can pretty much copy and use directly:
/**
* Create a USPS Intelligent Barcode using Barcode4J and add it to iText document.
* @param code barcode value
* @param cb overcontent from stamper
* @param fieldPositions stamper.getAcroFields().getFieldPositions(“…”) for the placeholder field in PDF template
*/
public static void createUspsIntelligentBarcode(String code, PdfContentByte cb, float[] fieldPositions) {
if ((code.length() != 20) && (code.length() != 25) && (code.length() != 29) && (code.length() != 31)) {
throw new RuntimeException(“UspsIntelligentBarcode: code length of ” + code.length() + ” is invalid.”);
}
//Note: fieldPositions data = [page, llx, lly, urx, ury]
float height = (fieldPositions[4]-fieldPositions[2]);
float width = (fieldPositions[3]-fieldPositions[1]);
PdfTemplate tp = cb.createTemplate(width, height);
// Create the graphics and canvas objects that will contain the barcode.
Graphics2D g2 = tp.createGraphics(width, height);
g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2.scale(2.5, 2.835); // (1mm == 2.835 points) + tweaking on x to make bar width correct
Java2DCanvasProvider provider = new Java2DCanvasProvider(g2, 0);
// Create Barcode4J barcode on canvas
USPSIntelligentMailBean barcode = new USPSIntelligentMailBean();
// use 70% of converted values. Measurements on printouts needed to be tweaked.
barcode.setAscenderHeight(UnitConv.in2mm(0.1f) * 0.7f); // height of ascender/descender per USPS spec
barcode.setTrackHeight(UnitConv.in2mm(0.05f)* 0.7f); // height of track bar per USPS spec (smallest bar of barcode)
barcode.setBarHeight(UnitConv.in2mm(0.145f)* 0.7f);
barcode.setIntercharGapWidth(UnitConv.in2mm(0.04f)* 0.7f * 1.13f); // adjust again for scaling
barcode.generateBarcode(provider, code);
g2.dispose();
// adjust x/y to perfect location for envelope window
float inch = 72f; // itext manual pg 33
float xCoordinate = fieldPositions[1] – ((1f/8f) * inch);
float yCoordinate = fieldPositions[2] – ((3f/32f) * inch);
cb.addTemplate(tp, xCoordinate, yCoordinate);
}
This lets you put the intelligent mail barcode (or any barcode in barcode4j) directly into iText. The key here is that the PdfTemplate in iText has a method that creates a generic Java Graphics2D object that is accessible for drawing by anything. This generic object is exactly what is needed to be passed into the barcode4j canvas provider, and then any barcode4j barcode is output to the canvas. A couple tweaks to position it however it is needed, and that is it. It was a lot simpler than I expected when I realized I couldn’t just use iText for the whole thing. Gotta love open source!
jsp tomahawk HtmlInputCalendar
•December 10, 2008 • Leave a CommentI had an ongoing issue recently with pop-up javascript calendars in the tomahawk jsp library. Hopefully this will save some trouble for someone. The problems I was having were:
- sometimes the calendar pop-up was not selectable, and
- when it was selectable, the pop-up would show up at a distorted location on the page and the tables would be messed up.
Being new to debugging this, my first assumption was that there would be protections built into this packaged code so that my CSS and whatever else would not affect it. So, to keep this post short and to the point, here is how I fixed it:
- An eventual look at the W3C HTML 4.1 Reference showed that this element was difficult to put directly inside a label, inside a fieldset. The problem is that the
labelelement only supports one input inside, so the additional calendar pop-up button is unusable and simply ignores any click on it and instead puts focus on theinputelement. I ended up fixing this one by simulating alabelelement using a DIV and CSS. (Note: at B6 Systems, we have developed an in-house jsp framework that we call PureFaces that allows writing JSP pages completely in Java code. So I wrote my own components for the fieldsets and labels. It is intended to be open source eventually and will be on the B6 website) - The second problem took a little longer, and eventually I fixed it without really knowing why what I did worked. I used a lot of the same techniques for debugging CSS that are shown in many places. Eventually I found that I had a
position: relative;style on my content wrapper that was totally throwing off the built-in CSS for the pop-up. The reason I don’t know why that fixed it was that I could not find anything in the built-in CSS that seemed to care about position. But, results speak loudly. I was able remove and work around that style. The second part of this one (the bad formatting of the pop-up) was related to having thetableelement way too globally defined in the CSS. Adding more specific selectors quickly solved that one.
javascript testing
•December 8, 2008 • Leave a CommentI have been messing around with javascript testing lately and i’ve had some issues. I have been attempting to incorporate jsunit into JUnit (per the minimal instructions on the website) and have been unsuccessful. emails to the site owner go unanswered.
The usage seems to be directed towards building via ANT/XML in something like CruiseControl, but I am trying to put it directly into a JUnit testcase that can be part of all of the other tests I run.
Here’s a summary of what i’m doing right now, and the problems i’m having:
- Create an instance of StandaloneTest, and supplying it with an implementation of ConfigurationSource
- Set up the test to be one that comes with the jsunit package… so that i KNOW it is formatting correctly. (This is the html file with the javascript tests and asserts in it)
- run the unit test.
Step 3, of course, is what fails. The browser I selected launches, and the java console says the everything is running and that it is waiting for a response.
Then it times out… I’m not sure what is going on here, but it seems that either the browsers are not sending a response, or JUnit is not able to catch it.
Any ideas?
new blog
•December 8, 2008 • Leave a CommentThis is the first post on geekin! It may take me a while to get things rolling, but i plan on covering topics that relate to my work. I’m a java developer at B6 Systems in Pittsburgh, and I also handle a lot of web development. So this blog will cover a wide range of topics like java, web scripting, web design, jsp, whatever else I can think of.
disclaimer: I’m new to web development. If my ideas seem strange or misguided, make a comment. i’d love to find out how to do whatever it is better.
