Skip to content

Commit c6114c8

Browse files
committed
Allow overriding static content base url
- Added property that allows overriding staticContentBase path from image resources. This allows using a local path instead of an absolute image url, which has to be downloaded in order to be embedded in a report. - Added more logging information when the rendering of the image fails. Issue: 208239
1 parent 2880e5d commit c6114c8

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

java/src/main/java/com/genexus/reports/GXReportPDFCommons.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public abstract class GXReportPDFCommons implements IReportHandler{
8484
protected static String defaultRelativePrepend = null;
8585
protected static String defaultRelativePrependINI = null;
8686
protected static String webAppDir = null;
87+
protected static String staticContentBaseOverride = null;
8788
private static String predefinedSearchPath = "";
8889
protected float leftMargin;
8990
protected float topMargin;
@@ -334,6 +335,9 @@ protected void loadProps() {
334335
props.setupGeneralProperty(Const.STYLE_LONG_DASHED, Const.DEFAULT_STYLE_LONG_DASHED);
335336
props.setupGeneralProperty(Const.STYLE_LONG_DOT_DASHED, Const.DEFAULT_STYLE_LONG_DOT_DASHED);
336337

338+
props.setupGeneralProperty("StaticContentBaseOverride", "");
339+
staticContentBaseOverride = props.getGeneralProperty("StaticContentBaseOverride", "");
340+
337341
loadSubstituteTable(); // Cargo la tabla de substitutos de fonts
338342

339343
Utilities.addPredefinedSearchPaths(new String[]{System.getProperty("java.awt.fonts", "c:\\windows\\fonts"),

java/src/main/java/com/genexus/reports/PDFReportItext2.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,12 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
364364
try {
365365
com.lowagie.text.Image image;
366366
try {
367+
if (!staticContentBaseOverride.isEmpty() && bitmap.startsWith(httpContext.getStaticContentBase())) {
368+
String bitmapOverride = staticContentBaseOverride + bitmap.substring(httpContext.getStaticContentBase().length());
369+
log.debug("GxDrawBitMap -> Overriding '" + bitmap + "' to '" + bitmapOverride + "'");
370+
bitmap = bitmapOverride;
371+
}
372+
367373
if (documentImages != null && documentImages.containsKey(bitmap)) {
368374
image = documentImages.get(bitmap);
369375
}
@@ -400,6 +406,10 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
400406
java.net.URL url= new java.net.URL(bitmap);
401407
image = com.lowagie.text.Image.getInstance(url);
402408
}
409+
catch(Exception ex) {
410+
log.debug("GxDrawBitMap -> '" + bitmap + "' " + ex.getMessage());
411+
throw ex;
412+
}
403413

404414
if (documentImages == null) {
405415
documentImages = new ConcurrentHashMap<>();
@@ -438,7 +448,9 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
438448
PdfContentByte cb = writer.getDirectContent();
439449
cb.addImage(image);
440450
}
441-
451+
else {
452+
log.debug("GxDrawBitMap -> '" + bitmap + "' not found");
453+
}
442454
}
443455
catch(DocumentException de) {
444456
log.error("GxDrawBitMap failed:", de);

java/src/main/java/com/genexus/reports/PDFReportItext8.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,12 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
395395
try {
396396
ImageData imageData;
397397
try {
398+
if (!staticContentBaseOverride.isEmpty() && bitmap.startsWith(httpContext.getStaticContentBase())) {
399+
String bitmapOverride = staticContentBaseOverride + bitmap.substring(httpContext.getStaticContentBase().length());
400+
log.debug("GxDrawBitMap -> Overriding '" + bitmap + "' to '" + bitmapOverride + "'");
401+
bitmap = bitmapOverride;
402+
}
403+
398404
if (documentImages != null && documentImages.containsKey(bitmap)) {
399405
imageData = ImageDataFactory.create(bitmap);
400406
}
@@ -429,6 +435,10 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
429435
URL url= new java.net.URL(bitmap);
430436
imageData = ImageDataFactory.create(url);
431437
}
438+
catch(Exception ex) {
439+
log.debug("GxDrawBitMap -> '" + bitmap + "' " + ex.getMessage());
440+
throw ex;
441+
}
432442

433443
if (documentImages == null) {
434444
documentImages = new ConcurrentHashMap<>();
@@ -451,6 +461,9 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
451461
image.scaleToFit(rightAux - leftAux , bottomAux - topAux);
452462
document.add(image);
453463
}
464+
else {
465+
log.debug("GxDrawBitMap -> '" + bitmap + "' not found");
466+
}
454467
}
455468
catch(IOException ioe) {
456469
log.error("GxDrawBitMap failed:", ioe);

java/src/main/java/com/genexus/reports/PDFReportPDFBox.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
346346
try (PDPageContentStream cb = currentPageContentStream){
347347
PDImageXObject image;
348348
try {
349+
if (!staticContentBaseOverride.isEmpty() && bitmap.startsWith(httpContext.getStaticContentBase())) {
350+
String bitmapOverride = staticContentBaseOverride + bitmap.substring(httpContext.getStaticContentBase().length());
351+
log.debug("GxDrawBitMap -> Overriding '" + bitmap + "' to '" + bitmapOverride + "'");
352+
bitmap = bitmapOverride;
353+
}
354+
349355
if (documentImages != null && documentImages.containsKey(bitmap)) {
350356
image = documentImages.get(bitmap);
351357
}
@@ -381,6 +387,10 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
381387
URL url= new java.net.URL(bitmap);
382388
image = PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(url.openStream()),bitmap);
383389
}
390+
catch(Exception ex) {
391+
log.debug("GxDrawBitMap -> '" + bitmap + "' " + ex.getMessage());
392+
throw ex;
393+
}
384394

385395
if (documentImages == null) {
386396
documentImages = new ConcurrentHashMap<>();
@@ -403,6 +413,9 @@ public void GxDrawBitMap(String bitmap, int left, int top, int right, int bottom
403413
else
404414
cb.drawImage(image, x, y, (rightAux - leftAux) * aspectRatio, (bottomAux - topAux) * aspectRatio);
405415
}
416+
else {
417+
log.debug("GxDrawBitMap -> '" + bitmap + "' not found");
418+
}
406419
}
407420
catch(IOException ioe) {
408421
log.error("GxDrawBitMap failed:", ioe);

0 commit comments

Comments
 (0)