FPDF-放置前要确定MultiCell的高度吗? [英] FPDF - Determining height of MultiCell before placing?

查看:119
本文介绍了FPDF-放置前要确定MultiCell的高度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本问题:是否可以在将MultiCell放入文档中之前确定其高度?

The basic question: Is it possible to determine the height of a MultiCell before placing it in the document?

原因:我的任务是创建表单的PDF版本.这种形式允许文本输入,并具有可变的长度.一个人我什么都不能输入,另一个人可以写几段. 存在的力量"不希望此文本在页面之间中断.

The reason: I've been tasked with creating a PDF version of a form. This form allows text input, with a resulting variable length. One person my not enter anything, another person may write a few paragraphs. "The Powers That Be" do not want this text breaking between pages.

当前,放置每个块后,我检查页面上的位置,如果快到末尾,则创建一个新页面.

Currently, after placing each block, I check the position on the page, and if I'm near the end, I create a new page.

if($this->getY() >= 250) {
    $this->AddPage('P');
}

在大多数情况下,这是可行的.但是,有一些偷偷摸摸的冒出来的东西,比如说249,然后有大量的文本.在放置块以查看其是否真正适合页面之前,确定块的高度似乎更有意义.

For the most part, this works. But there are the few sneaky ones that come in at, say 249, and then have boatloads of text. It seems it would make more sense to determine the height of a block before placing it to see if it actually fits on the page.

当然必须有一种方法可以做到这一点,但是谷歌搜索并没有被证明很有帮助.

Surely there must be a way to do this, but Googling around hasn't proven very helpful.

编辑以澄清:提交表单后,正在生成PDF.不是有效的,可编辑的PDF表单...

Edit to clarify: The PDF is being generated after the form is submitted. Not an active, editable PDF form...

推荐答案

已为该问题赢得了风滚草徽章. :(无论如何,如果有人碰到同样的问题,我想我会回答我所做的事情.我怀疑这是最有效的方法,但是它完成了工作.

Earned the tumbleweed badge for this question. :( Anyway, in the off chance someone has the same issue, I figured I'd answer with what I did. I doubt this is the most efficient method, but it got the job done.

首先,我创建两个FPDF对象;临时的,永远不会得到输出,以及最终用户将看到的最终的完整pdf.

First, I create two FPDF objects; a temporary one that never gets output and the final, complete pdf that the end user will see.

我在两个对象中都得到了当前的Y坐标.然后,我使用Cell()或MultiCell()(对于这种形式,通常是两者的组合)将数据块放入临时对象中,然后检查新的Y坐标.然后,我可以进行数学运算以确定块的高度. -请注意,如果该块跨页中断,则数学运算可能会变得有些时髦.请记住要考虑您的最高和最低利润.

I get my current Y coordinate in both objects. I then place the block of data using Cell() or MultiCell() (for this form, usually a combination of the two) into the temporary object, and then check the new Y coordinate. I can then do the math to determine the height of the block. -Note, the math can get a bit funky if the block breaks across pages. Remember to take your top and bottom margins into account.

现在,我知道一个块的高度了,我可以获取目标对象的当前Y坐标,然后从页面的总高度(减去底部边距)中减去它以获得可用空间.如果该块适合,则将其放置,否则,添加页面并将其放置在顶部.

Now that I know the height of a block, I can take the current Y coordinate of the destination object, subtract it from the total height of a page(minus bottom margin) to get my available space. If the block fits, place it, if not, add a page and place it at the top.

每个方块重复一次.

我唯一需要注意的是,是否有任何单个块的长度大于整个页面的长度,但是采用这种形式,这种情况永远不会发生. (著名的遗言...)

Only caveat I see is if any single block is longer then an entire page, but with this form, that never happens. (famous last words...)

这篇关于FPDF-放置前要确定MultiCell的高度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆