Camel Components

PDF

Since Camel 2.16

Only producer is supported

The PDF component provides the ability to create, modify or extract content from PDF documents. This component uses Apache PDFBox as the underlying library to work with PDF documents.

To use the PDF component, Maven users will need to add the following dependency to their pom.xml:

pom.xml

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-pdf</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

URI format

The PDF component only supports producer endpoints.

pdf:operation[?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, *.yaml files, 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 PDF component supports the following options which are listed below.

Name Description Default Type

lazyStartProducer (producer)

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

autowiredEnabled (advanced)

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 PDF endpoint is configured using URI syntax:

pdf:operation

With the following path and query parameters:

Path Parameters

Name Description Default Type

operation (producer)

Required The operation to perform: create writes the text of the message body to a new PDF document; append writes the text of the message body on new pages at the end of the PDF document of the CamelPdfDocument header; extractText extracts the text of the PDF document in the message body with the PDFBox PDFTextStripper; merge merges the PDF files of the CamelPdfFilesToMerge header into one document.

Enum values:

  • create

  • append

  • extractText

  • merge

PdfOperation

Query Parameters

Name Description Default Type

font (producer)

The font to write the text with, for the create and append operations: one of the 14 standard Type 1 fonts of PDF (the PDFBox Standard14Fonts), which are not embedded in the document. The value is case-sensitive. All of them except SYMBOL and ZAPF_DINGBATS use WinAnsiEncoding (Windows-1252), which has the Western European Latin characters only, not for example Polish, Cyrillic, Greek or Chinese characters; SYMBOL has Greek letters and mathematical symbols but no Latin letters, and ZAPF_DINGBATS has dingbats. A character that the font does not have fails the operation: to write it, use the fontFile option, which replaces this option when it is set.

Enum values:

  • COURIER

  • COURIER_BOLD

  • COURIER_OBLIQUE

  • COURIER_BOLD_OBLIQUE

  • HELVETICA

  • HELVETICA_BOLD

  • HELVETICA_OBLIQUE

  • HELVETICA_BOLD_OBLIQUE

  • TIMES_ROMAN

  • TIMES_BOLD

  • TIMES_ITALIC

  • TIMES_BOLD_ITALIC

  • SYMBOL

  • ZAPF_DINGBATS

HELVETICA

String

fontFile (producer)

A TrueType font file to write the text with, for the create and append operations, instead of the standard font of the font option. Use it to write characters that the standard fonts do not have, such as Cyrillic, Greek or Chinese. The value is a resource URI, which can be prefixed with classpath: (the default), file:, http: or https:. The file is loaded once, when the endpoint is created, which fails when the file is missing or is not a font that PDFBox can load as a PDType0Font, such as an OpenType font with CFF outlines (most .otf files) or a font collection (.ttc). Only the characters that are written are embedded in the PDF document (a font subset).

String

fontSize (producer)

The font size, in PDF points (1/72 inch), of the text that the create and append operations write. The space between lines is twice the height of the bounding box of the font at this size.

14

float

marginBottom (producer)

The bottom margin of each page, in PDF points (1/72 inch), for the create and append operations: the text continues on a new page when the baseline of the next line would be closer than this distance to the bottom edge of the page.

20

int

marginLeft (producer)

The left margin of each page, in PDF points (1/72 inch), for the create and append operations: the lines start at this distance from the left edge of the page.

20

int

marginRight (producer)

The right margin of each page, in PDF points (1/72 inch), for the create and append operations. Only textProcessingFactory=autoFormatting uses it, to wrap the lines within the page width minus the left and right margins (at least 20 points must remain, or the operation fails); with lineTermination a long line runs past the right margin.

40

int

marginTop (producer)

The top margin of each page, in PDF points (1/72 inch), for the create and append operations: the baseline of the first line of a page is at this distance below the top edge of the page.

20

int

pageSize (producer)

The size of the pages that the create and append operations add, in portrait orientation: a PDFBox PDRectangle of LETTER (8.5 x 11 inch), LEGAL (8.5 x 14 inch) or the ISO sizes A0 to A6. The value is case-insensitive, and the older names with the PAGE_SIZE_ prefix, such as PAGE_SIZE_A4, are also accepted. An unknown value fails when the endpoint is created.

Enum values:

  • LETTER

  • LEGAL

  • A0

  • A1

  • A2

  • A3

  • A4

  • A5

  • A6

A4

String

textProcessingFactory (producer)

How the create and append operations lay out the text of the message body. lineTermination writes each line of the text (ending at \r\n, \r or \n) as one line of the page, without wrapping, so a line that is longer than the page runs past its right edge; a tab is written as four spaces and other control characters are left out. autoFormatting splits the text into words at whitespace (so the line breaks of the text are not kept), and fills each line with as many words as fit between the left and right margins; a word that is longer than a line is broken across lines. Both start a new page when the bottom margin is reached.

Enum values:

  • autoFormatting

  • lineTermination

lineTermination

TextProcessingFactory

lazyStartProducer (producer (advanced))

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 PDF component supports the following message header(s), which is/are listed below:

Name Description Default Type

CamelPdfProtectionPolicy (producer)

Constant: PROTECTION_POLICY_HEADER_NAME

The PDFBox ProtectionPolicy to encrypt the PDF document of the create and append operations with, such as a StandardProtectionPolicy (owner password, user password and AccessPermission). The create operation uses only a StandardProtectionPolicy and ignores any other type; append also takes a PublicKeyProtectionPolicy (encryption for the certificates of the recipients). Not used by the extractText and merge operations.

ProtectionPolicy

CamelPdfDocument (producer)

Constant: PDF_DOCUMENT_HEADER_NAME

The PDF document that the append operation writes the text of the message body to, on new pages at the end. Required for append, and not used by the other operations. A PDFBox PDDocument, or a byte[], InputStream, File, Path or ByteArrayOutputStream with the PDF, which the type converter of this component loads without a password. The operation closes the document after it saves the result. When the document is encrypted, the result is not encrypted, unless the CamelPdfProtectionPolicy header is set.

PDDocument

CamelPdfDecryptionMaterial (producer)

Constant: DECRYPTION_MATERIAL_HEADER_NAME

Deprecated Not used: a PDFBox DecryptionMaterial (such as a StandardDecryptionMaterial with the password) that was meant to decrypt the PDF document. The component never reads this header. Deprecation note: No longer read by the component since Camel 2.21: PDFBox decrypts a document when it loads it, with the password given to Loader.loadPDF(bytes, password) in PDFBox 3. The byte[] converter loads without a password. Decrypt in a Processor before the PDF endpoint instead.

DecryptionMaterial

CamelPdfFilesToMerge (producer)

Constant: FILES_TO_MERGE_HEADER_NAME

The PDF files that the merge operation merges into one document, in the order of the list: a java.util.List of at least two elements, each a java.io.File or a java.nio.file.Path. Required for merge, and not used by the other operations.

List

Message Body

The message body at each side of the PDF 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

text, bytes, stream or file

The text to write for create and append, which takes the document from the CamelPdfDocument header; a Map or a POJO is written as its toString: marshal it first. extractText reads a PDF from bytes, such as the ByteArrayOutputStream of create, a stream, a file or a PDDocument, not from text; another body, or none, fails. merge takes the files from the CamelPdfFilesToMerge header.

  • With operation=extractText: bytes, stream, file or pojo

  • With operation=merge: any

Producer output

bytes (java.io.ByteArrayOutputStream)

The PDF of create, append and merge as bytes, in a ByteArrayOutputStream, which extractText reads as it is. The text of the document for extractText.

  • With operation=extractText: text (java.lang.String)

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

Type converter

Since Camel 4.8, the component is capable of doing simple document conversions. For instance, suppose you are receiving a PDF byte as a byte array:

  • Java

  • XML

  • YAML

from("direct:start")
    .to("pdf:extractText")
    .to("mock:result");
<route>
  <from uri="direct:start"/>
  <to uri="pdf:extractText"/>
  <to uri="mock:result"/>
</route>
- route:
    from:
      uri: direct:start
      steps:
        - to:
            uri: pdf:extractText
        - to:
            uri: mock:result

It is now possible to get the body as a PD Document by using PDDocument doc = exchange.getIn().getBody(PDDocument.class);, which saves the trouble of converting the byte-array to a document.

The type converter reads a PDF from a byte[], a ByteArrayOutputStream, an InputStream, a java.io.File or a java.nio.file.Path. The create, append and merge operations leave the PDF in a ByteArrayOutputStream, so pdf:create can be followed by pdf:extractText directly. The extractText operation fails with an InvalidPayloadException naming the type of the body when the body is missing or is not one of these types.

this only works for unprotected PDF files. For password-protected, the files still need to be converted manually.

Creating a PDF from text

The create operation needs a body with the text to write; without a body it fails with an InvalidPayloadException.

With the default textProcessingFactory=lineTermination, the text is written line by line: a line ends at \r\n, \r or \n. A tab is written as four spaces, and other control characters, which the fonts cannot show, are left out.

The pageSize option takes LETTER, LEGAL or A0 to A6 (default A4). The older names PAGE_SIZE_A0 to PAGE_SIZE_A6 and PAGE_SIZE_LETTER are still accepted. An unknown value fails when the endpoint is created.

Fonts

The font option selects one of the 14 standard fonts of PDF (default HELVETICA). These fonts are not embedded in the document. All of them except SYMBOL and ZAPF_DINGBATS cover only the Western European Latin characters of WinAnsiEncoding (Windows-1252); SYMBOL has Greek letters and mathematical symbols, and ZAPF_DINGBATS has dingbats. A character the font does not have, such as Cyrillic, Chinese or an emoji, fails the create or append operation with an IllegalArgumentException that names the character.

To write such characters, set the fontFile option to a TrueType font file (.ttf) that has them. The font file is loaded as a resource when the endpoint is created, so it can be prefixed with classpath: (the default), file: or http:, and the characters that are used are embedded in the document. When fontFile is set, the font option is not used. A character the font file does not have also fails the operation, rather than being left out.

  • Java

  • YAML

from("direct:start")
    .to("pdf:create?fontFile=file:/fonts/NotoSans-Regular.ttf")
    .to("file:target/out?fileName=hello.pdf");
- route:
    from:
      uri: direct:start
      steps:
        - to:
            uri: pdf:create
            parameters:
              fontFile: file:/fonts/NotoSans-Regular.ttf
        - to:
            uri: file:target/out
            parameters:
              fileName: hello.pdf

Choose a font that has the characters of the text. It must be a TrueType font: OpenType fonts with CFF outlines (most .otf files) and font collections (.ttc) cannot be used, and fail when the endpoint is created.

Merging PDF files

The merge operation reads the files to merge from the CamelPdfFilesToMerge header: a list of at least two elements, each a java.io.File or a java.nio.file.Path.