There have been plenty of requests for the ability to print from Corona SDK, but given that Corona SDK is all done with graphics in OpenGL, exactly what would you want to print? Well, one example would be to print a screenshot of your app. Another usage case would be printing downloaded PDFs.

Corona SDK doesn’t have built-in printing facilities, so how can you accomplish this? With Corona Enterprise of course! As a bonus, we’ll also demonstrate how to save an image from the system.DocumentsDirectory to the camera roll.

NOTE: This tutorial is for iOS and assumes that you have a wireless printer on the same WiFi network that supports the Apple AirPrint protocol.

Getting up to Speed

Before you proceed, you should read and understand the Corona Enterprise Quickstart (iOS) tutorial because many aspects of this tutorial build upon the lessons learned therein. Please follow all of the steps from that tutorial to set up a new project and name it whatever you prefer. This tutorial will build a plugin called image which you can use within your Corona SDK project.

Xcode Project Modifications

To begin, let’s go through the Xcode modifications which will allow this plugin to function. First, edit the AppCoronaDelegate.h file and change this line…

…to this:

This will allow your app to talk to the iOS Print Interaction Controller.

Now, let’s modify the file. In the PluginLibrary class where you had set up definitions for the on and off functions in the previous tutorial, rename one definition to saveImage and the other to printImage. Essentially, your class should look like this:

Next, a few lines down from this, change the PluginLibrary::kName value to "plugin.image". This will let you reference the plugin with that name in your Lua code. Also change the PluginLibrary::kEvent[] value to "image":

Several lines below this, in the PluginLibrary::Open method, you’ll need to update the kVTable array to represent the mapping of the Lua function to the Objective-C method:

Now, let’s write the two functions to handle printing and saving of the image. Because of the setting in the table above, "airPrint" will be the function name on the Lua side, while printImage defines the Xcode method. Since this tutorial is primarily about printing, let’s look at the printImage function first:

This creates a new NSString object called path that is initialized with the first item passed to the Lua parameter stack. It’s expected to be a string and an error will be thrown if not. On the next line, we create an NSData object called myData initialized with the contents of the file path. The remainder of the code sets up the print interaction controller. If your app can print, we set up various parameters needed for that process. If you wish to explore the source and logic behind this code, please see here.

Now, let’s write the saveImage function. This will be named "saveToAlbum" on the Lua side, while saveImage will be the Xcode method.

Like the printImage function, this gets the image path passed in as the first parameter. It then generates a UIImage object based on the contents of the file at path and writes it to the camera roll.

Lua Side

Now we’ll tackle the Lua/Corona side! In your main.lua file, paste this code:

Effectively, this code includes your custom plugin and creates a widget button with the label Print Photo. When the button is tapped, it calls the handleButtonEvent() function which does the magic. First, it generates a path to the image you want to print (this could also be a PDF). In this case, it picks up a file named myimage.jpg from the app’s bundle (system.ResourceDirectory). Alternatively, you could use the API to capture the screen to the system.DocumentsDirectory and pick the image from there.

With the path to that image, we call the plugin function called airPrint() (the printImage method in Objective-C).

If you wish to test the saving function, the Lua code should be almost identical, except that you’d call image.saveToAlbum( path ) instead of image.airPrint( path ).

If everything went well, you should now be able to build for a device and install it as described in the quickstart tutorial.


While Corona Enterprise may seem intimidating at first, utilizing it can empower your apps with great new features. Hopefully this tutorial has shown you the basic framework and will encourage you to explore new potential ideas in development.

Continued here:

Tutorial: Image Printing via Corona Enterprise (iOS)