How to print PDF documents with ScriptX.Services for Windows PC

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

v2.8.0 or later of ScriptX.Services for Windows PC is required.

If using the MeadCo ScriptX.Services print client modules then v1.5.3 or later of these are required.  Github .

Much here is the same as printing the current HTML document.

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.

To deliver a print experience to your users, any page in your app that provides printing of a PDF document 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 paper size and orientation,
  4. Add a print button or other appropriate UI to your page,
  5. Connect the print button click event, or appropriate UI event to the ScriptX.Services code using your favourite technique,
  6. Done!

Steps 3,4,5 and 6 are the same as working with ScriptX.Add-on for Internet Explorer. Steps 1 & 2 are functionally the same and in coding terms are as easy as including the MeadCo Security Manager and MeadCo ScriptX factory objects on the page.

A worked example

We will add a button to this page that when touched will 'print' a PDF file stored on this server to another PDF and then provide the `printed file` as a download - we are using printing but with this being a cloud system the only printers are those to file.

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 and printing a PDF is 'real'!.

When this page starts, it connects to the ScriptX.Services for Cloud, the code fragments below show how to use your local install of ScriptX.Services for 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 .

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/jquery-3.1.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-core-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprint-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintpdf-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintlicensing-1.11.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.11.1/dist/meadco-scriptxservices.min.js"></script>

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

ScriptX.Services Client library dependencies

 For version 1, ScriptX.Services client depends on jQuery for ajax and some DOM manipulation services so jQuery must be referenced.

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.

or, as synchronous behaviour is deprecated in all browsers, 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.

Installing the license

The API calls must occur in the order illustrated as the license must be available before the PDF.connect() call can succeed.

If you re-use a ScriptX.Add-on license and the license has not already been accepted then a blocking dialogue will be created and the user asked to approve that ScriptX.Services for Windows PC can used by content delivered from the domains listed in the license. The dialogue may not be obvious and for this reason we recommend an upgrade to the license to enable silent use which by-passes the need for a dialog. All licenses purchased for use with ScriptX.Services for Windows PC have silent use enabled.

The license used here ({e29212c9-3c6f-41ac-8078-4596738fdb59}) 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

We can now put together everything required to print a PDF document to the ScriptX.Services Server default printer. Note that this sample is requesting pages 3 and 4 from a longer document.

The UI :: the print button

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

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",function(event) {
            console.log("Print button clicked");
        });
</script>

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

Done!

We can now put everything together to print the PDF document to the ScriptX.Services Server default printer.

<script type="text/javascript" defer="true">
    document.getElementById("btn_printPage")
        .addEventListener("click",function(event) {
            printPDF();
        });
</script>

  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 used here (view source) 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).

Migrating current 'Add-on' code to ScriptX.Services for Windows PC

Moving older systems to modern browsers can be a daunting task. Moving code that provided controlled printing with ScriptX.Add-on to ScriptX.Services shouldn't be daunting.

Most code that uses ScriptX.Add-on assumes the presense of an object 'window.factory'. To make sure that moving code that relied upon ScriptX.Add-on isnt daunting, we have developed an emulation of the majority of the functionality of the ScriptX.Add-on object in javascript.

To support old code, the following libraries are required:

<script src="/Scripts/jquery-3.1.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-core-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprint-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintpdf-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxfactory-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintlicensing-1.11.1.js"></script>
<script
    data-meadco-license="{e29212c9-3c6f-41ac-8078-4596738fdb59}"
    data-meadco-server="http://127.0.0.1:41191" 
    src="/Scripts/MeadCo.ScriptX/meadco-secmgr-1.11.1.js"></script>

Note that the server and license identifier are included as attributes on the <script /> tag and so there is no reason to call MeadCo.ScriptX.Print.Licensing.apply() or MeadCo.ScriptX.Print.HTML.connect(). With this in place the <object /> tags required for ScriptX.Add-on (including MeadCo License Manager) can be replaced with the <script /> tags and a highly significant amount of code will `just work`.

Supporting old and new browsers

 Write once

If you can get your HTML to work with both old (before Internet Explorer 11) and modern browsers then you can use your current ScriptX code unchanged.

With our ScriptX.Services libraries you can write the code that works seemlessly with both the ScriptX.Add-on and ScriptX.Services.

Include the ScriptX.Services client libraries on the page and they will quietly do nothing when ScriptX.Add-on is available. The result is users with old browsers will be able to print to a printer connected to their workstation, users of modern browsers will be able to print to printers available to the ScriptX.Services server and which ever version of ScriptX, your users will get the same experience with consistent output.

To write once, leave the ScriptX.Add-on object on the page and add the script files. In those browseers that support ScriptX.Add-on it will be used, in those browsers that don't the emulation will be used:

<!-- MeadCo ScriptX -->
<object id="factory" style="display:none"
  classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
  codebase="installers/smsx.cab#Version=8,0,0,56">
</object>
<script src="/Scripts/jquery-3.1.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-core-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprint-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintpdf-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxfactory-1.11.1.js"></script>
<script src="/Scripts/MeadCo.ScriptX/meadco-scriptxprintlicensing-1.11.1.js"></script>
<script
    data-meadco-license="{e29212c9-3c6f-41ac-8078-4596738fdb59}"
    data-meadco-server="http://127.0.0.1:41191" 
    src="/Scripts/MeadCo.ScriptX/meadco-secmgr-1.11.1.js"></script>

<script type="text/javascript">
function printPDF() {
    factory.printing.orientation = "portrait";
    factory.printing.paperSize = "A4";
    factory.printing.BatchPrintPDF(factory.baseUrl("/content/doc.pdf"));
}
</script>

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 (a promise library is required for Internet Explorer 11) 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.

However, the library enables writing a single code base for printing which will work when either the Add-on or Services is used within the client browser. The library is used throughout our samples to illustrate this and allows us to write a sample just once.

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:

Features and futures

We do not intend to stand-still, read on for our roadmap.