UltraLightClient Code Community
[ start | index | login ]
Overview > Printing

Printing

Belongs to following categories: Utility, ULC5.2, ULC6.1, ULC6.2, UltraLightClient '08,

Project Home
Downloads

Purpose

The intended way how to print from within an ULC application running in a servlet container is to generate a PDF document on the server-side and make this document availabe for download by the client. The PDF document can then be printed like any other PDF document, only depending on the PDF viewer and installed printers.

In more detail, the PDF document is first generated on the server-side using session-specific data. The document is then made accessible to the client via a web server. Finally, the ULC client launches the web browser and queries the browser to fetch the PDF document from the respective web server address.

The described printing functionality can also be used to provide the client with any other kind of dynamically generated resource, e.g. an Excel document containing session-specific numbers.

The Printing utility works transparently in development mode and no web server is needed.

How to use

Implement a Web Resource Provider

We first have to decide whether we can generate in-memory the web resources to be sent to the client. If so, we inherit from MemoryResourceProvider and implement the abstract method createBytes(). Using the PDF library iText, we can generate in-memory PDF documents like shown in the simple example below.

public class PdfMemoryProvider extends MemoryResourceProvider

{ ...

protected byte[] createBytes() throws IOException { try

{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, outputStream);

document.open(); document.add(new Paragraph("Hello ULC!")); document.close();

writer.flush(); writer.close(); outputStream.close();

return outputStream.toByteArray(); } catch(DocumentException de) { throw new IOException("Could no create PDF document: " + de.getLocalizedMessage()); }

} }

In case we cannot generate the web resource in-memory but only write it to a file first, we inherit from FileResourceProvider and implement the abstract method getFile(). Using the PDF library iText, we can generate PDF files like shown in the simple example below.

public class PdfFileProvider extends FileResourceProvider
{

...

protected File createFile() throws IOException { try { File file = File.createTempFile(getClass().getName(), ".pdf"); file.deleteOnExit();

OutputStream outputStream = new FileOutputStream(file); Document document = new Document(PageSize.A4); PdfWriter writer = PdfWriter.getInstance(document, outputStream);

document.open(); document.add(new Paragraph("Hello ULC!")); document.close();

writer.flush(); writer.close(); outputStream.close();

return file; } catch(DocumentException de) { throw new IOException("Could no create PDF document: " + de.getLocalizedMessage()); }

} }

Register a Web Resource Provider with the Download Manager

Whenever the user wants to print some data, we register an instance of our web resource provider with the DownloadManager using the put() method and we will receive a unique id. We can then call the showDocument() method and pass it the id. This will cause the ULC client to launch the browser and download the web resource returned by the corresponding registered web resource provider.

IResourceProvider pdfProvider = new PdfMemoryProvider(...);

String id = DownloadManager.INSTANCE.put(pdfProvider);

try { DownloadManager.INSTANCE.showDocument(id, "template"); } catch(IOException ioe) { System.err.println("Cannot show document: " + ioe);

}

Add the Web Resource Download Servlet to the Servlet Context

Now, all that remains to be done is to add the ResourceDownloadServlet to the same servlet context as the ULC application that triggered the printing. Add the configuration below to your web.xml file.

<servlet>
  <servlet-name>ResourceDownloadServlet</servlet-name>

<servlet-class>com.canoo.ulc.community.ulcprinting.application.ResourceDownloadServlet</servlet-class> </servlet>

<servlet-mapping> <servlet-name>ResourceDownloadServlet</servlet-name>

<url-pattern>/download</url-pattern> </servlet-mapping>

Compatibility

  • ULC 5.2: JDK 1.4.2
  • ULC 6.1: JDK 1.4.2
  • ULC 6.2: JDK 1.4.2, JDK 1.5, JDK 1.6, Windows XP, Windows Vista, Linux, Mac OSX
  • UltraLightClient 2008: JDK 1.4.2, JDK 1.5, JDK 1.6, Windows XP, Windows Vista, Mac OSX

Project setup with Eclipse

The project contains all necessary Eclipse project files and can be directly imported from the repository. The classpath in this project contains a entry named ULC Library, which locates the needed UltraLightClient libraries. For to uses the ULC Library entry you have to install the Eclipse Java IDE Integration Plug-in that can be found at : >>http://update.canoo.com/eclipse. A installation guide how to install plug-ins in Eclipse can be found at the Eclipse help pages: Workbench User Guide > Tasks > Updating and installing software.

Project Links

Project Home
Downloads

Author

etienne

no comments | post comment
labels
Category:ULC6.2
Category:Utility
Category:ULC6.1
Category:ULC5.2
Category:UltraLightClient '08
attachments
util-printing-src.zip (6684)
Created by etienne. Last edited by daniel, 14 days ago. Viewed 10,060 times. #52
[diff] [history] [edit] [rdf]
Name
Email:
snipsnap.org | Copyright 2000-2002 Matthias L. Jugel and Stephan J. Schmidt