How to Embed Barcodes in SSRS

For a particular project, there was a need to embed some sort of barcode in a few business forms (sales order, invoice, etc), which are generated by SSRS (Microsoft’s reporting server). I decided to standardize on QR code because of the quick readability on smart phones, which will eventually be used in some processes yet to be implemented. I have already created a proof of concept mobile application (using HTML5) that utilizes a barcode library that is capable of reading QR code so from an implementation standpoint, the use of QR code is no longer a “high risk” endeavor (i.e. there’s nothing foreseeable that would hold back this decision as a fundamental standard that could drive business processes).

One piece that I could leverage as a result of developing this project is to create a web service or a URL that would simply output the QR code in PNG format, which could be used for future applications and reports. For my purposes, I decided on using PHP as the QR code provider. The library I used is fromĀ http://phpqrcode.sourceforge.net/. It’s quick and easy to implement, however, the drawback is that documentation is very sparse. In this implementation, I used a simple query parameter to specify the data for the code, $_GET['d']:

header('Content-Type: image/png');  // Send back a header letting the client know you're sending back a PNG.
if($_GET['d']) {                                    // Get data for QR Code from user input
  require_once 'qrcode.class.php';   // Include the QR Code PHP library
  $filename = './tmp/qr_' . session_id() . ".png";  // Use a filename bound to the current user's session (you can
                                                    // randomize it further if you wish, but I chose to use a
                                                    // filename with the high probability of being overwritten by
                                                    // the currently sessioned user).
  QRcode::png($_GET['d'], $filename, QR_ECLEVEL_L, ($_GET['size'] ? $_GET['size'] : 3), ($_GET['margin'] ? $_GET['margin'] : 0));
  $body = file_get_contents($filename);             // Get the image file's contents.
  unlink($filename);                                // We're done with the image file, so delete it.
}
else {
  header("HTTP/1.0 400 Bad Request");
}
echo $body;  // Send it out.

I included parameters for size and margin just in case. Another thing to note is that there are size limits for the URLs (and thus the GET parameters), so if you are working with particularly large data (such as international addresses, which could potentially get large), you may want to consider using $_POST instead.

On the SSRS side, implementation is straight forward. You’ll simply add an image component to your report that points the the URL above:

  1. Create a new report parameter or use an existing field (e.g. QRCodeData); this will be the data used for the image source below.
  2. Add an Image component.
  3. Change image source to External.
  4. Create a formula that will point to the above PHP file:
    ="http://my.server.com/qr-code.php?d=" + Fields!QRCodeData.Value

You now have a generic QR code generator that you can use across all of your project applications and reports (and beyond).