Generate Barcode - Salesforce
Barcode Generator sample in Salesforce demonstrating ‘Generate Barcode’
GenerateBarcode.cls
public class GenerateBarcode {
String API_KEY = 'YOUR_PDF_CO_API_KEY_HERE';
String endPointUrl = 'https://api.pdf.co/v1/barcode/generate';
public void generateBarcode() {
try {
String jsonBody = '{ "name": "barcode.png", "value": "abcdef123456", "type": "QRCode", "async": false }';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('x-api-key', API_KEY);
request.setEndpoint(endPointUrl);
request.setHeader('Content-Type', 'application/json');
request.setMethod('POST');
request.setBody(jsonBody);
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200) {
System.debug('Barcode generated successfully: ' + response.getBody());
} else {
System.debug('Error Response ' + response.getBody());
System.Debug(' Status ' + response.getStatus());
System.Debug(' Status Code' + response.getStatusCode());
System.Debug(' Response String' + response.toString());
}
} catch (Exception ex) {
String errorBody = 'Message: ' + ex.getMessage() + ' -- Cause: ' + ex.getCause() + ' -- Stacktrace: ' + ex.getStackTraceString();
System.Debug(errorBody);
}
}
}
GenerateBarcodeTest.cls
@isTest
private class GenerateBarcodeTest {
static testMethod void testGenerateBarcode() {
Test.startTest();
Test.setMock(HttpCalloutMock.class, new GenerateBarcodeTest.GenerateBarcodeMock());
GenerateBarcode generateBarcode = new GenerateBarcode();
generateBarcode.generateBarcode();
Test.stopTest();
}
public class GenerateBarcodeMock implements HttpCalloutMock {
public HttpResponse respond(HTTPRequest req) {
HttpResponse res = new HttpResponse();
String testBody = '{"url":"https://pdf-temp-files.s3.us-west-2.amazonaws.com/barcode/barcode.png","name":"barcode.png","error":false,"status":200}';
res.setHeader('Content-Type', 'application/json');
res.setBody(testBody);
res.setStatusCode(200);
return res;
}
}
}
PDF.co Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.
Download Source Code (.zip)
return to the previous page explore Barcode Generator endpoint
Copyright © 2016 - 2023 PDF.co