3

I have some problem with images (all images are embedded in html as base64 strings). I use css

img {page-break-inside: avoid;}

and it helps but not always. In some cases the same image can be processed correctly where in other situation is divided between pages.

It depends form many factors, examples:

  • image is assigned as block element
  • previous images are or are not block elements
  • there is some big image before divided one

I also noticed that if the problem occurred at least once than all images to the end of document can be broken when they don't fit on the page.

I'm using this approach with RepleacedElementFactory for embedded images: http://www.intelligrape.com/blog/using-data-urls-for-embedding-images-in-flying-saucer-generated-pdfs/

the only difference is that i'm changing the size a bit

public ReplacedElement createReplacedElement(LayoutContext c, BlockBox box, UserAgentCallback uac, int cssWidth, int cssHeight) {
 Element e = box.getElement();
 if (e == null) {
     return null;
 }
 String nodeName = e.getNodeName();
 if (nodeName.equals("img")) {
     String attribute = e.getAttribute("src");
     FSImage fsImage;
     try {
         fsImage = buildImage(attribute, uac, cssWidth, cssHeight);
     } catch (BadElementException e1) {
         fsImage = null;
     } catch (IOException e1) {
         fsImage = null;
     }
     if (fsImage != null) {
         if(cssWidth == -1 && cssHeight == -1)
         {
            int factor = _sharedContext.getDotsPerPixel();
            int width = fsImage.getWidth();
            int fWidth =  width * factor;
            fsImage.scale(fWidth, -1);
         }
         if(cssWidth == -1 || cssHeight == -1)
         {
            fsImage.scale(cssWidth, cssHeight);
         }
         return new ITextImageElement(fsImage);
     }
 }
 return null;

}

the difference is I've added this block:

if(cssWidth == -1 && cssHeight == -1)
         {
            int factor = _sharedContext.getDotsPerPixel();
            int width = fsImage.getWidth();
            int fWidth =  width * factor;
            fsImage.scale(fWidth, -1);
         }

to give proper size if it is not given by css. Without it I had the problem with all images be really tinny.

I guess there is some problem with calculating the real size (height) of the picture but I really don't have idea what else can i change to guarantee that images would never break between pages.

Please help.

user3333010
  • 113
  • 2
  • 9
  • 1
    Possible duplicate of [IText 2 + Flying Saucer: how to avoid that the images appears broken between two pdf's pages?](https://stackoverflow.com/questions/9499519/itext-2-flying-saucer-how-to-avoid-that-the-images-appears-broken-between-two) – Pylot Jun 15 '18 at 14:58

1 Answers1

0

page-break-inside only applies to block-level elements but img is an inline-block element. Try using img {display: block; page-break-inside: avoid;} and see if it works.

vlad1918
  • 338
  • 3
  • 15
  • Thanks for suggestion, but I at the beginning display:block was applied to all images and the problem was also visible. During my test I removed display:block and the results were quite ok, at least much better than with display:block case. Unfortunately I need images to be displayed as block in order to give them correct position and align them. – user3333010 Mar 09 '15 at 08:21
  • Because nobody could help me I'm adding html that is causing the problems and pdf with result am getting (there is rendered pdf with images beeing braked down in the middle). [link] https://www.dropbox.com/s/sjl2fmvlbkh9v4e/ExampleDocument.html?dl=0 [link] https://www.dropbox.com/s/1dapzf69l7fupze/outFile1.pdf?dl=0 – user3333010 Apr 10 '15 at 12:10