How can we generate barcodes for a VF page rendered as a pdf document. I am not looking for an external website as that might stop working in future. I have seen Gimbal paid app but was wondering if any one knows about of any native solution or are aware of any other appexchange package.
1 Answer
I have personally written a barcode component that works with PDF. It's actually pretty easy. The actual component is self-contained:
<c:barcode codeValue="12345" />
Edit: Please be careful with this code; it was an early prototype for what was eventually included in production code, but some bugs may exist and minimal error handling. I also no longer have a scanner to verify the output, so some testing is prudent. In addition, this only handles Code39 output, so if you need a different barcode method, you'll have to start from scratch.
<apex:component controller="code39" access="global">
<apex:attribute assignTo="{!shouldCheckDigit}" type="boolean" name="checkDigit" default="false" description="True if should append additional check character." />
<apex:attribute assignTo="{!sourceCodeValue}" type="string" name="codeValue" description="The string to encode." />
<apex:stylesheet value="{!URLFOR($Resource.barcodeCSS)}"/>
<div style="display: inline-block; border: 1px solid black; min-width: 1in; padding: 0.25in 1in;" >
<div>
<apex:repeat value="{!barCodeString}" var="bar">
<apex:outputText value="" styleClass="{!if(bar='1','blackbar','whitebar')}" />
</apex:repeat>
</div>
<div style="text-align: center">
{!sourceCodeValue}
</div>
</div>
</apex:component>
public class code39 {
public Boolean shouldCheckDigit { get; set; }
public String sourceCodeValue { get; set; }
static String keys = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*';
static String[] values = new String[] {
'1010001110111010', '1110100010101110', '1011100010101110', '1110111000101010',
'1010001110101110', '1110100011101010', '1011100011101010', '1010001011101110',
'1110100010111010', '1011100010111010', '1110101000101110', '1011101000101110',
'1110111010001010', '1010111000101110', '1110101110001010', '1011101110001010',
'1010100011101110', '1110101000111010', '1011101000111010', '1010111000111010',
'1110101010001110', '1011101010001110', '1110111010100010', '1010111010001110',
'1110101110100010', '1011101110100010', '1010101110001110', '1110101011100010',
'1011101011100010', '1010111011100010', '1110001010101110', '1000111010101110',
'1110001110101010', '1000101110101110', '1110001011101010', '1000111011101010',
'1000101011101110', '1110001010111010', '1000111010111010', '1000100010001010',
'1000100010100010', '1000101000100010', '1010001000100010', '1000101110111010' };
public String[] getBarCodeString() {
return generateCode39(sourceCodeValue, shouldCheckDigit).split('');
}
public String getBarCodeSource() {
return generateCode39(sourceCodeValue, shouldCheckDigit);
}
String generateCode39(String source, Boolean checkDigit) {
String[] result = new String[0];
Integer index, total = 0;
Integer[] keyValues = new Integer[0];
source = source == null? '': source;
result.add(values[keys.indexOf('*')]);
for(String sourceChar: source.toUpperCase().split('')) {
if((index = keys.indexOf(sourceChar)) > -1) {
result.add(values[index]);
total += index;
}
}
if(checkDigit) {
result.add(values[Math.mod(total, 43)]);
}
result.add(values[keys.indexOf('*')]);
return String.join(result,'');
}
}
barcodeCSS (Static Resource)
.blackbar, .whitebar {
display: inline-block;
overflow: hidden;
padding: 0;
width: 1px;
height: 1cm;
}
.blackbar {
background-color: black;
}
.whitebar {
background-color: white;
}
Also, no unit tests for this, as it was a prototype. However, it should be functional for basic use cases, including embedding in a PDF.
-
1Even if you don't include the code, it would be nice to hint at the design approach so the OP can benefit from your learning. :)– Phil WNov 30, 2020 at 17:26
-
1@philw fair enough. I'll share more when I get home from my appointment.– sfdcfoxNov 30, 2020 at 18:02
-
@sfdcfox Thanks for the detailed response and code snippets. Will try these out. This is very helpful. Dec 1, 2020 at 3:38
-
2@sfdcfox to make it more complete -- can you please add barcodeCSS static resource? Thanks!– kurunveDec 1, 2020 at 9:31
-
1