Coding with ScriptX.Services
This page provides an introduction to adding the use of ScriptX.Services for Cloud to your own pages to provide a "download as PDF" feature on your web pages:
- Add code to configure the library,
- Add code to describe required settings such as margins, headers and footers,
- Add a print button to your page,
- Connect the print button click event to the ScriptX.Services code using your favourite technique,
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 ScriptX.Services to `print` this document to a PDF and then provide the `printed file` as a download.
To show that we arent cheating and using a pre-prepared file for download the page includes some form fields so you can customise the print. Also note that the date and time on the PDF will be the time you requested the 'print'.
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:
At this point we have a button which when clicked writes an information line to the console.
<script src="/js/jquery-3.1.1.js"></script> <script src="/js/meadco-core-1.7.1.js"></script> <script src="/js/meadco-scriptxprint-1.7.1.js"></script> <script src="/js/meadco-scriptxprinthtml-1.7.1.js"></script>
The whole of MeadCo.ScriptX.Print.HTML is now available to the page.
The sample code above has listed out each individual file for illustration. Use your environment workflow to package these files as desired.
For version 1, ScriptX.Services client libraries depend on jQuery for ajax and some DOM manipulation services so jQuery must be referenced.
Configure ScriptX.Services client
ScriptX.Services client must be configured with two parameters:
- The url of the ScriptX.Services server.
- A subscription identifier.
A subscription identifier is always required as the ScriptX.Services server is at different url to the server from which the document requesting the print originated and will therefore be subject to cross origin restrictions on scripting. The server will allow calls from origins with a valid identifier.
Define print parameters and print
Finally, we can put everything together to print the page to a PDF document. The ScriptX.Services client modules will take care of the response from the server will be a file and will download the file when it is ready.
Complete the form and then print. Your form values will be included on the print though the value you enter for password will not print obviously.
More samples and illustrations
This has been a quick introduction on how to develop with ScriptX.Services for Cloud and quickly and eaily deliver PDF downloads of your web pages in the way you want; headers, footers, page orientation etc.
See more in the Showcase.
We use this ScriptX.Services for Cloud service to deliver our showcase of how to use basic and advanced features - take a look at our Samples showcase to see more of what is possible and what makes ScriptX.Services for Cloud the most powerful tool on the Web for delivering highly controlled and consistent HTML to PDF to your users.
With ScriptX.Services for On-Premise Devices you get exactly the same features, write almost exactly similar code but:
- Data never leaves your network
- You can direct output to physical printers on your premises - so for example, users with mobile devices can produce consistent print results formatted in the way you want on a printer available centrally.
If hosting the service on your own servers printing to physical printers not files is what you need then look at our on-premise services.
Features and futures
We do not intend to stand-still, read on for our roadmap.