Skip to content

Commit

Permalink
CAMEL-21305: enable PDF conversions with stream cache
Browse files Browse the repository at this point in the history
  • Loading branch information
orpiske committed Oct 2, 2024
1 parent 8b05232 commit 7fbde8f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public void load(TypeConverterRegistry registry) throws TypeConverterLoaderExcep
private void registerConverters(TypeConverterRegistry registry) {
addTypeConverter(registry, org.apache.pdfbox.pdmodel.PDDocument.class, byte[].class, false,
(type, exchange, value) -> getPdfConverter().convertToPDF((byte[]) value));
addTypeConverter(registry, org.apache.pdfbox.pdmodel.PDDocument.class, java.io.InputStream.class, false,
(type, exchange, value) -> getPdfConverter().toPDDocument((java.io.InputStream) value));
}

private static void addTypeConverter(TypeConverterRegistry registry, Class<?> toType, Class<?> fromType, boolean allowNull, SimpleTypeConverter.ConversionMethod method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.camel.component.pdf.converter;

import java.io.InputStream;

import org.apache.camel.Converter;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
Expand All @@ -28,4 +30,10 @@ public class PdfConverter {
public PDDocument convertToPDF(byte[] bytes) throws Exception {
return Loader.loadPDF(bytes);
}

@Converter
public PDDocument toPDDocument(InputStream stream) throws Exception {
final byte[] bytes = stream.readAllBytes();
return convertToPDF(bytes);
}
}

0 comments on commit 7fbde8f

Please sign in to comment.