在flying-saucer的pdf页面之间打破了嵌入的图像

我对图像有一些问题(所有图像都嵌入在html中作为base64字符串)。 我用css

img {page-break-inside: avoid;} 

它有帮助但并非总是如此。 在某些情况下,可以正确处理相同的图像,而在其他情况下在页面之间划分。

这取决于许多因素,例如:

  • 图像被指定为块元素
  • 以前的图像是或不是块元素
  • 分开之前有一些大的形象

我还注意到,如果问题至少发生一次,那么当文档不适合页面时,文档末尾的所有图像都会被破坏。

我正在使用这种方法将RepleacedElementFactory用于嵌入式图像: http : //www.intelligrape.com/blog/using-data-urls-for-embedding-images-in-flying-saucer-generated-pdfs/

唯一的区别是我正在改变一些尺寸

 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; 

}

区别是我添加了这个块:

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

如果没有css给出适当的大小。 没有它,我遇到的问题是所有图像都非常小。

我想计算图片的实际尺寸(高度)有一些问题,但我真的不知道我还能改变什么来保证图像永远不会在页面之间断开。

请帮忙。

page-break-inside仅适用于块级元素,但img是一个内联块元素。 尝试使用img {display: block; page-break-inside: avoid;} img {display: block; page-break-inside: avoid;}并查看它是否有效。