Search code examples
javahtmlcssitextpdfhtml

DIV height doesn't working in pdfhtm iText?


I'm trying to put the image inside a Div. However, the height attribute doesn't get applied inside the generated PDF.

I wish to restrict the image height according to the DIV height. If the image is of smaller dimensions then the image shouldn't stretch, keeping the image dimension as it is.

HTML:

   <div style="height: 396.0pt !important; border: 1px solid red;"> 
   <img border="0" src="C:/naveed personal/eclipse-jee-oxygen-M2-win32-x86_64/eclipse/Docs_a0Lg000000XzFhgEAF_4/015g0000001O9Qf.JPG"> 
  </div> 

PDF


Solution

  • I think you might want to consider setting height to percentage of div. Here's an example of how you would do that.

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    html, body {
      height: 100%;
    }
    
    img.one {
      height: auto;
      width: auto;
    }
    
    img.two {
      height: 50%;
      width: 50%;
    }
    </style>
    </head>
    <body>
    
    <h2>Set the height and width in %</h2>
    <p>Resize the browser window to see the effect.</p>
    
    <p>Original image:</p>
    <img class="one" src="ocean.jpg" width="300" height="300"><br>
    
    <p>Sized image (in %):</p>
    <img class="two" src="ocean.jpg" width="300" height="300">
    
    </body>
    </html>