Skip to content

Commit e0be99d

Browse files
committed
Support for base64 images (added in itext and fop renderer)
1 parent 7f0374c commit e0be99d

File tree

14 files changed

+76
-15
lines changed

14 files changed

+76
-15
lines changed

fj-doc-base/src/main/java/org/fugerit/java/doc/base/helper/SourceResolverHelper.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,40 @@
55
import org.apache.commons.codec.binary.Base64;
66
import org.fugerit.java.core.io.StreamIO;
77
import org.fugerit.java.core.io.helper.StreamHelper;
8+
import org.fugerit.java.core.lang.helpers.StringUtils;
9+
import org.fugerit.java.doc.base.model.DocImage;
810

911
public class SourceResolverHelper {
1012

1113
public static final String MODE_CLASSLOADER = StreamHelper.PATH_CLASSLOADER;
1214

13-
public static final String MODE_BASE64 = "base64://";
15+
public static String resolveImageToBase64( DocImage img ) throws Exception {
16+
String path = img.getUrl();
17+
String base64 = img.getBase64();
18+
if ( StringUtils.isEmpty( base64 ) && path != null ) {
19+
byte[] data = null;
20+
if ( path.startsWith( StreamHelper.PATH_CLASSLOADER ) ) {
21+
data = StreamIO.readBytes( StreamHelper.resolveStream( path ) );
22+
} else {
23+
URL url = new URL( path );
24+
data = StreamIO.readBytes( url.openConnection().getInputStream() );
25+
}
26+
base64 = Base64.encodeBase64String( data );
27+
} else {
28+
throw new Exception( "Null path and base64 provided!" );
29+
}
30+
return base64;
31+
}
1432

15-
public static byte[] resolveByte( String path ) throws Exception {
33+
public static byte[] resolveImage( DocImage img ) throws Exception {
1634
byte[] data = null;
17-
if ( path != null ) {
35+
String path = img.getUrl();
36+
String base64 = img.getBase64();
37+
if ( StringUtils.isNotEmpty( base64 ) ) {
38+
data = Base64.decodeBase64( base64 );
39+
} else if ( path != null ) {
1840
if ( path.startsWith( StreamHelper.PATH_CLASSLOADER ) ) {
1941
data = StreamIO.readBytes( StreamHelper.resolveStream( path ) );
20-
} else if ( path.startsWith( MODE_BASE64 ) ) {
21-
String base64 = path.substring( MODE_BASE64.length() );
22-
data = Base64.decodeBase64( base64 );
2342
} else {
2443
URL url = new URL( path );
2544
data = StreamIO.readBytes( url.openConnection().getInputStream() );

fj-doc-base/src/main/java/org/fugerit/java/doc/base/model/DocImage.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ The Apache Software Foundation (http://www.apache.org/).
2525
*/
2626
package org.fugerit.java.doc.base.model;
2727

28+
import org.fugerit.java.core.lang.helpers.StringUtils;
29+
import org.fugerit.java.doc.base.helper.SourceResolverHelper;
30+
2831
/**
2932
*
3033
*
@@ -36,6 +39,8 @@ public class DocImage extends DocElement {
3639
private Integer scaling;
3740

3841
private String url;
42+
43+
private String base64;
3944

4045
/**
4146
* @return the url
@@ -64,5 +69,21 @@ public Integer getScaling() {
6469
public void setScaling(Integer scaling) {
6570
this.scaling = scaling;
6671
}
72+
73+
public String getBase64() {
74+
return base64;
75+
}
76+
77+
public void setBase64(String base64) {
78+
this.base64 = base64;
79+
}
80+
81+
public String getResolvedBase64() throws Exception {
82+
String res = this.getBase64();
83+
if ( StringUtils.isEmpty( res ) ) {
84+
res = SourceResolverHelper.resolveImageToBase64( this );
85+
}
86+
return res;
87+
}
6788

6889
}

fj-doc-base/src/main/java/org/fugerit/java/doc/base/xml/DocContentHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ The Apache Software Foundation (http://www.apache.org/).
3131
import java.util.LinkedList;
3232
import java.util.Properties;
3333

34+
import org.fugerit.java.core.lang.helpers.StringUtils;
3435
import org.fugerit.java.doc.base.model.DocBarcode;
3536
import org.fugerit.java.doc.base.model.DocBase;
3637
import org.fugerit.java.doc.base.model.DocBorders;
@@ -286,6 +287,10 @@ public void startElement(String uri, String localName, String qName, Attributes
286287
} else {
287288
docImage.setScaling( null );
288289
}
290+
String base64 = props.getProperty( "base64" );
291+
if ( StringUtils.isNotEmpty( base64 ) ) {
292+
docImage.setBase64( base64 );
293+
}
289294
this.currentElement = docImage;
290295
} else if ( "para".equalsIgnoreCase( qName ) ) {
291296
DocPara docPara = new DocPara();

fj-doc-base/src/main/resources/config/doc-1-0.xsd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @project : org.fugerit.java.doc.base
88
* @creation : 2019-11-06
9-
* @version : 1.0.3 (2019-11-27)
9+
* @version : 1.0.4 (2019-11-29)
1010
*
1111
* XSD for fugerit doc configuration
1212
*/
@@ -140,10 +140,12 @@
140140
cl://pathinjar (Path inside the class loader, safest way to load the image)
141141
http://imageurl (but generating machine should be able to access the url)
142142
file://imagepath (Path should be absolute, but you can use, for instance, free marker templating to make it more generic.)
143+
In case of base64 just provide the tyoe (png,jpg etc.)
143144
</xsd:documentation>
144145
</xsd:annotation>
145146
</xsd:attribute>
146147
<xsd:attribute name='scaling' type='xsd:int' use='optional' />
148+
<xsd:attribute name='base64' type="xsd:string" use='optional' />
147149
</xsd:complexType>
148150
</xsd:element>
149151

fj-doc-mod-fop/src/main/resources/fm_fop/template/macro/doc_element.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@
88
<#elseif elementType = 'DocPara'>
99
<fo:block <@handleStyle styleValue=current.style/> font-size="${defaultFontSize}pt">${current.text}</fo:block>
1010
<#elseif elementType = 'DocTable'>
11-
<@handleTable docTable=current/>
11+
<@handleTable docTable=current/>
12+
<#elseif elementType = 'DocImage'>
13+
<@handleImage docImage=current/>
1214
<#else>
1315
<fo:block space-after="5mm" font-weight="bold" font-size="${defaultFontSize}pt">Element type non implemented yet : ${elementType}</fo:block>
1416
</#if>
1517
</#macro>
1618

19+
<#macro handleImage docImage>
20+
<#if (docImage.scaling)??>
21+
<#assign imageScaling="height='${docImage.scaling}%' content-height='${docImage.scaling}%' content-width='scale-to-fit' scaling='uniform' width='${docImage.scaling}%'"/>
22+
<#else>
23+
<#assign imageScaling=""/>
24+
</#if>
25+
<fo:external-graphic xmlns:fo="http://www.w3.org/1999/XSL/Format"
26+
src="data:image;base64,${docImage.resolvedBase64}" ${imageScaling}/>
27+
</#macro>
28+
1729
<#macro handleTable docTable>
1830
<fo:block font-size="10pt">
1931
<fo:table border-collapse="separate" width="${docTable.width}%" table-layout="fixed">

fj-doc-mod-itext/src/main/java/org/fugerit/java/doc/mod/itext/ITextDocHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ protected static Image createImage( DocImage docImage ) throws Exception {
189189
Image image = null;
190190
String url = docImage.getUrl();
191191
try {
192-
byte[] data = SourceResolverHelper.resolveByte( url );
192+
byte[] data = SourceResolverHelper.resolveImage( docImage );
193193
image = Image.getInstance( data );
194194
if ( docImage.getScaling() != null ) {
195195
image.scalePercent( docImage.getScaling().floatValue() );

fj-doc-sample/src/main/resources/free_marker/test_01.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929

3030
<phrase>Test template page apache free marker</phrase>
3131

32-
<image url="cl://test/img_test.png" scaling="100"/>
32+
<image url="cl://test/img_test_green.png" scaling="100"/>
3333

34-
<image url="base64://${testBase64Img}" scaling="25"/>
34+
<image url="cl://test/img_test_red.png" scaling="50"/>
35+
36+
<image url="png" base64="${testBase64Img}" scaling="25"/>
3537

3638
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
3739
<row>
-864 Bytes
Binary file not shown.
864 Bytes
Loading
865 Bytes
Loading

0 commit comments

Comments
 (0)