Friday, June 22, 2012

piwik stats for your website

After setting up piwik on your server, here is some simple code to get your website to send some usage data. In the .jsp load the piwik script:
<script type="text/javascript">
      var pkBaseURL = (("https:" == document.location.protocol) ?"https://[your-server-name]/" : "http://[your-server-name]/");
      document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
Then, to tack something in you .javascirpt, the code will look something like this:
var siteId = 1;  // check piwik website settings to get siteId needed for the app
if (typeof pkBaseURL === 'string') {
   try{
 // setup any variables you want to track
 var customVariable1 = 'test';
 // can track up to 5 custom variables
          var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", siteId);
          piwikTracker.setCustomVariable (1, 'customVariable1', customVariable1, 'page');
          piwikTracker.setCustomVariable (2, 'customVariable1', customVariable1, 'page');
          piwikTracker.setCustomVariable (3, 'customVariable1', customVariable1, 'page');
          piwikTracker.trackPageView();
          piwikTracker.enableLinkTracking();
   } catch( err ) {}
}
That's it, pretty simple...

Thursday, June 21, 2012

Image + Text in iText PdfPCell

Recently, while working on the project involving creating PDFs with iText library, I needed to create PDF with cells that have image and text inside of them. After some searching and playing around, the following piece of code solved the problem:
Paragraph paragraph = new Paragraph();
if (image != null) {
      image.scalePercent(90f);
      paragraph.add(new Chunk(image, -1f, 1f));
}
font.setColor(WebColors.getRGBColor(fontColor));
paragraph.add(new Phrase(text, font));
PdfPCell cell = new PdfPCell(paragraph);
cell.setBorder(Rectangle.LEFT);
cell.setBorder(Rectangle.RIGHT);
cell.setBorderColor(WebColors.getRGBColor(BORDER_COLOR));
cell.setBackgroundColor(WebColors.getRGBColor(backgroundColor));
table.addCell(cell);
One thing to notice, initially, I had an issue with image stretching to the height of the cell. The fix for that problem is in RED in the code above.