This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 00c42022f567 CAMEL-15912: camel-pdf - write text with a TrueType font
file of the new fontFile option (#30885)
00c42022f567 is described below
commit 00c42022f56769b5b2ea56b91b3082292b2625de
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Nov 26 19:14:42 2026 +0100
CAMEL-15912: camel-pdf - write text with a TrueType font file of the new
fontFile option (#30885)
The font option takes only the 14 standard fonts of PDF, which cover Latin
text,
so create and append failed on Cyrillic, Greek, Chinese or emoji text with
no way
around it. The new fontFile option loads a TrueType font as a resource
(classpath:, file:, http:) when the endpoint is created, and both text
processing
factories write and measure the text with it, embedded as a subset in the
document. Without fontFile, the error of a character the standard font
cannot
show now points to the option.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Claus Ibsen <[email protected]>
---
.../org/apache/camel/catalog/components/pdf.json | 19 +--
.../apache/camel/catalog/docs/pdf-component.adoc | 44 +++++
.../camel/component/pdf/PdfEndpointConfigurer.java | 6 +
.../camel/component/pdf/PdfEndpointUriFactory.java | 3 +-
.../org/apache/camel/component/pdf/pdf.json | 19 +--
.../camel-pdf/src/main/docs/pdf-component.adoc | 44 +++++
.../camel/component/pdf/PdfConfiguration.java | 34 +++-
.../apache/camel/component/pdf/PdfEndpoint.java | 31 ++++
.../apache/camel/component/pdf/PdfProducer.java | 17 +-
.../pdf/text/DefaultLineBuilderStrategy.java | 41 +++--
.../component/pdf/text/DefaultWriteStrategy.java | 18 +--
.../text/LineTerminationWriterAbstractFactory.java | 3 +-
.../apache/camel/component/pdf/text/PdfUtils.java | 19 +++
.../camel/component/pdf/PdfFontFileTest.java | 177 +++++++++++++++++++++
.../endpoint/dsl/PdfEndpointBuilderFactory.java | 26 ++-
15 files changed, 449 insertions(+), 52 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pdf.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pdf.json
index 349dc587262b..d54bed92663d 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pdf.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pdf.json
@@ -39,14 +39,15 @@
},
"properties": {
"operation": { "index": 0, "kind": "path", "displayName": "Operation",
"group": "producer", "label": "", "required": true, "type": "enum", "javaType":
"org.apache.camel.component.pdf.PdfOperation", "enum": [ "create", "append",
"extractText", "merge" ], "deprecated": false, "autowired": false, "secret":
false, "configurationClass": "org.apache.camel.component.pdf.PdfConfiguration",
"configurationField": "pdfConfiguration", "description": "Operation type" },
- "font": { "index": 1, "kind": "parameter", "displayName": "Font", "group":
"producer", "label": "", "required": false, "type": "enum", "javaType":
"java.lang.String", "enum": [ "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" ], "deprecated": false,
"autowired": false, "secret": false, "defaultVa [...]
- "fontSize": { "index": 2, "kind": "parameter", "displayName": "Font Size",
"group": "producer", "label": "", "required": false, "type": "number",
"javaType": "float", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 14.0, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Font size in pixels" },
- "marginBottom": { "index": 3, "kind": "parameter", "displayName": "Margin
Bottom", "group": "producer", "label": "", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin bottom in pixels" },
- "marginLeft": { "index": 4, "kind": "parameter", "displayName": "Margin
Left", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin left in pixels" },
- "marginRight": { "index": 5, "kind": "parameter", "displayName": "Margin
Right", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 40, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin right in pixels" },
- "marginTop": { "index": 6, "kind": "parameter", "displayName": "Margin
Top", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin top in pixels" },
- "pageSize": { "index": 7, "kind": "parameter", "displayName": "Page Size",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "LETTER", "LEGAL", "A0", "A1", "A2",
"A3", "A4", "A5", "A6" ], "deprecated": false, "autowired": false, "secret":
false, "defaultValue": "A4", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Page size: LETTER, LEGAL or [...]
- "textProcessingFactory": { "index": 8, "kind": "parameter", "displayName":
"Text Processing Factory", "group": "producer", "label": "", "required": false,
"type": "enum", "javaType":
"org.apache.camel.component.pdf.TextProcessingFactory", "enum": [
"autoFormatting", "lineTermination" ], "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "lineTermination", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfigur [...]
- "lazyStartProducer": { "index": 9, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produc [...]
+ "font": { "index": 1, "kind": "parameter", "displayName": "Font", "group":
"producer", "label": "", "required": false, "type": "enum", "javaType":
"java.lang.String", "enum": [ "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" ], "deprecated": false,
"autowired": false, "secret": false, "defaultVa [...]
+ "fontFile": { "index": 2, "kind": "parameter", "displayName": "Font File",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "supportFileReference": true, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "A TrueType font file (.ttf) to write the
text with, instead of the standard font of the fon [...]
+ "fontSize": { "index": 3, "kind": "parameter", "displayName": "Font Size",
"group": "producer", "label": "", "required": false, "type": "number",
"javaType": "float", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 14.0, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Font size in pixels" },
+ "marginBottom": { "index": 4, "kind": "parameter", "displayName": "Margin
Bottom", "group": "producer", "label": "", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin bottom in pixels" },
+ "marginLeft": { "index": 5, "kind": "parameter", "displayName": "Margin
Left", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin left in pixels" },
+ "marginRight": { "index": 6, "kind": "parameter", "displayName": "Margin
Right", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 40, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin right in pixels" },
+ "marginTop": { "index": 7, "kind": "parameter", "displayName": "Margin
Top", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin top in pixels" },
+ "pageSize": { "index": 8, "kind": "parameter", "displayName": "Page Size",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "LETTER", "LEGAL", "A0", "A1", "A2",
"A3", "A4", "A5", "A6" ], "deprecated": false, "autowired": false, "secret":
false, "defaultValue": "A4", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Page size: LETTER, LEGAL or [...]
+ "textProcessingFactory": { "index": 9, "kind": "parameter", "displayName":
"Text Processing Factory", "group": "producer", "label": "", "required": false,
"type": "enum", "javaType":
"org.apache.camel.component.pdf.TextProcessingFactory", "enum": [
"autoFormatting", "lineTermination" ], "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "lineTermination", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfigur [...]
+ "lazyStartProducer": { "index": 10, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produ [...]
}
}
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pdf-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pdf-component.adoc
index 913cf95edc27..8463d87ae5b9 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pdf-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pdf-component.adoc
@@ -113,6 +113,50 @@ With the default `textProcessingFactory=lineTermination`,
the text is written li
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, and cover only Latin text: a character they do not have, such as
Cyrillic, Greek, 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.
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:start")
+ .to("pdf:create?fontFile=file:/fonts/NotoSans-Regular.ttf")
+ .to("file:target/out?fileName=hello.pdf");
+----
+
+YAML::
++
+[source,yaml]
+----
+- 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
diff --git
a/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointConfigurer.java
b/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointConfigurer.java
index edef345de92c..f945221429fc 100644
---
a/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointConfigurer.java
+++
b/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointConfigurer.java
@@ -25,6 +25,8 @@ public class PdfEndpointConfigurer extends
PropertyConfigurerSupport implements
PdfEndpoint target = (PdfEndpoint) obj;
switch (ignoreCase ? name.toLowerCase(Locale.ROOT) : name) {
case "font":
target.getPdfConfiguration().setFont(property(camelContext,
java.lang.String.class, value)); return true;
+ case "fontfile":
+ case "fontFile":
target.getPdfConfiguration().setFontFile(property(camelContext,
java.lang.String.class, value)); return true;
case "fontsize":
case "fontSize":
target.getPdfConfiguration().setFontSize(property(camelContext, float.class,
value)); return true;
case "lazystartproducer":
@@ -49,6 +51,8 @@ public class PdfEndpointConfigurer extends
PropertyConfigurerSupport implements
public Class<?> getOptionType(String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase(Locale.ROOT) : name) {
case "font": return java.lang.String.class;
+ case "fontfile":
+ case "fontFile": return java.lang.String.class;
case "fontsize":
case "fontSize": return float.class;
case "lazystartproducer":
@@ -74,6 +78,8 @@ public class PdfEndpointConfigurer extends
PropertyConfigurerSupport implements
PdfEndpoint target = (PdfEndpoint) obj;
switch (ignoreCase ? name.toLowerCase(Locale.ROOT) : name) {
case "font": return target.getPdfConfiguration().getFont();
+ case "fontfile":
+ case "fontFile": return target.getPdfConfiguration().getFontFile();
case "fontsize":
case "fontSize": return target.getPdfConfiguration().getFontSize();
case "lazystartproducer":
diff --git
a/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointUriFactory.java
b/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointUriFactory.java
index b833baa716a0..278cede2d877 100644
---
a/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointUriFactory.java
+++
b/components/camel-pdf/src/generated/java/org/apache/camel/component/pdf/PdfEndpointUriFactory.java
@@ -24,8 +24,9 @@ public class PdfEndpointUriFactory extends
org.apache.camel.support.component.En
private static final Set<String> ENDPOINT_IDENTITY_PROPERTY_NAMES;
private static final Map<String, String> MULTI_VALUE_PREFIXES;
static {
- Set<String> props = new HashSet<>(10);
+ Set<String> props = new HashSet<>(11);
props.add("font");
+ props.add("fontFile");
props.add("fontSize");
props.add("lazyStartProducer");
props.add("marginBottom");
diff --git
a/components/camel-pdf/src/generated/resources/META-INF/org/apache/camel/component/pdf/pdf.json
b/components/camel-pdf/src/generated/resources/META-INF/org/apache/camel/component/pdf/pdf.json
index 349dc587262b..d54bed92663d 100644
---
a/components/camel-pdf/src/generated/resources/META-INF/org/apache/camel/component/pdf/pdf.json
+++
b/components/camel-pdf/src/generated/resources/META-INF/org/apache/camel/component/pdf/pdf.json
@@ -39,14 +39,15 @@
},
"properties": {
"operation": { "index": 0, "kind": "path", "displayName": "Operation",
"group": "producer", "label": "", "required": true, "type": "enum", "javaType":
"org.apache.camel.component.pdf.PdfOperation", "enum": [ "create", "append",
"extractText", "merge" ], "deprecated": false, "autowired": false, "secret":
false, "configurationClass": "org.apache.camel.component.pdf.PdfConfiguration",
"configurationField": "pdfConfiguration", "description": "Operation type" },
- "font": { "index": 1, "kind": "parameter", "displayName": "Font", "group":
"producer", "label": "", "required": false, "type": "enum", "javaType":
"java.lang.String", "enum": [ "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" ], "deprecated": false,
"autowired": false, "secret": false, "defaultVa [...]
- "fontSize": { "index": 2, "kind": "parameter", "displayName": "Font Size",
"group": "producer", "label": "", "required": false, "type": "number",
"javaType": "float", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 14.0, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Font size in pixels" },
- "marginBottom": { "index": 3, "kind": "parameter", "displayName": "Margin
Bottom", "group": "producer", "label": "", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin bottom in pixels" },
- "marginLeft": { "index": 4, "kind": "parameter", "displayName": "Margin
Left", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin left in pixels" },
- "marginRight": { "index": 5, "kind": "parameter", "displayName": "Margin
Right", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 40, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin right in pixels" },
- "marginTop": { "index": 6, "kind": "parameter", "displayName": "Margin
Top", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin top in pixels" },
- "pageSize": { "index": 7, "kind": "parameter", "displayName": "Page Size",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "LETTER", "LEGAL", "A0", "A1", "A2",
"A3", "A4", "A5", "A6" ], "deprecated": false, "autowired": false, "secret":
false, "defaultValue": "A4", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Page size: LETTER, LEGAL or [...]
- "textProcessingFactory": { "index": 8, "kind": "parameter", "displayName":
"Text Processing Factory", "group": "producer", "label": "", "required": false,
"type": "enum", "javaType":
"org.apache.camel.component.pdf.TextProcessingFactory", "enum": [
"autoFormatting", "lineTermination" ], "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "lineTermination", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfigur [...]
- "lazyStartProducer": { "index": 9, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produc [...]
+ "font": { "index": 1, "kind": "parameter", "displayName": "Font", "group":
"producer", "label": "", "required": false, "type": "enum", "javaType":
"java.lang.String", "enum": [ "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" ], "deprecated": false,
"autowired": false, "secret": false, "defaultVa [...]
+ "fontFile": { "index": 2, "kind": "parameter", "displayName": "Font File",
"group": "producer", "label": "", "required": false, "type": "string",
"javaType": "java.lang.String", "deprecated": false, "autowired": false,
"secret": false, "supportFileReference": true, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "A TrueType font file (.ttf) to write the
text with, instead of the standard font of the fon [...]
+ "fontSize": { "index": 3, "kind": "parameter", "displayName": "Font Size",
"group": "producer", "label": "", "required": false, "type": "number",
"javaType": "float", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 14.0, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Font size in pixels" },
+ "marginBottom": { "index": 4, "kind": "parameter", "displayName": "Margin
Bottom", "group": "producer", "label": "", "required": false, "type":
"integer", "javaType": "int", "deprecated": false, "autowired": false,
"secret": false, "defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin bottom in pixels" },
+ "marginLeft": { "index": 5, "kind": "parameter", "displayName": "Margin
Left", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin left in pixels" },
+ "marginRight": { "index": 6, "kind": "parameter", "displayName": "Margin
Right", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 40, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin right in pixels" },
+ "marginTop": { "index": 7, "kind": "parameter", "displayName": "Margin
Top", "group": "producer", "label": "", "required": false, "type": "integer",
"javaType": "int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 20, "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Margin top in pixels" },
+ "pageSize": { "index": 8, "kind": "parameter", "displayName": "Page Size",
"group": "producer", "label": "", "required": false, "type": "enum",
"javaType": "java.lang.String", "enum": [ "LETTER", "LEGAL", "A0", "A1", "A2",
"A3", "A4", "A5", "A6" ], "deprecated": false, "autowired": false, "secret":
false, "defaultValue": "A4", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfiguration", "description": "Page size: LETTER, LEGAL or [...]
+ "textProcessingFactory": { "index": 9, "kind": "parameter", "displayName":
"Text Processing Factory", "group": "producer", "label": "", "required": false,
"type": "enum", "javaType":
"org.apache.camel.component.pdf.TextProcessingFactory", "enum": [
"autoFormatting", "lineTermination" ], "deprecated": false, "autowired": false,
"secret": false, "defaultValue": "lineTermination", "configurationClass":
"org.apache.camel.component.pdf.PdfConfiguration", "configurationField":
"pdfConfigur [...]
+ "lazyStartProducer": { "index": 10, "kind": "parameter", "displayName":
"Lazy Start Producer", "group": "producer (advanced)", "label":
"producer,advanced", "required": false, "type": "boolean", "javaType":
"boolean", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": false, "description": "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 produ [...]
}
}
diff --git a/components/camel-pdf/src/main/docs/pdf-component.adoc
b/components/camel-pdf/src/main/docs/pdf-component.adoc
index 913cf95edc27..8463d87ae5b9 100644
--- a/components/camel-pdf/src/main/docs/pdf-component.adoc
+++ b/components/camel-pdf/src/main/docs/pdf-component.adoc
@@ -113,6 +113,50 @@ With the default `textProcessingFactory=lineTermination`,
the text is written li
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, and cover only Latin text: a character they do not have, such as
Cyrillic, Greek, 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.
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:start")
+ .to("pdf:create?fontFile=file:/fonts/NotoSans-Regular.ttf")
+ .to("file:target/out?fileName=hello.pdf");
+----
+
+YAML::
++
+[source,yaml]
+----
+- 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
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java
index 57c0523ac96c..8f39f49b9d6f 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfConfiguration.java
@@ -68,6 +68,11 @@ public class PdfConfiguration {
+
"TIMES_ROMAN,TIMES_BOLD,TIMES_ITALIC,TIMES_BOLD_ITALIC,"
+ "SYMBOL,ZAPF_DINGBATS")
private String font = "HELVETICA";
+ @UriParam
+ @Metadata(supportFileReference = true)
+ private String fontFile;
+ // the content of fontFile, loaded when the endpoint is initialized
+ private byte[] fontFileContent;
@UriParam(defaultValue = "lineTermination")
private TextProcessingFactory textProcessingFactory =
TextProcessingFactory.lineTermination;
@@ -170,12 +175,39 @@ public class PdfConfiguration {
}
/**
- * Font
+ * One of the 14 standard fonts of PDF. These fonts cover only Latin text:
to write other characters, such as
+ * Cyrillic, Greek or Chinese, use the fontFile option.
*/
public void setFont(String font) {
this.font = font;
}
+ public String getFontFile() {
+ return fontFile;
+ }
+
+ /**
+ * A TrueType font file (.ttf) to write the text with, 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 font is loaded as a
+ * resource: prefix with classpath:, file: or http: (classpath: is the
default). The characters that are used are
+ * embedded in the PDF document.
+ */
+ public void setFontFile(String fontFile) {
+ this.fontFile = fontFile;
+ }
+
+ /**
+ * The content of the font file of the fontFile option, which the endpoint
loads when it is initialized. This is not
+ * an endpoint option.
+ */
+ public byte[] getFontFileContent() {
+ return fontFileContent;
+ }
+
+ public void setFontFileContent(byte[] fontFileContent) {
+ this.fontFileContent = fontFileContent;
+ }
+
public TextProcessingFactory getTextProcessingFactory() {
return textProcessingFactory;
}
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java
index 547edd9c18b8..fe224725ba2e 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfEndpoint.java
@@ -16,6 +16,10 @@
*/
package org.apache.camel.component.pdf;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
import org.apache.camel.Category;
import org.apache.camel.Component;
import org.apache.camel.Consumer;
@@ -27,6 +31,10 @@ import org.apache.camel.spi.BodyTypes;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.support.DefaultEndpoint;
+import org.apache.camel.support.ResourceHelper;
+import org.apache.camel.util.IOHelper;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.font.PDType0Font;
/**
* Create, modify or extract content from PDF documents.
@@ -68,6 +76,29 @@ public class PdfEndpoint extends DefaultEndpoint {
return false;
}
+ @Override
+ protected void doInit() throws Exception {
+ super.doInit();
+ String fontFile = pdfConfiguration.getFontFile();
+ if (fontFile != null && pdfConfiguration.getFontFileContent() == null)
{
+ // load the font once, and fail when the endpoint is created
rather than with the first message
+ byte[] content;
+ InputStream is =
ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext(),
fontFile);
+ try {
+ content = is.readAllBytes();
+ } finally {
+ IOHelper.close(is);
+ }
+ // check that PDFBox can use the font, such as not an OpenType
font with CFF outlines
+ try (PDDocument document = new PDDocument()) {
+ PDType0Font.load(document, new ByteArrayInputStream(content));
+ } catch (IOException e) {
+ throw new IOException(String.format("Cannot use the fontFile
%s: %s", fontFile, e.getMessage()), e);
+ }
+ pdfConfiguration.setFontFileContent(content);
+ }
+ }
+
@Override
public Producer createProducer() throws Exception {
return new PdfProducer(this);
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java
index 2b99b90dac61..431bb371b2d1 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/PdfProducer.java
@@ -163,9 +163,20 @@ public class PdfProducer extends DefaultProducer {
}
private void appendToPdfDocument(String text, PDDocument document,
ProtectionPolicy protectionPolicy) throws IOException {
- Collection<String> words = splitStrategy.split(text);
- Collection<String> lines = lineBuilderStrategy.buildLines(words);
- writeStrategy.write(lines, document);
+ try {
+ Collection<String> words = splitStrategy.split(text);
+ Collection<String> lines = lineBuilderStrategy.buildLines(words);
+ writeStrategy.write(lines, document);
+ } catch (IllegalArgumentException e) {
+ if (pdfConfiguration.getFontFile() == null) {
+ // PDFBox fails on a character that the standard font cannot
show, such as Cyrillic or Chinese
+ throw new IllegalArgumentException(
+ e.getMessage() + ". The standard fonts of the font
option cover only Latin text:"
+ + " use the fontFile option
with a TrueType font that has these characters",
+ e);
+ }
+ throw e;
+ }
if (protectionPolicy != null) {
document.protect(protectionPolicy);
}
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java
index 3bec47adc52f..d700f8065d74 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultLineBuilderStrategy.java
@@ -23,8 +23,8 @@ import java.util.LinkedList;
import org.apache.camel.component.pdf.PdfConfiguration;
import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.pdfbox.pdmodel.font.PDType1Font;
-import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.font.PDFont;
import static org.apache.camel.component.pdf.PdfConstants.MIN_CONTENT_WIDTH;
@@ -50,13 +50,21 @@ public class DefaultLineBuilderStrategy implements
LineBuilderStrategy {
*/
@Override
public Collection<String> buildLines(Collection<String> splittedText)
throws IOException {
+ // the font is only used to measure the text, so a font file is loaded
into a document that is not saved
+ try (PDDocument document = new PDDocument()) {
+ PDFont font = PdfUtils.createFont(pdfConfiguration, document);
+ return buildLines(splittedText, font);
+ }
+ }
+
+ private Collection<String> buildLines(Collection<String> splittedText,
PDFont font) throws IOException {
LinkedList<String> wordsList = new LinkedList<>(splittedText);
Collection<String> lines = new ArrayList<>();
LineBuilder currentLine = new LineBuilder();
float allowedLineWidth = getAllowedLineWidth();
while (!wordsList.isEmpty()) {
String word = wordsList.removeFirst();
- if (isWordFitInCurrentLine(currentLine, word, allowedLineWidth)) {
+ if (isWordFitInCurrentLine(currentLine, word, font,
allowedLineWidth)) {
currentLine.appendWord(word);
if (wordsList.isEmpty()) {
lines.add(currentLine.buildLine());
@@ -65,7 +73,7 @@ public class DefaultLineBuilderStrategy implements
LineBuilderStrategy {
lines.add(currentLine.buildLine());
wordsList.addFirst(word);
} else {
- int splitIndex = findSplitIndex(word, allowedLineWidth);
+ int splitIndex = findSplitIndex(word, font, allowedLineWidth);
currentLine.appendWord(word.substring(0, splitIndex));
lines.add(currentLine.buildLine());
wordsList.addFirst(word.substring(splitIndex));
@@ -74,7 +82,7 @@ public class DefaultLineBuilderStrategy implements
LineBuilderStrategy {
return lines;
}
- private int findSplitIndex(String word, float allowedLineWidth) throws
IOException {
+ private int findSplitIndex(String word, PDFont font, float
allowedLineWidth) throws IOException {
// Using binary search algorithm to find max amount of characters that
fit int the line.
int middle = word.length() >> 1;
@@ -83,33 +91,32 @@ public class DefaultLineBuilderStrategy implements
LineBuilderStrategy {
int currentSplitIndex = 0;
do {
- if (isLineFitInLineWidth(word.substring(0, middle),
allowedLineWidth)) {
+ if (isLineFitInLineWidth(word.substring(0, middle), font,
allowedLineWidth)) {
currentSplitIndex = middle;
middle += word.substring(middle, end).length() >> 1;
} else {
end = middle;
middle = currentSplitIndex +
(word.substring(currentSplitIndex, middle).length() >> 1);
}
- } while (currentSplitIndex == -1 || !isSplitIndexFound(word,
allowedLineWidth, currentSplitIndex));
+ } while (currentSplitIndex == -1 || !isSplitIndexFound(word, font,
allowedLineWidth, currentSplitIndex));
return currentSplitIndex;
}
- private boolean isSplitIndexFound(String word, float allowedLineWidth, int
currentSplitIndex) throws IOException {
- return isLineFitInLineWidth(word.substring(0, currentSplitIndex),
allowedLineWidth)
- && !isLineFitInLineWidth(word.substring(0, currentSplitIndex +
1), allowedLineWidth);
+ private boolean isSplitIndexFound(String word, PDFont font, float
allowedLineWidth, int currentSplitIndex)
+ throws IOException {
+ return isLineFitInLineWidth(word.substring(0, currentSplitIndex),
font, allowedLineWidth)
+ && !isLineFitInLineWidth(word.substring(0, currentSplitIndex +
1), font, allowedLineWidth);
}
- private boolean isWordFitInCurrentLine(LineBuilder currentLine, String
word, float allowedLineWidth) throws IOException {
+ private boolean isWordFitInCurrentLine(LineBuilder currentLine, String
word, PDFont font, float allowedLineWidth)
+ throws IOException {
LineBuilder lineBuilder = currentLine.copy().appendWord(word);
- return isLineFitInLineWidth(lineBuilder.buildLine(), allowedLineWidth);
+ return isLineFitInLineWidth(lineBuilder.buildLine(), font,
allowedLineWidth);
}
- private boolean isLineFitInLineWidth(String currentLine, float
allowedLineWidth) throws IOException {
- float fontWidth = PdfUtils.getFontWidth(
- currentLine,
- new
PDType1Font(Standard14Fonts.FontName.valueOf(pdfConfiguration.getFont())),
- pdfConfiguration.getFontSize());
+ private boolean isLineFitInLineWidth(String currentLine, PDFont font,
float allowedLineWidth) throws IOException {
+ float fontWidth = PdfUtils.getFontWidth(currentLine, font,
pdfConfiguration.getFontSize());
return fontWidth <= allowedLineWidth;
}
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java
index 3dd7639024bd..72437dbeda9b 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/DefaultWriteStrategy.java
@@ -23,8 +23,7 @@ import org.apache.camel.component.pdf.PdfConfiguration;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
-import org.apache.pdfbox.pdmodel.font.PDType1Font;
-import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
+import org.apache.pdfbox.pdmodel.font.PDFont;
/**
* Writes given lines to PDF document. If document already contains some text
then new text will be appended to new
@@ -44,12 +43,12 @@ public class DefaultWriteStrategy implements WriteStrategy {
document.addPage(page);
float x = pdfConfiguration.getMarginLeft();
float y = page.getMediaBox().getHeight() -
pdfConfiguration.getMarginTop();
- float averageFontHeight
- = PdfUtils.getAverageFontHeight(new
PDType1Font(Standard14Fonts.FontName.valueOf(pdfConfiguration.getFont())),
- pdfConfiguration.getFontSize());
+ // one font for all the pages, so a font file is embedded once in the
document
+ PDFont font = PdfUtils.createFont(pdfConfiguration, document);
+ float averageFontHeight = PdfUtils.getAverageFontHeight(font,
pdfConfiguration.getFontSize());
float lineSpacing = averageFontHeight * 2;
- PDPageContentStream contentStream = initializeContentStream(document,
page);
+ PDPageContentStream contentStream = initializeContentStream(document,
page, font);
for (String line : lines) {
writeLine(x, y, line, contentStream);
y -= lineSpacing;
@@ -57,7 +56,7 @@ public class DefaultWriteStrategy implements WriteStrategy {
contentStream.close();
page = new PDPage(pdfConfiguration.getPageSize());
document.addPage(page);
- contentStream = initializeContentStream(document, page);
+ contentStream = initializeContentStream(document, page, font);
y = page.getMediaBox().getHeight() -
pdfConfiguration.getMarginTop();
}
}
@@ -76,10 +75,9 @@ public class DefaultWriteStrategy implements WriteStrategy {
contentStream.endText();
}
- private PDPageContentStream initializeContentStream(PDDocument document,
PDPage page) throws IOException {
+ private PDPageContentStream initializeContentStream(PDDocument document,
PDPage page, PDFont font) throws IOException {
PDPageContentStream contentStream = new PDPageContentStream(document,
page);
- contentStream.setFont(new
PDType1Font(Standard14Fonts.FontName.valueOf(pdfConfiguration.getFont())),
- pdfConfiguration.getFontSize());
+ contentStream.setFont(font, pdfConfiguration.getFontSize());
return contentStream;
}
}
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java
index f7552a5bc453..b288802b4bd3 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/LineTerminationWriterAbstractFactory.java
@@ -26,7 +26,8 @@ import org.apache.camel.component.pdf.PdfConfiguration;
/**
* Builds set of classes for line-termination writing strategy. Text getting
sliced by line termination symbol (\r\n, \r
* or \n) and then it will be written regardless it fits in the line or not. A
tab is written as spaces, and other
- * control characters, which the fonts cannot show, are left out.
+ * control characters, which the fonts cannot show, are left out. Other
characters that the font cannot show fail the
+ * writing.
*/
public class LineTerminationWriterAbstractFactory implements
TextProcessingAbstractFactory {
diff --git
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java
index 5713303a2cda..e85203022edf 100644
---
a/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java
+++
b/components/camel-pdf/src/main/java/org/apache/camel/component/pdf/text/PdfUtils.java
@@ -16,9 +16,15 @@
*/
package org.apache.camel.component.pdf.text;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
+import org.apache.camel.component.pdf.PdfConfiguration;
+import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType0Font;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.apache.pdfbox.pdmodel.font.Standard14Fonts;
import static org.apache.camel.component.pdf.PdfConstants.PDF_PIXEL_SIZE;
@@ -26,6 +32,19 @@ public final class PdfUtils {
private PdfUtils() {
}
+ /**
+ * The font to write the text with: the font file of the fontFile option,
which is embedded in the given document,
+ * or else the standard font of the font option.
+ */
+ public static PDFont createFont(PdfConfiguration pdfConfiguration,
PDDocument document) throws IOException {
+ byte[] fontFile = pdfConfiguration.getFontFileContent();
+ if (fontFile != null) {
+ // a Type0 font belongs to the document it is embedded in, so it
is loaded for each document
+ return PDType0Font.load(document, new
ByteArrayInputStream(fontFile));
+ }
+ return new
PDType1Font(Standard14Fonts.FontName.valueOf(pdfConfiguration.getFont()));
+ }
+
public static float getAverageFontHeight(PDFont font, float fontSize)
throws IOException {
return font.getBoundingBox().getHeight() / PDF_PIXEL_SIZE * fontSize;
}
diff --git
a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfFontFileTest.java
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfFontFileTest.java
new file mode 100644
index 000000000000..9a807056d178
--- /dev/null
+++
b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfFontFileTest.java
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pdf;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.apache.pdfbox.Loader;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.PDResources;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDType0Font;
+import org.apache.pdfbox.text.PDFTextStripper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class PdfFontFileTest extends CamelTestSupport {
+
+ // a TrueType font with Latin, Cyrillic and Greek characters (SIL Open
Font License), which PDFBox ships
+ private static final String FONT_RESOURCE =
"org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf";
+ private static final String FONT_FILE = "classpath:" + FONT_RESOURCE;
+ private static final String TEXT = "Hello Привет мир Γειά σου κόσμε";
+
+ @TempDir
+ Path tempDir;
+
+ @Test
+ public void testStandardFontCannotWriteCyrillic() {
+ Exception e = assertThrows(CamelExecutionException.class,
+ () -> template.requestBody("pdf:create", TEXT));
+ IllegalArgumentException cause =
assertInstanceOf(IllegalArgumentException.class, e.getCause());
+ assertThat(cause.getMessage(), containsString("is not available in the
font Helvetica"));
+ assertThat(cause.getMessage(), containsString("fontFile"));
+ }
+
+ @Test
+ public void testCreateWithFontFile() throws Exception {
+ Object body = template.requestBody("pdf:create?fontFile=" + FONT_FILE,
TEXT + "\nsecond line");
+ try (PDDocument doc = load(body)) {
+ String text = new PDFTextStripper().getText(doc);
+ assertThat(text, containsString(TEXT));
+ assertThat(text, containsString("second line"));
+ assertEmbeddedFontFile(doc);
+ }
+ }
+
+ @Test
+ public void testCreateWithFontFileFromFile() throws Exception {
+ Path fontFile = tempDir.resolve("my-font.ttf");
+ try (InputStream is =
getClass().getClassLoader().getResourceAsStream(FONT_RESOURCE)) {
+ Files.copy(is, fontFile);
+ }
+ Object body = template.requestBody("pdf:create?fontFile=file:" +
fontFile.toAbsolutePath(), TEXT);
+ try (PDDocument doc = load(body)) {
+ assertThat(new PDFTextStripper().getText(doc),
containsString(TEXT));
+ assertEmbeddedFontFile(doc);
+ }
+ }
+
+ @Test
+ public void testAutoFormattingWithFontFile() throws Exception {
+ // many words, so the text is wrapped over many lines and pages,
measured with the font file
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1500; i++) {
+ sb.append("Привет").append(i).append(' ');
+ }
+ Object body = template.requestBody(
+
"pdf:create?textProcessingFactory=autoFormatting&fontSize=12&fontFile=" +
FONT_FILE, sb.toString());
+ try (PDDocument doc = load(body)) {
+ assertTrue(doc.getNumberOfPages() > 1, "Expected the text on more
than one page");
+ String text = new PDFTextStripper().getText(doc);
+ assertThat(text, containsString("Привет0 "));
+ assertThat(text, containsString("Привет1499"));
+ // every page uses the same embedded font
+ assertEmbeddedFontFile(doc);
+ }
+ }
+
+ @Test
+ public void testAppendWithFontFile() throws Exception {
+ try (PDDocument document = PDFUtil.textToPDF("first page")) {
+ Object body = template.requestBodyAndHeader("pdf:append?fontFile="
+ FONT_FILE, TEXT,
+ PdfHeaderConstants.PDF_DOCUMENT_HEADER_NAME, document);
+ try (PDDocument doc = load(body)) {
+ assertEquals(2, doc.getNumberOfPages());
+ String text = new PDFTextStripper().getText(doc);
+ assertThat(text, containsString("first page"));
+ assertThat(text, containsString(TEXT));
+ }
+ }
+ }
+
+ @Test
+ public void testFontFileWithoutTheCharacter() {
+ // the font has no Chinese characters, which fails rather than leaving
them out
+ Exception e = assertThrows(CamelExecutionException.class,
+ () -> template.requestBody("pdf:create?fontFile=" + FONT_FILE,
"Hello 你好"));
+ assertThat(e.getCause().getMessage(), containsString("4F60"));
+ assertThat(e.getCause().getMessage(), not(containsString("use the
fontFile option")));
+ }
+
+ @Test
+ public void testUnknownFontFile() {
+ Exception e = assertThrows(Exception.class,
+ () ->
context.getEndpoint("pdf:create?fontFile=classpath:no-such-font.ttf"));
+ assertThat(messages(e), containsString("no-such-font.ttf"));
+ }
+
+ @Test
+ public void testFontFileNotTrueType() throws Exception {
+ Path fontFile = tempDir.resolve("not-a-font.ttf");
+ Files.writeString(fontFile, "this is not a font");
+ Exception e = assertThrows(Exception.class,
+ () -> context.getEndpoint("pdf:create?fontFile=file:" +
fontFile.toAbsolutePath()));
+ assertThat(messages(e), containsString("Cannot use the fontFile"));
+ }
+
+ private static String messages(Throwable e) {
+ // the messages of the exception and its causes
+ StringBuilder sb = new StringBuilder();
+ for (Throwable t = e; t != null; t = t.getCause()) {
+ sb.append(t.getMessage()).append('\n');
+ }
+ return sb.toString();
+ }
+
+ private static PDDocument load(Object body) throws IOException {
+ return Loader.loadPDF(((ByteArrayOutputStream) body).toByteArray());
+ }
+
+ private static void assertEmbeddedFontFile(PDDocument doc) throws
IOException {
+ Set<String> fontNames = new HashSet<>();
+ for (PDPage page : doc.getPages()) {
+ PDResources resources = page.getResources();
+ for (COSName name : resources.getFontNames()) {
+ PDFont font = resources.getFont(name);
+ assertInstanceOf(PDType0Font.class, font);
+ assertTrue(font.isEmbedded(), "Expected the font file embedded
in the document");
+ fontNames.add(font.getName());
+ }
+ }
+ // the font is embedded once, as a subset with a name such as
ABCDEF+LiberationSans
+ assertEquals(1, fontNames.size(), "Expected one embedded font, but got
" + fontNames);
+ assertThat(fontNames.iterator().next(),
containsString("LiberationSans"));
+ }
+}
diff --git
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PdfEndpointBuilderFactory.java
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PdfEndpointBuilderFactory.java
index 38643299881c..574468e3e24a 100644
---
a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PdfEndpointBuilderFactory.java
+++
b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PdfEndpointBuilderFactory.java
@@ -45,7 +45,9 @@ public interface PdfEndpointBuilderFactory {
}
/**
- * Font.
+ * One of the 14 standard fonts of PDF. These fonts cover only Latin
+ * text: to write other characters, such as Cyrillic, Greek or Chinese,
+ * use the fontFile option.
*
* The option is a: <code>java.lang.String</code> type.
*
@@ -59,6 +61,28 @@ public interface PdfEndpointBuilderFactory {
doSetProperty("font", font);
return this;
}
+ /**
+ * A TrueType font file (.ttf) to write the text with, 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
+ * font is loaded as a resource: prefix with classpath:, file: or http:
+ * (classpath: is the default). The characters that are used are
+ * embedded in the PDF document.
+ *
+ * This option can also be loaded from an existing file, by prefixing
+ * with file: or classpath: followed by the location of the file.
+ *
+ * The option is a: <code>java.lang.String</code> type.
+ *
+ * Group: producer
+ *
+ * @param fontFile the value to set
+ * @return the dsl builder
+ */
+ default PdfEndpointBuilder fontFile(String fontFile) {
+ doSetProperty("fontFile", fontFile);
+ return this;
+ }
/**
* Font size in pixels.
*