@@ -858,15 +858,17 @@ private void loadSupportedHTMLTags(){
858
858
this .supportedHTMLTags .add ("h1" );
859
859
this .supportedHTMLTags .add ("h2" );
860
860
this .supportedHTMLTags .add ("h3" );
861
+ this .supportedHTMLTags .add ("h4" );
861
862
this .supportedHTMLTags .add ("img" );
862
863
this .supportedHTMLTags .add ("a" );
863
864
}
864
865
865
866
private void processHTMLElement (PDPageContentStream cb , PDRectangle htmlRectangle , SpaceHandler spaceHandler , Element blockElement ) throws Exception {
867
+ this .fontBold = false ;
866
868
String tagName = blockElement .normalName ();
867
869
PDFont htmlFont = PDType1Font .TIMES_ROMAN ;
868
870
869
- if (tagName .equals ("div" )) {
871
+ if (tagName .equals ("div" ) || tagName . equals ( "span" ) ) {
870
872
for (Node child : blockElement .childNodes ())
871
873
if (child instanceof Element )
872
874
processHTMLElement (cb , htmlRectangle , spaceHandler , (Element ) child );
@@ -886,7 +888,6 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
886
888
887
889
float fontSize = 16f ; // Default font size for the HTML <p> tag
888
890
cb .setFont (htmlFont , 16f );
889
- this .fontBold = false ;
890
891
if (tagName .equals ("h1" )){
891
892
cb .setFont (htmlFont , 32f );
892
893
fontSize = 32f ;
@@ -904,6 +905,11 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
904
905
fontSize = 16.5f ;
905
906
tagName = "h" ;
906
907
}
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
+
907
913
if (tagName .equals ("h" )){
908
914
this .fontBold = true ;
909
915
float lines = renderHTMLContent (cb , blockElement .text (), fontSize , llx , lly , urx , spaceHandler .getCurrentYPosition ());
@@ -930,6 +936,9 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
930
936
cb .setStrokingColor (new Color (0 , 0 , 0 ));
931
937
} else if (tagName .equals ("img" )){
932
938
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
+
933
942
PDImageXObject image ;
934
943
935
944
try {
@@ -952,8 +961,10 @@ private void processHTMLElement(PDPageContentStream cb, PDRectangle htmlRectangl
952
961
URL url = new java .net .URL (bitmap );
953
962
image = PDImageXObject .createFromByteArray (document , IOUtils .toByteArray (url .openStream ()),bitmap );
954
963
}
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 );
957
968
}
958
969
959
970
float availableSpace = spaceHandler .getCurrentYPosition () - lly ;
0 commit comments