Skip to content

Commit 9448f61

Browse files
Fixes to heading and image rendering and text positioning
1 parent 7ccfe21 commit 9448f61

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -858,15 +858,17 @@ private void loadSupportedHTMLTags(){
858858
this.supportedHTMLTags.add("h1");
859859
this.supportedHTMLTags.add("h2");
860860
this.supportedHTMLTags.add("h3");
861+
this.supportedHTMLTags.add("h4");
861862
this.supportedHTMLTags.add("img");
862863
this.supportedHTMLTags.add("a");
863864
}
864865

865866
private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangle, SpaceHandler spaceHandler, Element blockElement) throws Exception{
867+
this.fontBold = false;
866868
String tagName = blockElement.normalName();
867869
PDFont htmlFont = PDType1Font.TIMES_ROMAN;
868870

869-
if (tagName.equals("div")) {
871+
if (tagName.equals("div") || tagName.equals("span")) {
870872
for (Node child : blockElement.childNodes())
871873
if (child instanceof Element)
872874
processHTMLElement(cb, htmlRectangle, spaceHandler, (Element) child);
@@ -886,7 +888,6 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
886888

887889
float fontSize = 16f; // Default font size for the HTML <p> tag
888890
cb.setFont(htmlFont, 16f);
889-
this.fontBold = false;
890891
if (tagName.equals("h1")){
891892
cb.setFont(htmlFont, 32f);
892893
fontSize = 32f;
@@ -904,6 +905,11 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
904905
fontSize = 16.5f;
905906
tagName = "h";
906907
}
908+
909+
//fontsize / 2 is subtracted from the current Y position so that the rendered item fits within the specified rectangle in the canvas. This is because
910+
//PDFBox renders text from left to right and bottom to top
911+
spaceHandler.setCurrentYPosition(spaceHandler.getCurrentYPosition() - fontSize / 2);
912+
907913
if (tagName.equals("h")){
908914
this.fontBold = true;
909915
float lines = renderHTMLContent(cb, blockElement.text(), fontSize, llx, lly, urx, spaceHandler.getCurrentYPosition());
@@ -930,6 +936,9 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
930936
cb.setStrokingColor(new Color(0, 0, 0));
931937
} else if (tagName.equals("img")){
932938
String bitmap = blockElement.attr("src");
939+
float height = blockElement.attr("height") != "" ? Float.parseFloat(blockElement.attr("height")) : 0;
940+
float width = blockElement.attr("width") != "" ? Float.parseFloat(blockElement.attr("width")) : 0;
941+
933942
PDImageXObject image;
934943

935944
try {
@@ -952,8 +961,10 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
952961
URL url= new java.net.URL(bitmap);
953962
image = PDImageXObject.createFromByteArray(document, IOUtils.toByteArray(url.openStream()),bitmap);
954963
}
955-
cb.drawImage(image, llx, spaceHandler.getCurrentYPosition() - image.getHeight(), image.getWidth(), image.getHeight());
956-
spaceHandler.setCurrentYPosition(spaceHandler.getCurrentYPosition() - image.getHeight() - 10f);
964+
if (height == 0) height = image.getHeight();
965+
if (width == 0) width = image.getWidth();
966+
cb.drawImage(image, llx, spaceHandler.getCurrentYPosition() - height, width, height);
967+
spaceHandler.setCurrentYPosition(spaceHandler.getCurrentYPosition() - height - 10f);
957968
}
958969

959970
float availableSpace = spaceHandler.getCurrentYPosition() - lly;

0 commit comments

Comments
 (0)