A4 页面像 html 中的布局

新手上路,请多包涵

我正在开发基于 web 的小型应用程序,用户可以看到 2-3 页长的报告,可以打印为 PDF。我在 stackoverflow /internet 上查看了不同的解决方案,发现了一些打印方面的工作解决方案(内容打印有额外的边距,但我需要解决这个问题)我目前的问题是我无法在浏览器中显示 html 内容页面布局。我能够显示 A4 尺寸的第一页,但一旦内容超出一页,它就好像打印在页面外一样,您可以查看下面的图片

  1. 页面在浏览器中的显示方式 在此处输入图像描述

  2. 打印预览的样子 在此处输入图像描述

这是CSS

 .A4 {
   background: white;
   width: 21cm;
   height: 29.7cm;
   display: block;
   margin: 0 auto;
   padding: 10px 25px;
   margin-bottom: 0.5cm;
   box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
}

@media print {
.page-break { display: block; page-break-before: always; }
size: A4 portrait;
}

@media print {
 .noprint {display:none;}
 .enable-print { display: block; }
}

我正在尝试解决以下问题,

  1. 如果所有报告都以类似布局的页面显示(另外,如果我可以水平分页而不是长垂直页面显示页面),我会很高兴
  2. 打印时没有填充问题,所见即所得!

原文由 Ninad Mhatre 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 519
2 个回答

你的第二个问题:

您必须将主体边距和填充设置为零。您还需要从 A4 类中删除框阴影、边距、宽度和高度,以便打印多页。

 .A4 {
  background: white;
  width: 21cm;
  height: 29.7cm;
  display: block;
  margin: 0 auto;
  padding: 10px 25px;
  margin-bottom: 0.5cm;
  box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);
  overflow-y: scroll;
  box-sizing: border-box;
}

@media print {
  .page-break {
    display: block;
    page-break-before: always;
  }

  size: A4 portrait;
}

@media print {
  body {
    margin: 0;
    padding: 0;
  }

  .A4 {
    box-shadow: none;
    margin: 0;
    width: auto;
    height: auto;
  }

  .noprint {
    display: none;
  }

  .enable-print {
    display: block;
  }
}

你的第一个问题:

您可以尝试通过计算 scrollheight 来创建分页功能,并不断从页面中删除元素,直到 scollheight 小于页面本身。

示例: https ://jsfiddle.net/tk8rwnav/31/

 var max_pages = 100;
var page_count = 0;

function snipMe() {
  page_count++;
  if (page_count > max_pages) {
    return;
  }
  var long = $(this)[0].scrollHeight - Math.ceil($(this).innerHeight());
  var children = $(this).children().toArray();
  var removed = [];
  while (long > 0 && children.length > 0) {
    var child = children.pop();
    $(child).detach();
    removed.unshift(child);
    long = $(this)[0].scrollHeight - Math.ceil($(this).innerHeight());
  }
  if (removed.length > 0) {
    var a4 = $('<div class="A4"></div>');
    a4.append(removed);
    $(this).after(a4);
    snipMe.call(a4[0]);
  }
}

$(document).ready(function() {
  $('.A4').each(function() {
    snipMe.call(this);
  });
});

这个例子在每个元素上都中断。段落不会断言,但你可以实现这个,但这会很快变得复杂。

原文由 Simon Backx 发布,翻译遵循 CC BY-SA 3.0 许可协议

下面是 snipMe() 函数的修订版本,以确保第 2-n 页中的元素按原始顺序排列。我还添加了评论。

    function snipMe() {
      page_count++;
      if (page_count > max_pages) {
        return;
      }
      var long = $(this)[0].scrollHeight - Math.ceil($(this).innerHeight());
      var children = $(this).children().toArray(); // Save elements in this page to children[] array
      var removed = [];
      // Loop while this page is longer than an A4 page
      while (long > 0 && children.length > 0) {
        var child = children.pop(); // Remove last array element from the children[] array
        $(child).detach();  // JQuery Method detach() removes the "child" element from DOM for the current page
        removed.push(child);  // Add element that was removed to the end of "removed" array
        long = $(this)[0].scrollHeight - Math.ceil($(this).innerHeight()); // Compute current size of the page
      }
      // If elements were removed from the page
      if (removed.length > 0) {
        var rev_removed = removed.reverse();  // Reverse the order of the removed array
        var a4 = $('<div class="A4"></div>'); // Start a new page
        a4.append(rev_removed); // Add elements removed from last page to the new page
        $(this).after(a4); // Add the new page to the document
        snipMe.call(a4[0]); // Recursively call myself to adjust the remainder of the pages
      }
    }

原文由 Mike Meinz 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏