Coding with ScriptX.Services
for Windows PC

This page provides an introduction to using your own ScriptX.Services for Windows PC to print pages from your own web apps.

ScriptX.Services for Windows PC is a local only web server that listens on a local loopback address (127.0.0.1) or localhost: and custom port (default 41191). Your web app will not be hosted on the same domain or server as ScriptX.Services for Windows PC.

This means that even if you know the local loopback address port for ScriptX.Services for Windows PC, calls to the server will be blocked unless a valid ScriptX client license is used.

Client PC Installation

  1. With ScriptX.Services for Windows PC, the service must first be installed to each device (Microsoft Windows Intel x64 PC only). Unlike with ScriptX.Add-on for Internet Explorer this has to be a separate process; it cannot be automated using a .cab based installer that is referenced by and delivered with the page.

    Javascript can be used to detect if a service is available and if not the user then redirected to a suitable page that assists with installation of the required applications/services.

  2. A valid MeadCo ScriptX Client license must be referenced on any page that wishes to use the local ScriptX.Services for Windows PC. The license is referenced using script.

ScriptX.Services for On-Premise Devices

ScriptX.Services for On-Premise Devices is a 'one-time' install to a suitable server. Updates to the system are to the single server and client library updates are automatically delivered to client devices: all the advantages of web based delivery.

The disadvantage may be ensuring the abilility to address required printers, but if printers are centralised ScriptX.Services for On-Premise Devices may provide a compelling alternative.

Please take a look at our on-premise services for more details.

If you are a SASS provider with a public website providing services to customers with several users from each customer then customers may prefer to install ScriptX.Services for On-Premise Devices at their premises. All you will need to do is to determine a way for each customer to configure the javascript client code delivered by your app with their own On-premise server address. Our samples system illustrates this.

To deliver a print experience to your users any page in your app that provides printing of the page must:

  1. Reference the ScriptX.Services javascript library and dependencies (or write your own code to use the  Web API),
  2. Add code to configure the library - including the MeadCo ScriptX Client license to use,
  3. Add code to describe required settings such as margins, headers and footers,
  4. Add a print button or other appropriate UI to your page,
  5. Connect the print button click even, or appropriate UI event to the ScriptX.Services code using your favourite technique,
  6. Done!

A worked example - this page

We'll do things in a slightly different order.

This page has a 'Download to PDF' button, as do other pages on the site. The download is obtained by using Cloud ScriptX.Services to `print` this document to a PDF and then provide the `printed file` as a download.

Working with ScriptX.Services for Windows PC is just (almost) the same, but with ScriptX.Services for Windows PC the printer may be a physical printer connected to the PC so there would be no need for a download to the user.

When this page starts, it connects to the ScriptX.Services for Cloud, later we will add the ability to connect to a ScriptX.Services for Windows PC installed on your Windows PC.

As the name says, this only works on a Windows PC

It would appear you are not using a PC running Microsoft Windows (we think you are using Unknown). Please take a look at ScriptX.Services for On-Premise Devices .

The UI :: the print button

The button is standard html styled with some Bootstrap css and Font Awesome:

        
            <button type="button"
                    class="btn btn-primary d-print-none"
                    id="btn_printPage">
                <i class="fas fa-cloud-download-alt"><i>  Download as PDF
            </button>
        
    

Wire up an event handler

We won't use any library to help us though obviously we could as they all resolve to being an abstraction on the underlying DOM:

        
            <script type="text/javascript" defer="true">
            document.getElementById("btn_printPage")
                .addEventListener("click",() => {
                    console.log("Print button clicked");
                });
            </script>
        
    

At this point we have a button which when clicked writes an information line to the console.

Reference the ScriptX.Services javascript modules

The client part of ScriptX.Services is written entirely in javascript and is delivered to client browsers in the usual way viz. a <script /> tag with src attribute referencing the location of the script.

The MeadCo ScriptX.Services print client modules are available for download from  Github . Packages are also available:  Yarn ,  Nuget .

Download and/or install a package and then add these javascript modules to the page:

        
            <script src="/Scripts/MeadCo.ScriptX/meadco-core-1.15.1.js"></script>
            <script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprint-1.15.1.js"></script>
            <script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprinthtml-1.15.1.js"></script>
            <script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintlicensing-1.15.1.js"></script>
        
    

The sample code above has listed out each individual file for illustration. Your environment workflow will enable packaging these files as desired, or you may use a CDN:

CDN: <script src="//cdn.jsdelivr.net/npm/scriptxprint-html@1.15.1/dist/meadco-scriptxservices.min.js"></script>

The whole of MeadCo.ScriptX.Print.HTML is now available to the page.

Configure ScriptX.Services

ScriptX.Services client must be configured with the MeadCo ScriptX Client license to use and the url of the ScriptX.Services for Windows PC server.

Use an asynchronous connection to the server with a callback function to be called when the server connection is completed. Asynchronous initialisation does not block the browser UI.

        
            'use strict';
            MeadCo.ScriptX.Print.Licensing.connectLite("http://127.0.0.1:41191", 
                "{3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497}",0,"warehouse");
            // apply is only needed if the license is not already applied, as we dont know if it is - always do it
            // as this also verifies and completes the connection to the server.
            MeadCo.ScriptX.Print.Licensing.applyAsync("{3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497}",
                0,"warehouse",
                () => {
                    console.log("Connected to ScriptX.Services for Windows PC");
                    ...            
                },
                (e) => { alert("Failed to connect to ScriptX.Services: " + e) });
        
    

Installing the license

The license used here ({3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497}) is the evaluation license you can use to work with developing your own content delivered from localhost on the same machine as ScriptX.Services for Windows PC.

The value "warehouse" means the license will be downloaded from MeadCo's license service. Alternatively, provide a valid url to the location of the license file (sxlic.mlf).

Define print parameters and print

we can now put everything together to print the page to the ScriptX.Services Server default printer.

        
            'use strict';
            MeadCo.ScriptX.Print.Licensing.connectLite("http://127.0.0.1:41191", 
                "{3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497}",0,"warehouse");
            // apply is only needed if the license is not already applied, as we dont know if it is - always do it
            // as this also verifies and completes the connection to the server.
            MeadCo.ScriptX.Print.Licensing.applyAsync("{3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497}",
                0,"warehouse",
                () => {
                    console.log("Connected to ScriptX.Services for Windows PC");
                    document.getElementById("btn_printPage");
                    .addEventListener("click", () => {
                        const settings = MeadCo.ScriptX.Print.HTML.settings;
                        settings.header = "ScriptX.Print :: Browser Agnostic Printing";
                        settings.footer = "&D&b&p of &P&b&t";
                        settings.page.orientation =
                        MeadCo.ScriptX.Print.HTML.PageOrientation.PORTRAIT;

                        settings.page.units = MeadCo.ScriptX.Print.MeasurementUnits.MM;
                        var margins = settings.page.margins;
                        margins.left = 12.5;
                        margins.top = 12.5;
                        margins.bottom = 12.5;
                        margins.right = 12.5;

                        // print is asynchronous
                        MeadCo.ScriptX.Print.HTML.printDocument(false);

                        // say we're printing
                        showBusyModal();

                        // promise-less (i.e. callback) wait for the document to be returned
                        // so we can then hide the we're printing dialog
                        MeadCo.ScriptX.Print.WaitForSpoolingComplete(-1,
                            (result) => {
                            hideBusyModal();
                            if ( typeof result === "string" )
                                alert("An error occurred while printing\n\n" + result);
                            });
                    });
                },
                (e) => { alert("Failed to connect to ScriptX.Services: " + e) });
        
    

Done!

Complete the form and then print. Your form values will be included on the print.

  Cloud printing!

The print buttons on this page use the MeadCo ScriptX.Services for Cloud to print and deliver the printed output as a PDF file. The code is (almost) exactly the same as would be used to ScriptX.Services for Windows PC (the use of a license parts are a little different).

See a sample working with installed ScriptX.Services for Windows PC.

MeadCoJS Library and Asynchronous functions

Many developers have made use of our MeadCoJS Script Library availabe on GitHub  MeadCoScriptXJS . Since v1.5 this library provides a complete encapsulation of using both ScriptX.Add-on and ScriptX.Services from the same code. The library (optionally) uses modern promise based coding and provides wrappers for implementing asynchronous functions that we provided in ScriptX.Add-on. For example, the ubiquitous factory.printing.WaitForSpoolingComplete() is implemented albeit with a different function prototype.

More samples and illustrations

This has been a quick introduction on how to develop with ScriptX.Services for Windows PC and deliver controlled and consistent print experiences to your users.

Continue in this section with how to work with ScriptX.Services for Windows PC: