Printer
Since Camel 2.1
Only producer is supported
The Printer component provides a way to direct payloads on a route to a printer. The payload has to be a formatted piece of payload in order for the component to appropriately print it. The goal is to be able to direct specific payloads as jobs to a line printer in a camel flow.
The functionality allows for the payload to be printed on a default printer, named local, remote or wireless linked printer using the javax printing API under the covers.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-printer</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
Since the URI scheme for a printer has not been standardized (the nearest thing to a standard being the IETF print standard), and therefore not uniformly applied by vendors, we have chosen "lpr" as the scheme.
lpr://localhost/default[?options] lpr://remotehost:port/path/to/printer[?options]
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Printer component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The Printer endpoint is configured using URI syntax:
lpr:hostname:port/printername
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required The host name of the printer. With localhost, the producer looks up a printer of this machine by printername alone, or uses the default printer of this machine when printername is default. With another host, it looks up the printer by hostname/printername. | String | ||
The port of the printer. The producer does not use it, as it looks up the printer by name. It is only part of the service location of the endpoint. | int | ||
The name of the printer, for example default or sales/salesprinter, as the print services of this machine (javax.print.PrintServiceLookup) know it, so a remote printer must be installed on this machine. A print service matches when its name ends with the looked-up name or the other way round, ignoring case and treating / and \ alike. The producer fails to start when no print service matches. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The number of copies to print, sent as the Copies attribute of the print request. The producer fails to start when the value is less than 1. | 1 | int | |
The doc flavor as a javax.print.DocFlavor, for example a reference to a bean. When set, it replaces the doc flavor that the flavor and mimeType options select. | DocFlavor | ||
The representation class of the doc flavor, as the name of a nested class of javax.print.DocFlavor: DocFlavor.BYTE_ARRAY, DocFlavor.INPUT_STREAM or DocFlavor.URL, and for the mimeType TEXT_HTML or TEXT_PLAIN DocFlavor.CHAR_ARRAY, DocFlavor.READER or DocFlavor.STRING. If not set, DocFlavor.BYTE_ARRAY is used. Together with mimeType it selects the doc flavor, and an unknown value or a combination that names no doc flavor fails the creation of the endpoint. | DocFlavor.BYTE_ARRAY | String | |
The paper size, as the name that javax.print.attribute.standard.MediaSizeName gives it, for example na-letter, na-legal, iso-a4 or japanese-postcard. Case is ignored and _ may be used for -, so ISO_A4 works too. An unknown size fails the creation of the endpoint. | na-letter | String | |
The name of the input tray to take the paper from, as javax.print.attribute.standard.MediaTray gives it with spaces replaced by _, for example top, middle, bottom or manual. The name must match, with the same case, a tray that the printer reports, or the producer fails to start. | String | ||
The format of the document, as the name of a doc flavor constant of javax.print.DocFlavor, for example AUTOSENSE, PDF, POSTSCRIPT, PCL, GIF, JPEG, TEXT_PLAIN_UTF_8 or TEXT_HTML. If not set, AUTOSENSE is used, which lets the print service detect the format. With sendToPrinter set to false, it also selects the extension of the written file. | AUTOSENSE | String | |
The orientation of the printed pages, as a value of javax.print.attribute.standard.OrientationRequested. Enum values:
| portrait | String | |
A text that the producer puts before hostname/printername to form the name of the printer to look up. It is not used when the host is localhost. | String | ||
Whether to send the print job to the printer. If false, the producer writes the document to a file named PrintOutput_ followed by a random UUID in the target directory of the working directory, with the extension gif, jpeg, pdf or txt that matches mimeType. The producer still looks up the printer when it starts, and the printer must support the doc flavor. | true | boolean | |
Whether to print on one side or on both sides of the paper, as a value of javax.print.attribute.standard.Sides. The value duplex is the same as two-sided-long-edge, and tumble the same as two-sided-short-edge. Enum values:
| one-sided | String | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean |
Message Headers
The Printer component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The name of the print job, sent to the print service as the JobName attribute. If not set, the job is named Camel: lpr. | String |
Message Body
The message body at each side of the Printer component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The document to print, read as a stream: text, bytes, a stream or a file, in the format of the mimeType and flavor options. A Map, a POJO, a number or no body fails with an InvalidPayloadException. |
Producer output |
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
Sending Messages to a Printer
Printer Producer
Sending data to the printer is very straightforward and involves creating a producer endpoint that can be sent message exchanges on in route.
The document is printed in the doc flavor of the flavor and mimeType options. When the print service does not support that doc flavor, the exchange fails with a javax.print.PrintException that names the doc flavor and the print service, and no print job is created. This also applies with sendToPrinter=false.
Examples
Usage samples.
Printing text-based payloads
-
Java
-
XML
-
YAML
from("file://inputdir/?delete=true")
.to("lpr://localhost/default?copies=2&flavor=DocFlavor.INPUT_STREAM&mimeType=AUTOSENSE&mediaSize=NA_LETTER&sides=one-sided"); <route>
<from uri="file://inputdir/?delete=true"/>
<to uri="lpr://localhost/default?copies=2&flavor=DocFlavor.INPUT_STREAM&mimeType=AUTOSENSE&mediaSize=NA_LETTER&sides=one-sided"/>
</route> - route:
from:
uri: file://inputdir/
parameters:
delete: true
steps:
- to:
uri: lpr://localhost/default
parameters:
copies: 2
flavor: DocFlavor.INPUT_STREAM
mimeType: AUTOSENSE
mediaSize: NA_LETTER
sides: one-sided Printing GIF-based payloads
-
Java
-
XML
-
YAML
from("file://inputdir/?delete=true")
.to("lpr://remotehost/sales/salesprinter?copies=2&sides=one-sided&mimeType=GIF&mediaSize=ISO_A4&flavor=DocFlavor.INPUT_STREAM"); <route>
<from uri="file://inputdir/?delete=true"/>
<to uri="lpr://remotehost/sales/salesprinter?copies=2&sides=one-sided&mimeType=GIF&mediaSize=ISO_A4&flavor=DocFlavor.INPUT_STREAM"/>
</route> - route:
from:
uri: file://inputdir/
parameters:
delete: true
steps:
- to:
uri: lpr://remotehost/sales/salesprinter
parameters:
copies: 2
sides: one-sided
mimeType: GIF
mediaSize: ISO_A4
flavor: DocFlavor.INPUT_STREAM Printing JPEG-based payloads
-
Java
-
XML
-
YAML
from("file://inputdir/?delete=true")
.to("lpr://remotehost/sales/salesprinter?copies=2&sides=one-sided&mimeType=JPEG&mediaSize=JAPANESE_POSTCARD&flavor=DocFlavor.INPUT_STREAM"); <route>
<from uri="file://inputdir/?delete=true"/>
<to uri="lpr://remotehost/sales/salesprinter?copies=2&sides=one-sided&mimeType=JPEG&mediaSize=JAPANESE_POSTCARD&flavor=DocFlavor.INPUT_STREAM"/>
</route> - route:
from:
uri: file://inputdir/
parameters:
delete: true
steps:
- to:
uri: lpr://remotehost/sales/salesprinter
parameters:
copies: 2
sides: one-sided
mimeType: JPEG
mediaSize: JAPANESE_POSTCARD
flavor: DocFlavor.INPUT_STREAM