使用可变大小的可变数量的部分动态地将分页符添加到html

时间:2016-12-21 19:05:08

标签: php jquery html css page-break

我正在尝试解决当前的问题,我可以使用一些帮助: 从使用php的数据库中,页面必须加载X个部分,每个部分都有一些内容在顶部,一个表和底部的一些内容,表可以有0到任意数量的行,具体取决于之前的内容数据库。 除了表格外,其余内容的大小不变。

由于表格的原因,我事先无法知道每个部分的大小,通常至少有30个部分。

此页面必须是打印友好的,除了分页符之外,一切都已经正常工作。

我需要找到一种方法来动态添加分页符,以使各部分适合页面而不会在页面之间切换。

代码大约是400行,所以在这里发布它是不切实际的,但是部分结构是这样的:

<section class="row">
<section>
	<section class="col-md-2"></section>
	<section class="col-md-8 printcenter">
		<p class="docenteheader" >
			<span class="tabspaceright">NAME: <strong>name</strong></span>
			<span class="tabspaceleft">ID: <strong>number</strong></span>
		</p>
		<table>
		  <tr>
		    <th><strong>1:</strong></th>
			<th><strong>2:</strong></th>
			<th><strong>3:</strong></th>
			<th><strong>4:</strong></th>
		  </tr>
		  <tr>
			<td>1</td>
			<td>2</td>
			<td>3</td>
			<td>4</td>
		  </tr>
		</table>
		<p><strong>Something1:</strong> Something</p>
		<p>
			<span><strong>Something2: number</strong></span>
			<span ><strong>Something3: number</strong></span>
		</p>
		<section class="rel_endline"></section>
	</section>
	<section class="col-md-2"></section>
</section>                       
</section>

我删除了css类和原始名称以及PHP代码以便于查看

2 个答案:

答案 0 :(得分:1)

以下是示例PHP代码,如果有任何不清楚的地方,请告诉我。我试图使用自解释变量名。

<?php

    $PAGEBREAK_LENGTH = 1000; // Add a page break every 1000 characters.
    $lengthTracker = '';

    while ($row = odbc_fetch_array($res)){
        $lengthTracker .= $row['article']; 
        $lengthSize = strlen($lengthTracker);

        if ($lengthSize > $PAGEBREAK_LENGTH) {
            echo '<div class="page-break">This is a page break</div>';
            $lengthTracker = ''; // Reset length tracker since we just added a break
        }

        echo '<div class="article">';
        echo $row['article'];
        echo '</div>';
    }

?>

如果您在回复文章之前添加if ($lengthSize > $PAGEBREAK_LENGTH),则会更频繁地包含分页符&#34;&#34;如果您在文章发布后添加if语句,则会减少分页次数(因此您可能会有更大的文本块)。

尝试两种方式来查看您喜欢哪种风格,因为您(大概)可以在文章中包含分页符。

答案 1 :(得分:0)

我发现我的问题不是由错误的分页符引起的,但是我在一个元素中有负边距,这使得整个页面分解工作变得混乱,通过删除它,它正常工作。