Development Guide

To Bookmark this document, click

BOOK MARK 4

Creating a Printer Application

Published 2014-05-21 | Tutorial. (Compatible with SDK 3.5,4.5,5.0,5.1 and 2012,2013,2014 models)

This tutorial describes the use the printer class of the External Interworking API. This class is needed to develop various printer applications.

This tutorial demonstrates development of an application that uses printer. The application can check whether current TV supports printing or not and prints file or screen.

Programming of applications using printer is mostly based on the External Interworking API of the Hardware API.

Prerequisites

To develop TV applications, you need:

  • SDK (Samsung Smart TV SDK is recommended) or a text editor for creating HTML, JavaScript and CSS files
  • Samsung Smart TV 2013 Emulator (available form SDK version 4.0 b)

Note

The list of compatible printers is available here.

Development Environment

Use Samsung Smart TV SDK to develop the application. You can find general instructions for creating applications in Implementing Your Application Code.

Source Files

Note

The files needed for the sample application are here.

Source files in directory are explained in the table:

DirectoryDescription
cssContains the stylesheet file printer-tutorial.css
jsContains the javascript file printer-tutorial.js which does the following: - initializes module - handles keys - activates printing
imagesContains the background image file back.png and also contains the print test image file testpage.png.

Printer Application

Using the API

First of all we need index.html file where we will include all needed objects from different APIs for starting the application (Widget Object), handling keys (TVKeyValue Object) and using the printer (External Interworking API). For this we have the following code in <head> section of index.html file:

<script type="text/javascript" src="$MANAGER_WIDGET/Common/API/Widget.js"></script>
<script type="text/javascript" src="$MANAGER_WIDGET/Common/API/TVKeyValue.js"></script>
<script type="text/javascript" src="$MANAGER_WIDGET/Common/webapi/1.0/webapis.js"></script>

In the js/printer-tutorial.js we have the code of our application. Lets have a closer look at it.

To use the printer we have to create the printer object.

var printer = window.webapis.printer || {};

Checking if Printing is Supported

Because printing is supported only on some kinds of TVs: 7000, 8000 and 9000 series for ATSC and 9000 series for DVB-T, we have to check whether the printing is supported or not. For this we use the window.webapis.printer.isPrintingServiceSupported() function. It is good to check it only once, just after the app initialization.

if (printer.isPrintingServiceSupported()) {
    document.getElementById('message').innerHTML = 'There IS a printer module available.<br>'
        + 'Press <span style="color: red; font-weight: bold;">RED</span> key to print an image from a file<br>'
        + 'or <span style="color: blue; font-weight: bold;">BLUE</span> key to print the whole TV screen.';
    canPrint = 1;
} else {
    document.getElementById('message').innerHTML = 'There is NO printer module available.';
    canPrint = 0;
}

Printing

Now we can use methods of printer object to print the image from a file or a TV screen (whole screen or a desired part).

Printing an Image from a File

To print an image from a file we use runFilePrinting() method:

printer.runFilePrinting('file://images/testpage.png');
Printing an Image from an URL

We can also use an URL of the image to be printed (not supported on SDK 4.0b yet):

printer.runFilePrinting('http://www.example.com/testImage.jpeg');

Note

Only sequential encoded image files (sequential jpg, bmp, gif, png) can be printed.

Printing the current TV screen

To print the TV screen we use runScreenPrinting() method:

var screenAreaToPrint = new SRect(0, 0, curWidget.width, curWidget.height);
printer.runScreenPrinting(printer.SCREEN_LAYER_ALL, screenAreaToPrint);

To learn more about printing please refer to Printer.

Connecting or Disconnecting Printer

There is no printer connection/disconnection API to use from JavaScript, but there is printer settings application on TV menu so user can search and connect/disconnect their printer through TV menu.

On the real TV you can see printing application window similar to the image below, so you can select printing options and print.

On the other hand on a Samsung Smart TV Emulator, when user runs the app, a printer connection dialog will appear allowing the user to search for a printer in a LAN or to add a printer manualy by providing its IP and port number.

To Bookmark this document, click

BOOK MARK 4