Use the sample source code below to convert HTML to PDF in JavaScript using ByteScout Cloud API (low level).

It is also possible to convert PDF to HTML in JavaScript.

Sample.html

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

    <form id="form1">
        <p>
            <label>Copy-paste your API Key for Bytescout.IO here</label>
            <input type="text" id="apiKey" placeholder="API Key"/>
        </p>
        <p>
            <label>Input URL</label>
            <input type="text" name="inputUrl" id="inputUrl" placeholder="http://www.google.com/" />
        </p>
        <p>
            <label>or HTML code</label>
            <textarea id="inputHtmlCode"></textarea>
        </p>
        <p>
            <label>Orientation</label>
            <select id="orientation">
                <option>Portrait</option>
                <option selected="selected">Landscape</option>
            </select>
        </p>
        <p>
            <label>Margins:</label>
            <label>Left</label>
            <input type="number" id="marginLeft">
            <label>Top</label>
            <input type="number" id="marginTop">
            <label>Right</label>
            <input type="number" id="marginRight">
            <label>Bottom</label>
            <input type="number" id="marginBottom">
        </p>
        <button type="button" id="submit" onclick="return convertHtmlToPdf()">Proceed</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>
        <a id="result" href="" target="_blank"></a>
    </div>

</body>
</html>

html_to_pdf.js

function convertHtmlToPdf()
{
    // 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;
    }
    // Get input options
    var inputUrl = document.getElementById("inputUrl").value.trim();
    var inputHtmlCode = document.getElementById("inputHtmlCode").value.trim();
    var orientation = document.getElementById("orientation").value;
    var marginLeft = document.getElementById("marginLeft").value.trim();
    var marginTop = document.getElementById("marginTop").value.trim();
    var marginRight = document.getElementById("marginRight").value.trim();
    var marginBottom = document.getElementById("marginBottom").value.trim();

    // Prepare URL
    var url = "https://bytescout.io/api/v1/htmltopdf/convert";
    url += "?apiKey=" + apiKey; // Get your API key by registering at http://www.bytescout.io
    url += "&properties.orientation=" + orientation; // Set page orientation
    url += "&properties.margins.left=" + marginLeft; // Set left margin
    url += "&properties.margins.top=" + marginTop; // Set top margin
    url += "&properties.margins.right=" + marginRight; // Set right margin
    url += "&properties.margins.bottom=" + marginBottom; // Set bottom margin
    url += "&input=" + encodeURIComponent(inputUrl || inputHtmlCode); // Set input value
    url += "&outputType=link"; // Set output type Link

    // Prepare request
    var httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", url, true);
    // Handle asynchronous response
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4) {
            // If OK, display result
            if (httpRequest.status == 200) {
                var pdfFileLink = httpRequest.responseText;
                document.getElementById("resultBlock").style.display = "block"; // show hidden resultBlock
                var a = document.getElementById("result");
                a.setAttribute("href", pdfFileLink);
                a.innerHTML = pdfFileLink;
            }
            // 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();

    return true;
}