java - 嵌入式图像在 Flying Saucer 中的 pdf 页面之间被破坏

标签 java pdf pdf-generation itext flying-saucer

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

img {page-break-inside: avoid;}

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

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

  • 图像被指定为 block 元素
  • 以前的图像是或不是 block 元素
  • 分割前有大图

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

我将这种方法与 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;

区别在于我添加了这个 block :

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 仅适用于 block-level elements但是 img 是一个内联 block 元素。尝试使用 img {display: block; page-break-inside: avoid; 看看它是否有效。

关于java - 嵌入式图像在 Flying Saucer 中的 pdf 页面之间被破坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28882592/

相关文章:

java - 在 Google App Engine 上安排备份

c# - NReco PDF 在 Azure 上有黑色方 block

java - 测试使用其他测试的内部 ContextConfiguration

java - java.util.HashMap 中的无限循环

javascript - 用 puppeteer 创建的 PDF 不显示 Font Awesome 图标

pdf-generation - 文件保存在 wkhtmltopdf 中的位置

php - 在 Fancybox 或其他灯箱中显示 PDF

html - 使用iText将HTML转换为PDF

java - Visual Studio 2013 Express Edition,附加到进程(Java JNI 应用程序)似乎不起作用

objective-c - 使用 CGPDFScanner 仅从 PDF 文件中提取文本