This sample code will show how to extract data from PDF to Text, XML or CSV in JavaScript using Cloud API (low level).

Also, check this article to learn how to extract and convert spreadsheets between various file formats in JavaScript using Cloud API.

Sample.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>PDF Extractor Cloud API Example</title>
    <script src="pdf_extractor.js" type="text/javascript" encoding="UTF-8"></script>
</head>
<body>

    <form id="form1" enctype="multipart/form-data">
        <p>
            <label>Copy-paste your API Key for Bytescout.IO here</label>
            <input type="text" id="apiKey" placeholder="API Key" />
        </p>
        <p>
            <label>InputFile</label>
            <input type="file" name="file" id="inputFile" />
        </p>
        <p>
            <label>Extract</label>
            <select id="extractionType">
                <option value="0" selected="selected">to Text</option>
                <option value="1">to XML</option>
                <option value="2">to CSV</option>
                <option value="3">get Info</option>
            </select>
        </p>
        <p>
            <label>Page Index (zero-based)</label>
            <input type="number" id="pageIndex" value="0">
        </p>
        <button type="button" id="submit" onclick="return onSubmit()">Extract</button>
    </form>

    <div id="errorBlock" style="display: none">
        <h2>Error:</h2>
        <h4>Code: <span id="statusCode"></span></h4>
        <div id="errors"></div>
    </div>

    <div id="resultBlock" style="display: none">
        <h2>Result:</h2>
        <pre id="result"></pre>
    </div>

</body>
</html>

pdf_extractor.js

function onSubmit()
{
    // Hide result blocks
    document.getElementById("errorBlock").style.display = "none";
    document.getElementById("resultBlock").style.display = "none";

    // Get API Key
    var apiKey = document.getElementById("apiKey").value.trim();
    if (apiKey == null || apiKey == "") {
        alert("API Key should not be empty.");
        return false;
    }

    // UPLOAD FILE

    var url = "https://bytescout.io/api/v1/file/upload?apiKey=" + apiKey; // Get your API key by registering at http://www.bytescout.io
    var formData = new FormData(document.getElementById("form1"));

    var httpRequest = new XMLHttpRequest();
    httpRequest.open("POST", url, true);

    // Handle asynchronous response
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            // If OK
            if (httpRequest.status == 200) {
                var uploadedFileId = httpRequest.responseText;
                switch(document.getElementById("extractionType").value) {
                    case "0":
                        ExtractText(apiKey, uploadedFileId);
                        break;
                    case "1":
                        ExtractXML(apiKey, uploadedFileId);
                        break;
                    case "2":
                        ExtractCSV(apiKey, uploadedFileId);
                        break;
                    case "3":
                        ExtractInfo(apiKey, uploadedFileId);
                        break;
                }
            }
            // Else display error
            else {
                document.getElementById("errorBlock").style.display = "block"; // show hidden errorBlock
                // Display error
                document.getElementById("statusCode").innerHTML = httpRequest.status + " - " + httpRequest.statusText;
                document.getElementById("errors").innerHTML = httpRequest.responseText;
            }
        }
    }

    httpRequest.send(formData);

    return true;
}

function ExtractText(apiKey, uploadedFileId)
{
    var url = "https://bytescout.io/api/v1/pdfextractor/textextractor/extract?apiKey=" + apiKey;

    var pageIndex = document.getElementById("pageIndex").value;

    var options = {
        "properties": {
            "startPageIndex": pageIndex,
            "endPageIndex": pageIndex,
            "extractInvisibleText": "false"
        },
        "input": uploadedFileId,
        "inputType": "fileId"
    };

    // Prepare request
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    // Handle asynchronous response
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            // If OK
            if (httpRequest.status == 200) {
                document.getElementById("resultBlock").style.display = "block"; // show hidden resultBlock
                document.getElementById("result").innerHTML = httpRequest.responseText;
            }
            // Else display error
            else {
                document.getElementById("errorBlock").style.display = "block"; // show hidden errorBlock
                // Display error
                document.getElementById("statusCode").innerHTML = httpRequest.status + " - " + httpRequest.statusText;
                document.getElementById("errors").innerHTML = httpRequest.responseText;
            }
        }
    }
    // Send request
    httpRequest.send(JSON.stringify(options));
}

function ExtractXML(apiKey, uploadedFileId)
{
    var url = "https://bytescout.io/api/v1/pdfextractor/xmlextractor/extract?apiKey=" + apiKey;

    var pageIndex = document.getElementById("pageIndex").value;

    var options = {
        "properties": {
            "startPageIndex": pageIndex,
            "endPageIndex": pageIndex,
            "extractInvisibleText": "false"
        },
        "input": uploadedFileId,
        "inputType": "fileId"
    };

    // Prepare request
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    // Handle asynchronous response
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            // If OK
            if (httpRequest.status == 200) {
                document.getElementById("resultBlock").style.display = "block"; // show hidden resultBlock
                document.getElementById("result").innerText = httpRequest.responseText;
            }
            // Else display error
            else {
                document.getElementById("errorBlock").style.display = "block"; // show hidden errorBlock
                // Display error
                document.getElementById("statusCode").innerHTML = httpRequest.status + " - " + httpRequest.statusText;
                document.getElementById("errors").innerHTML = httpRequest.responseText;
            }
        }
    }
    // Send request
    httpRequest.send(JSON.stringify(options));
}

function ExtractCSV(apiKey, uploadedFileId)
{
    var url = "https://bytescout.io/api/v1/pdfextractor/csvextractor/extract?apiKey=" + apiKey;

    var pageIndex = document.getElementById("pageIndex").value;

    var options = {
        "properties": {
            "startPageIndex": pageIndex,
            "endPageIndex": pageIndex,
            "extractInvisibleText": "false"
        },
        "input": uploadedFileId,
        "inputType": "fileId"
    };

    // Prepare request
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    // Handle asynchronous response
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            // If OK
            if (httpRequest.status == 200) {
                document.getElementById("resultBlock").style.display = "block"; // show hidden resultBlock
                document.getElementById("result").innerHTML = httpRequest.responseText;
            }
                // Else display error
            else {
                document.getElementById("errorBlock").style.display = "block"; // show hidden errorBlock
                // Display error
                document.getElementById("statusCode").innerHTML = httpRequest.status + " - " + httpRequest.statusText;
                document.getElementById("errors").innerHTML = httpRequest.responseText;
            }
        }
    }
    // Send request
    httpRequest.send(JSON.stringify(options));
}

function ExtractInfo(apiKey, uploadedFileId)
{
    var url = "https://bytescout.io/api/v1/pdfextractor/infoextractor/extract?apiKey=" + apiKey;

    var options = {
        "input": uploadedFileId,
        "inputType": "fileId"
    };

    // Prepare request
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader("Content-Type", "application/json");
    // Handle asynchronous response
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            // If OK
            if (httpRequest.status == 200) {
                document.getElementById("resultBlock").style.display = "block"; // show hidden resultBlock
                document.getElementById("result").innerHTML = httpRequest.responseText;
            }
                // Else display error
            else {
                document.getElementById("errorBlock").style.display = "block"; // show hidden errorBlock
                // Display error
                document.getElementById("statusCode").innerHTML = httpRequest.status + " - " + httpRequest.statusText;
                document.getElementById("errors").innerHTML = httpRequest.responseText;
            }
        }
    }
    // Send request
    httpRequest.send(JSON.stringify(options));
}