DOMPDF如何使PDF高度自动 [英] DOMPDF How to make PDFs height automatic

查看:873
本文介绍了DOMPDF如何使PDF高度自动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我恐怕这个问题没有答案,但是在这里。

I'm afraid that this question does not have an answer, but here it goes.

我想要做的是让人们填写各种信息在我的网站上关于他们自己,并回复所有的HTML模板文件(稍后将转换为PDF)。由于所有用户都有不同数量的条目和条目长度,因此无法预测文档的最终结果高度。

What I am trying to do is get people to fill in various information about them selves in my website, and echo all of it in a HTML template file (which gets converted into PDF later). Since all users have variable amounts of entries and entry lengths, it's impossible to predict the end result height of the document.

换句话说:用户给出的信息越多他们自己,文档的大小越高。并且没有办法根据HTML模板的最终结果设置DOMPDF生成PDF高度。

In other words: the more info a user gives about them selves, the higher in size the document gets. And there is no way to set DOMPDF to generate PDF heights according to the end result of the HTML template.

我在我的鼻子下面缺少一个解决方案?

Am I missing a solution right under my nose here?

推荐答案

没有内置的方法来做到这一点,但这并不意味着它是不可能的。你恰巧是第二个问我这个问题的人,我第一次管理得出一个可以做到的方法。

There is not a built-in way to do this, but that doesn't mean it is not possible. You happen to be the second person to ask this and that first time I did manage come up with a way it can be done.

以下来自原始帖子 dompdf支持小组

首先,您需要对文档的样式表进行小的修改。您要将顶部和底部页边距设置为零,以便它们不会增加高度计算。所以在你的第一遍HTML添加以下附加样式声明:

First, you need to make a minor modification to the stylesheet of your document. You want to set the top and bottom page margins to zero so they don't add in to the height calculation. So in your first-pass HTML add the following additional style declaration:

@page { margin-top: 0px; margin-bottom: 0px; }

接下来,您必须确定最佳的第一遍文档大小。我使用一个简单的文档,所以我去了一个简单的8厘米x 8厘米的页面大小。您可能需要使用更大的高度,以避免与分页相关的任何错误。 8cm约为226.77pt。所以我设置了第一次使用一个用适当维度定义的文档:

Next, you have to determine what the best first-pass document size will be. I was using a simple document, so I went with a simple 8cm x 8cm page size. You'll probably want to use a larger height to avoid any bugs related to paging. 8cm is roughly 226.77pt. So I set up the first pass to use a document defined with the appropriate dimensions:

$dompdf = new DOMPDF( );
$dompdf->set_paper( array( 0 , 0 , 226.77 , 226.77 ) );
$dompdf->load_html( $first_pass_html );
$dompdf->render( );

然后我们得到这个pass的页面数,并取消设置$ dompdf变量,这样我们可以我们的第二个传递:

Then we get the number of pages that resulted from this pass and unset the $dompdf variable so we can do our second pass:

$page_count = $dompdf->get_canvas( )->get_page_number( );
unset( $dompdf );

最后,使用从第一页中使用的页面高度计算的页面高度再次渲染文档通过乘以生成的页数(加上一些额外的填充以容纳边距)。

Finally, render the document a second time using a page height calculated from the page height used in the first pass multiplied by the number of pages generated (plus a little extra padding to accommodate margins).

$dompdf = new DOMPDF( );
$dompdf->set_paper( array( 0 , 0 , 226.77 , 226.77 * $page_count + 20 ) );
$dompdf->load_html( $original_html );
$dompdf->render( );
$dompdf->stream( 'sample.pdf' , array( 'Attachment' => 0 ) );

这篇关于DOMPDF如何使PDF高度自动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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