【问题标题】:Prevent body overlapping footer in mPDF防止 mPDF 中的正文重叠页脚
【发布时间】:2012-06-11 12:22:23
【问题描述】:

我正在使用 mPDF 库生成 PDF,并且我的页眉和页脚的大小取决于几个参数。

静态解决方案是设置页脚边距,这将解决重叠问题 - 但由于页脚大小可能不同,这不是我满意的解决方案。有没有办法获取页脚尺寸并相应地应用边距?

【问题讨论】:

  • 我的ATM也遇到同样的问题,如果您找到了解决办法,能否更新您的问题?
  • 我在下面更新了我的答案。希望它会帮助你。必须为 margin_header/footer 设置文档边距。您可能还需要在配置文件中禁用 setAutoTopMargin 设置。

标签: php mpdf


【解决方案1】:

问题在于 mpdf 的文档。我认为 margin_footer 和 margin_header 是文档正文和这些之间的边距。相反,margin_footer 和 margin_header 是文档的边距,就像人们认为的 margin_top 和 margin_bottom 一样。

因此,更改下边距和上边距将决定文档正文的开始位置。更改页眉/页脚边距将决定打印边距。

希望对你有帮助!

更新答案

mPDF 文档对于构造函数调用有点偏离,我猜。 margin_top/bottom 参数实际上是内容边距,不适用于 margin_header/footer 参数。 (如果我没记错的话)。 margin_top/bottom 是文档顶部的绝对边距,应该包括页眉/页脚的高度。

这是处理边距的正确方法:

/**
 * Create a new PDF document
 *
 * @param string $mode
 * @param string $format
 * @param int $font_size
 * @param string $font
 * @param int $margin_left
 * @param int $margin_right
 * @param int $margin_top (Margin between content and header, not to be mixed with margin_header - which is document margin)
 * @param int $margin_bottom (Margin between content and footer, not to be mixed with margin_footer - which is document margin)
 * @param int $margin_header
 * @param int $margin_footer
 * @param string $orientation (P, L)
 */
new mPDF($mode, $format, $font_size, $font, $margin_left, $margin_right, $margin_top, $margin_bottom, $margin_header, $margin_footer, $orientation);

【讨论】:

  • 感谢更新,“实验设置”$mpdf->setAutoTopMargin = 'stretch' and $mpdf->setAutoBottomMargin = 'stretch' 设置将确保页眉和页脚永远不会与主体重叠,而无需手动计算页眉和页脚边距。在处理用户创建的动态页眉和页脚时非常有用
  • 哇!不知道这是可能的。感谢您提供这些信息,它很快就会派上用场。
  • 我试过 $mpdf->setAutoBottomMargin = 'stretch' 没有成功,请问如何使用它?
  • $mpdf->setAutoBottomMargin = 'stretch';为我们工作
【解决方案2】:
$mpdf->setAutoBottomMargin = 'stretch';

为我工作。我所要做的就是确保在页脚之前包含该选项。

【讨论】:

  • 直到我将它设置在页脚之前,它对我也不起作用。
  • 你可以使用->setAutoTopMargin()作为标题。
  • 它的工作..我正在使用<htmlpagefooter> 的页脚。 $mpdf->setAutoBottomMargin = 'stretch'; 它解决了重叠问题。
【解决方案3】:

它对我不起作用,但我设法找到了解决这个问题的方法。我所要做的就是在任何内容之前设置页脚。这是因为页脚的高度计算是在任何内容处理之前完成的。但是如果你稍后用SetHTMLFooter添加页脚,mPDF不会重新计算页脚的高度,所以它会是0。这样就没有分页符,内容会与正文重叠。所以最终的解决方案是:

  1. 使用'setAutoBottomMargin' => 'stretch' 创建 mPDF 对象
  2. 添加标题
  3. 添加页脚
  4. 添加内容

希望对你有帮助。

【讨论】:

  • 爱你的朋友,解决了我的问题
【解决方案4】:
'setAutoBottomMargin' => 'stretch' 

这个属性对我有用。

这是我的代码

$mpdf = new \Mpdf\Mpdf([
  'setAutoBottomMargin' => 'stretch'
]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多