How to Convert PDF to CSV in Salesforce Apex using

In this tutorial, we will show you how to convert a PDF document to CSV in Salesforce Apex using

Step 1: Create Remote Site Settings

Create a Remote Site Settings in the Salesforce Org like below. Add the link as the Remote Site URL.

Salesforce screenshot
Salesforce screenshot

Step 2: Create Apex Class in Salesforce

Click the Gear icon on the upper right corner and open the Developer Console.

Salesforce screenshot
Salesforce screenshot

In the Developer Console window, click on File –> New –> Apex Class. Write the class name DocumentParserOutputAsCSV and click OK. Now copy the DocumentParserOutputAsCSV code in this file.

Salesforce screenshot
Salesforce screenshot

Similarly, create a new file with the name DocumentParserOutputAsCSVTest and copy the code.

Step 3: Add API Key in the File

In the DocumentParserOutputAsCSVTest file, add your API Key. You can change the Destination file name which you want as a CSV.

Code screenshot
Code screenshot

Step 4: Verify Code

To verify the code, open the execute Anonymous window and call the method below.

Salesforce screenshot
Salesforce screenshot

Then, click on Execute.

Code execution window
Code execution window

Step 5: Search Files

Now, open the App Launcher to view the output file.

Salesforce screenshot
Salesforce screenshot

Step 6: Output CSV

Here’s the generated CSV output.

Salesforce screenshot
Salesforce screenshot

Source Code Files:


public class DocumentParserOutputAsCSV {
    String API_KEY = '***************';
    static string SourceFileUrl = '';
    static String destinationFileName = 'Test Document';
    String jsonOutput;
    public void parseDocumentAsCSV()
            // Create HTTP client instance
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            // Set API Key
            request.setHeader('x-api-key', API_KEY);
            Boolean async = false;
            String inline = 'true';
            String profiles = '';
            Boolean encrypt = false;
            Boolean storeResult = false;
            String generateCSVHead ='true';
            // PDF document password. Leave empty for unprotected documents.
    		string Password = '';
            // Prepare requests params as JSON
            // See documentation:
            // Create JSON payload
            JSONGenerator gen = JSON.createGenerator(true);
            gen.writeStringField('url', SourceFileUrl);            
            gen.writeStringField('templateId', '1');
            gen.writeStringField('outputFormat', 'CSV');
            gen.writeStringField('generateCsvHeaders', generateCSVHead);
            gen.writeBooleanField('async', async);
            gen.writeBooleanField('encrypt', encrypt);
            gen.writeStringField('inline', inline);
            gen.writeStringField('password', password);                       
            // Convert dictionary of params to JSON
            String jsonPayload = gen.getAsString();
            // URL of 'documentparser' endpoint
            string url = '';                        
            request.setHeader('Content-Type', 'application/json;charset=UTF-8');
            // Execute request
            HttpResponse response =  http.send(request);                
            if(response.getStatusCode() == 200)
                // Parse JSON response
                Map<String, Object> json = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
                if ((Boolean)json.get('error') == false)
                    jsonOutput = response.getBody();
                    System.debug('JSON :: '+ jsonOutput);    
                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();
    public static void saveDocument(String jsonResponse)
        ContentVersion cv = new ContentVersion();
        cv.Title = destinationFileName;
        cv.PathOnClient = 'TestDocument.csv';
        cv.VersionData = Blob.valueOf(jsonResponse);
        cv.IsMajorVersion = true;
        Insert cv;        


private class DocumentParserOutputAsCSVTest {
    @isTest static void testParseDocumentAsCSV()
        Test.setMock(HttpCalloutMock.class, new DocumentParseHttpCalloutMock());
        DocumentParserOutputAsCSV docParse = new DocumentParserOutputAsCSV();
    @isTest static void testParseDocumentAsCSVForCatch()
        DocumentParserOutputAsCSV docParse = new DocumentParserOutputAsCSV();
    public class DocumentParseHttpCalloutMock implements HttpCalloutMock {
        public HTTPResponse respond(HTTPRequest request) {
            // Create a fake response
            HttpResponse response = new HttpResponse();
            response.setHeader('Content-Type', 'application/json;charset=UTF-8');
            response.setBody('{"body": "companyName,companyName2","pageCount": 1,"error": false,"status": 200,"name": "sample-invoice.csv","remainingCredits": 60804}');
            return response;