如何动态检查 PDFMake 中的剩余页面高度?

Pet*_*ukh 5 pdfmake

有没有办法动态检查 PDFMake 中的剩余页面高度?动态创建页面时,我希望能够检查剩余的可用页面高度以将其与元素高度进行比较,以便无法剪切页面上的最后一个元素(例如图像或长文本区域内容)而是转移到另一个页面反而。不知道如何动态地做到这一点。

Pet*_*ukh 6

感谢大家。我最终使用了 pageBreakBefore 函数和用作标记的标题级别,并找到了一个 pdfmake 版本,它允许我们查看节点是否为图像,从而计算元素的高度。这是它在我的代码中的样子。在那里我还有一个页脚,必须在我的计算中考虑它,以便内容不应该继续:

pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
          var pageInnerHeight = currentNode.startPosition.pageInnerHeight;
          var top = (currentNode.startPosition.top) ? currentNode.startPosition.top : 0;
          var footerHeight = 30;
          var nodeHeight = 0;
          if (followingNodesOnPage && followingNodesOnPage.length) {
            nodeHeight = followingNodesOnPage[0].startPosition.top - top;
          }

          if (currentNode.headlineLevel === 'footer') return false;

          return (currentNode.image && (top + nodeHeight + footerHeight > pageInnerHeight))
              || (currentNode.headlineLevel === 'longField' && (top + nodeHeight + footerHeight > pageInnerHeight))
              || currentNode.startPosition.verticalRatio >= 0.95;
        }
Run Code Online (Sandbox Code Playgroud)


Fou*_*898 5

嗯,我可能来得有点晚了。但在0.1.17版本中,他们引入了pageBreakBefore函数。

Github 上的发行说明

您现在可以指定 pageBreakBefore 函数,该函数可以确定是否应在分页符之前插入分页符。要实施“无孤儿”规则,可以像这样:

var dd = {
  content: [
    {text: '1 Headline', headlineLevel: 1},
    'Some long text of variable length ...',
    {text: '2 Headline', headlineLevel: 1},
    'Some long text of variable length ...',
    {text: '3 Headline', headlineLevel: 1},
    'Some long text of variable length ...',
  ],
  pageBreakBefore: function(currentNode, followingNodesOnPage, nodesOnNextPage, previousNodesOnPage) {
    return currentNode.headlineLevel === 1 && followingNodesOnPage.length === 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

如果 pageBreakBefore 返回 true,则将在 currentNode 之前添加分页符。当前节点附加了以下信息:

{
 id: '<as specified in doc definition>', 
 headlineLevel: '<as specified in doc definition>',
 text: '<as specified in doc definition>', 
 ul: '<as specified in doc definition>', 
 ol: '<as specified in doc definition>', 
 table: '<as specified in doc definition>', 
 image: '<as specified in doc definition>', 
 qr: '<as specified in doc definition>', 
 canvas: '<as specified in doc definition>', 
 columns: '<as specified in doc definition>', 
 style: '<as specified in doc definition>', 
 pageOrientation '<as specified in doc definition>',
 pageNumbers: [2, 3], // The pages this element is visible on (e.g. multi-line text could be on more than one page)
 pages: 6, // the total number of pages of this document
 stack: false, // if this is an element which encapsulates multiple sub-objects
 startPosition: {
   pageNumber: 2, // the page this node starts on
   pageOrientation: 'landscape', // the orientation of this page
   left: 60, // the left position
   right: 60, // the right position
   verticalRatio: 0.2, // the ratio of space used vertically in this document (excluding margins)
   horizontalRatio: 0.0  // the ratio of space used horizontally in this document (excluding margins)
 }
}
Run Code Online (Sandbox Code Playgroud)


Flo*_*kap 0

我最近在开发大会上与一位同事进行了交谈。他们面临着同样的问题。如果你真的需要知道,据我所知有两种可能性:

1)测试渲染页面数据并检查输出是否超过一页。这是愚蠢的,但你不知道内部原理。

2)在生成pdf之前,请自行在pdfmake中完成计算。不幸的是,关于如何,您需要研究 pdfmake 生成代码本身。

如果有更优雅的解决方案,我非常想了解自己!