Introduction to ScriptX.Services
ScriptX.Services is for use within web applications within any browser. The use of ScriptX.Services provides scripted control of a printing experience within the browser including allowing a choice of the output printer, orientation, page header and footers etc and the initiation of printing.
ScriptX.Services provides developers with the tools for ensuring the consistent formatting and appearance of printed output regardless of the users device or modern browser.
On the client (browser), ScriptX.Services is a javascript module that communicates with a server using an Open API.
If you are a current licensee of ScriptX.Addon then, for example, ScriptX.Services enables maintaining your investment in your current code:
<!-- MeadCo ScriptX.Services -->
<script
data-meadco-license="{3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497}"
data-meadco-server="http://127.0.0.1:41191"
data-meadco-license-path="warehouse"
data-meadco-license-revision="0"
src="/scripts/meadco-scriptx.js"></script>
<!-- Original code targetting ScriptX.Addon -->
<script type="text/javascript">
function printPage() {
factory.printing.header = "";
factory.printing.footer = "";
factory.printing.paperSize = "A4";
factory.printing.Print(false); // don't prompt
}
</script>
or, if you are a developer of new applications using common frameworks such as React, Angular, Vue then you might not want to use external modules/libraries but use the Open API to print content from the current document:
<script type="text/javascript">
async function printHtml(strHtml, printerName, pageSettings) {
try {
let response = await fetch('http://127.0.0.1:41191/api/v1/printHtml/print'), {
method: 'POST',
cache: 'no-cache',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(evaluationLicenseGUID + ':')
},
body: JSON.stringify({
contentType: 2, // "InnerHtml" - specifies an HTML fragment is to be printed
content: strHtml, // HTML fragment from page
device: {
printerName: printerName
},
settings: pageSettings
})
});
response = await handleResponse(response);
console.log("print started, status: " + response.status);
console.log(response);
}
catch (error) {
console.error("Error while printing: " + error);
}
}
(async () => {
try {
await applyPrintingLicense(evaluationLicenseGUID);
const printConfig = await printingDefaults();
document.getElementById("btn-print").addEventListener("click", async () => {
await printHtml(document.getElementById("printContent").innerHTML,
printConfig.device.printerName,
{
header: '',
footer: '',
page: {
units: 2, // mm
margins: { top: 12.5, left: 12.5, bottom: 12.5, right: 12.5 }
}
});
});
} catch (error) {
console.error("An error occured:" + error);
}
})();
</script>
This example is simple but the margins, the page header (including an image and html content) and footer and the paper orientation have been described in JavaScript. Standard CSS is used to remove some content for the print and layout the content appropriately. Notice that the date and time in the footer is the time right now - the PDF was produced right now, it isn't a canned file.
This is the output from our cloud service `printing` using a print to PDF print driver.
The result is output with consistent formatting and appearance regardless of the browser and its settings that you are using.
The same idea works with ScriptX.Services hosted not in the cloud but on your own network or on each individual Windows PC and you can print to paper/labels from any browser to any printer available on the network / PC.
ScriptX.Services product variants
Variants provide the optimum solution for your envioronment:
For Windows PC
The closest equivalent to the ScriptX Add‑on for Internet Explorer, the Microsoft Windows (x64) PC service provides for ‘printing’ to any print device available to the client Microsoft Windows (x64) PC.
For On Premise Devices
The on-premise ScriptX.Services is completely under your own control hosted on your own Microsoft Windows (x64) Server and provides for ‘printing’ to any print device available to the server.
Cloud Service
Our FREE cloud service provides for ‘printing’ to a PDF or XPS format file which is downloaded by the user. They may open, save or print the file - think of this as an easy way to provide a “download this page as a PDF/XPS document” facility on your website.
Technical background
ScriptX.Addon
ScriptX.Add-on for Internet Explorer uses a deep integration with the Internet Explorer UI and custom templates for the Trident Print engine within Internet Explorer. This enables the Internet Explorer engine to be used to paginate the document and render to the print output device.
The add-on needs to be installed on each client PC - a process that in general requires Administrator privileges and for advanced usage requires a license is accepted and installed by each user of Internet Explorer.
To control the print process the developer writes script code to set required properties on and call methods from the add-on to perform printing and other tasks. Typically the add-on was instantiated with the id 'factory' so code such as this is typical:
<!-- 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 type="text/javascript">
function printPage() {
factory.printing.header = "";
factory.printing.footer = "";
factory.printing.paperSize = "A4";
factory.printing.Print(false); // don't prompt
}
</script>
As well as working within Internet Explorer we developed ScriptX so that it could be used as an HTML print engine for applications and services such as Internet Information Services (IIS).
In other words, the print technology of ScriptX is tried, tested and used over many years to prove its reliability and robustness.
ScriptX.Services Evolution
To support modern browsers on any device with ScriptX.Services we take that technology and deploy it behind a rich Web API on a local web server that provides the interface between the ScriptX print engine on the PC and the client devices.
The interface accepts a stream of html to print and the settings to use and then passes those onto the print engine which then prints the html with the provided settings such as headers, footers, margins, printer to use, papersize to use and so on.
So, instead of the printing being performed within and by the user's browser, the content to be printed is sent from user's browser to ScriptX.Services where it is paginated and rendered to a print device connected to the ScriptX.Services host - the local PC, team server or cloud.
Browser support
Supporting customers who have an investment in javascript that works with ScriptX.Add-on for Internet Explorer is very important to us. It will be enough of a challenge to convert HTML content originally authored for IE 7/8 to modern HTML that works in all browsers.
To support customers with current code, we provide a library that implements an emulation of ScriptX.Add-on in javascript ScriptX.Services Client Library .
The library will work with both '.factory'
based code and with MeadCoScriptXJS Library based code.
Our ScriptX.Services client libraries support Edge, Chrome, Firefox and Safari (and any modern evergreen browser that supports HTML 5 and ECMAScript 5 or later).
ScriptX.Add-on for Internet Explorer continues to be supported for Internet Explorer.
Print functions
ScriptX.Add-on provides three three ways of printing HTML content from the browser. Each of these functions prints a 'snapshot' of the content as currently displayed in the browser. ScriptX.Services supports each print function.
Print the page/document [aka factory.Print()]
To print the page/document means delivering the HTML stream to print from the client device to the server. A javascript module implements capturing the current browser DOM content and then delivers that to the server along with required settings.
A further javascript module implements the behaviour of the ScriptX.Add-on window.factory
object, but in javascript. Very familiar looking code can then be written.
-
Replace the
<object id="factory" ... />
element from the ScriptX.Addon with including a javascript module that defines awindow.factory
object that fully emulates ScriptX.Add-on.<script src="/scripts/meadco-core-1.15.1.js"></script> <script src="/scripts/meadco-scriptxprint-1.15.1.js"></script> <script src="/scripts/meadco-scriptxprinthtml-1.15.1).js"></script> <script src="/scripts/meadco-scriptxfactory-1.15.1.js"></script> <script src="/scripts/meadco-scriptxprintlicensing-1.15.1.js"></script> <script src="/scripts/meadco-secmgr-1.15.1.js" data-meadco-license="3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497" data-meadco-license-revision="0" data-meadco-license-path="warehouse" data-meadco-server="http://127.0.0.1:41191"> </script>
-
The javascript doesnt change:
<script type="text/javascript"> function printPage() { factory.printing.header = ""; factory.printing.footer = ""; factory.printing.paperSize = "A4"; factory.printing.Print(false); // don't prompt } </script>
To be clear; when the 'Print(false)' function executes, the html as displayed on screen is sent to the local web server along with references to required external resources such as style sheets and images (script references are not sent). The html document is then paginated and rendered.
The settings such as header and footer are not sent with each setting of a property. The values are stored in variables on the client and then sent to the server with the stream of html to print.
Print a remote document [aka factory.PrintHtml()]
To print a remote document is simple - send the url to be downloaded and printed to the local web server along with the required settings. In otherwords, instead of the stream to print, just the url. The server then requests the HTML content from the url then it is paginated and rendered.
Print document fragments [aka factory.SetPageRange(); factory.Print()]
The SetPageRange() function sets a flag such that only the content that is selected (either programmatically or by the user) is printed. The requirement for a selection means this is not hugely useful, but the ability to print a fragment of or part of the current html document or even a dynamically generated document fragment without the need to copy it into the DOM is useful.
With ScriptX.Services we can easily do this by taking a snap shot of part of the DOM rather then the whole page DOM when printing the whole document. The html is sent along with references to required external resources such as style sheets and images (script references are not sent). The html is then assembled as a complete document, paginated and rendered.
Write once
With our ScriptX.Services libraries you can write the code that works seemlessly with both the ScriptX.Add-on and ScriptX.Services. Whichever browser, which ever device, 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="secmgr" style="display:none"
classid="clsid:5445be81-b796-11d2-b931-002018654e2e"
codebase="installers/smsx.cab#Version=8,0,0,56">
<param name="GUID" value="3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497" />
<param name="Path" value="sxlic.mlf" />
<param name="Revision" value="0" />
</object>
<object id="factory" style="display:none"
classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"">
</object>
<script src="/scripts/meadco-core-1.15.1.js"></script>
<script src="/scripts/meadco-scriptxprint-1.15.1.js"></script>
<script src="/scripts/meadco-scriptxprinthtml-1.15.1.js"></script>
<script src="/scripts/meadco-scriptxfactory-1.15.1.js"></script>
<script src="/scripts/meadco-scriptxprintlicensing-1.15.1.js"></script>
<script src="/scripts/meadco-secmgr-1.15.1.js"
data-meadco-license="3cfd70e2-f38f-4ab2-95f4-4ce4c1e39497"
data-meadco-license-revision="0"
data-meadco-license-path="warehouse"
data-meadco-server="http://127.0.0.1:41191">
</script>
<script type="text/javascript">
function printPage() {
factory.printing.header = "";
factory.printing.footer = "";
factory.printing.paperSize = "A4";
factory.printing.Print(false); // don't prompt
}
</script>
Modern code
A chance to re-write is always welcome and the very flat properties/methods list of the add-on isn't the design approach that would be taken now.
Our service exposes a rich API for describing the parameters to use on the print and delivering the HTML content to print.
If you are starting new projects now, then you might want to use your preferred library - jQuery, Angular, React, Vue, Axios etc etc to provide the library for calling a REST API and then extract relevant portions from our libraries on ScriptX.Services Client Library for extracting the HTML to send to the rich API.
The choice is yours.
If you have curerent code using ScriptX.Addon - you can continue to use that code if you want to.
Or, you might want to develop optimal code for your solution.