Printing a Salesforce Record to a DYMO Printer

Sometimes you might need to print records in Salesforce onto label. You have a few options. You could print them in batches using a custom Visualforce page, or a merge tool such as CongaMerge or Drawloop. For my use case, though, I needed to be able to print one-off labels on demand. DYMO has a JavaScript library that can interact directly with DYMO printers installed on the computer. To use, it, you must first install the DYMO software onto your computer. After installing it, make sure to close your browser completely and then open it again – watch out for background processes – they caused me a lot of grief because I couldn’t get it to work until I found all the background Chrome processes running.

You can get the latest version of the DYMO JavaScript Library here: http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js. DYMO doesn’t recommend using that URL for production use. Instead, you should download it and upload it as a static resource in Salesforce.

I wanted to create a custom button on a Case that when clicked would print a label with the case number in a barcode format. Accessing static resources from custom JavaScript button is a little tricky, but I found this hack to get it to work: http://jevonearth.blogspot.com/2011/08/using-static-resources-with-salesforce.html.

The code for the JavaScript is fairly simple. It builds the label xml, then prints it to the first DYMO label printer that it finds. You can change what is printed by modifying the label xml. There are a lot of examples on the DYMO blog.

{!REQUIRESCRIPT('/resource/' & LEFT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TEXT(NOW()),':',''),'-',''),' ',''),10) & '000/DymoLib')}
 
try
{
// open label
 
var labelXml = '<?xml version="1.0" encoding="utf-8"?>' +
'<DieCutLabel Version="8.0" Units="twips">' +
'<PaperOrientation>Landscape</PaperOrientation>' +
'<Id>Address</Id>' +
'<PaperName>30252 Address</PaperName>' +
'<DrawCommands>' +
'<RoundRectangle X="0" Y="0" Width="1581" Height="5040" Rx="270" Ry="270" />' +
'</DrawCommands>' +
'<ObjectInfo>' +
'<BarcodeObject>' +
'<Name>Barcode</Name>' +
'<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />' +
'<BackColor Alpha="0" Red="255" Green="255" Blue="255" />' +
'<LinkedObjectName></LinkedObjectName>' +
'<Rotation>Rotation0</Rotation>' +
'<IsMirrored>False</IsMirrored>' +
'<IsVariable>True</IsVariable>' +
'<Text>' + '{!Case.CaseNumber}' + '</Text>' +
'<Type>Code39</Type>' +
'<Size>Medium</Size>' +
'<TextPosition>Bottom</TextPosition>' +
'<TextFont Family="Arial" Size="7.3125" Bold="False" Italic="False" Underline="False" Strikeout="False" />' +
'<CheckSumFont Family="Arial" Size="7.3125" Bold="False" Italic="False" Underline="False" Strikeout="False" />' +
'<TextEmbedding>None</TextEmbedding>' +
'<ECLevel>0</ECLevel>' +
'<HorizontalAlignment>Center</HorizontalAlignment>' +
'<QuietZonesPadding Left="0" Top="0" Right="0" Bottom="0" />' +
'</BarcodeObject>' +
'<Bounds X="331" Y="345.600006103516" Width="4386.5" Height="720" />' +
'</ObjectInfo>' +
'</DieCutLabel>';
var label = dymo.label.framework.openLabelXml(labelXml);
 
// select printer to print on
// for simplicity sake just use the first LabelWriter printer
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0)
throw "No DYMO printers are installed. Install DYMO printers.";
 
var printerName = "";
for (var i = 0; i < printers.length; ++i)
{
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter")
{
printerName = printer.name;
break;
}
}
 
if (printerName == "")
throw "No LabelWriter printers found. Install LabelWriter printer";
 
// finally print the label
label.print(printerName);
}
catch(e)
{
alert(e.message || e);
}