打印时在多个页面上强制中断<div>

时间:2018-09-05 20:58:12

标签: html css printing

我正在编写一个JSP应用程序,专门用于生成EDI标准文档。现在,我在格式化输出时遇到问题。

以下JSP代码段将生成有问题的PDF:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title></title>
    <style>
    @page {
        size: 7in 9.25in;
        margin: 0.25in 0.25in 0.25in 0.25in;
    }

    p:first-child {
        margin-top: 0;
    }

    .main-container {
        display: block;
        margin: 0 auto;
    }

    .main-container .row .left {
        display: inline-block;
        width: 29%;
        text-align: right;
        vertical-align: top;
        padding: 0.1em 0.2em;
        font-weight: bold;
    }

    .main-container .row .right {
        display: inline-block;
        width: 69%;
        text-align: left;
        vertical-align: top;
        padding: 0.1em 0.2em;
    }
    </style>
</head>
<body>
    <div class="main-container">
        <div class="row">
            <div class="left">
                Label1:
            </div><div class="right">
                <% for(int i = 0; i < 10; i++) { %>
                    <p>First 10 lines will fit on the first page.</p>
                <% } %>
            </div>
        </div>
        <div class="row">
            <div class="left">
                Label2:
            </div><div class="right">
                <% for(int i = 0; i < 24; i++) { %>
                    <p><%= i %>Test line goes here. Let's make a 1000.</p>
                <% } %>
            </div>
        </div>
    </div>
</body>
</html>

问题是,当我在Chrome中打开JSP并点击“打印”时,打印预览在首页上显示了第一行<div class="row">,列出了10行,但是第二行<div class="row">却出现在了整个页面都适合的下一页。

我希望文本仅在上一个<div>停止的地方继续。我不希望下一个<div>适合下一页。本质上,我希望浏览器停止避免分页。

到目前为止,我已经尝试了多种CSS属性,即:page-break-before / after / inside设置为auto/avoid/always/etc的多种组合。不幸的是,这些解决方案并未奏效。我尝试使用flyingsaucer进行渲染,但那里也找不到任何解决方案。我真的不想非要手动计算距离,因为这超出了原本应该是快速而肮脏的工具的范围。

我一直在尝试找到解决方案,但是由于最常见的情况是完全按照我想避免的方式进行操作,因此很难找出答案(如果有的话)。也许你们当中的一个人知道实现这一目标的可靠方法?

0 个答案:

没有答案