javascript - 打印 : How to stick footer on every page to the bottom?

标签 javascript node.js css pdf electron

我正在尝试将生成的 HTML 文档打印为 PDF。文档本身可以容纳多页。每个页面都是这样构建的:

<!-- Header -->

<!-- Content -->

<!-- Footer -->

这三个在每个页面上看起来都很好。唯一的问题是页脚不会粘在底部...页脚将始终 catch 页面的最后一个元素。只要页面填充了足够的内容,页脚就会像您期望的那样位于底部。

这是我的 CSS:

.docFooter{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    width: 100%;
    position: absolute;
    bottom: 0;
    padding-right: 2cm;
    padding-bottom: 1cm;
}

我还尝试生成一个单独的 CSS,如下所示:

@media print{
    .docFooter{
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-pack: justify;
        -ms-flex-pack: justify;
        justify-content: space-between;
        width: 100%;
        position: absolute;
        bottom: 0;
        padding-right: 2cm;
        padding-bottom: 1cm;
    }
}

当然,我使用的是这样的 CSS:

<link rel="stylesheet" href="./main.min.css" media="all">

旁注:

  • 我只需要 Chrome 支持,因为我正在使用 Electron 框架进行开发
  • 我正在使用这个:https://github.com/delight-im/HTML-Sheets-of-Paper CSS 文件使页面可见,就像它们将被打印给用户一样。
  • 逻辑是使用 Node.js 7.5 构建的

我做了一些研究并尝试了这些问题的答案,但没有成功:

那么有没有可能用纯 CSS 来实现这一点?如果没有,我还会构建一些逻辑来填充页脚上方的所有空白空间,直到页面达到其最大大小。

最佳答案

好吧,由于某些奇怪的原因,解决方案非常简单。 不过我已经改变了我的CSS:

.docFooter{
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    width: 100%;
    position: absolute;
    bottom: 0;
    padding-right: 2cm;
    padding-bottom: 1cm;
}

对此:

display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: justify;
    -ms-flex-pack: justify;
    justify-content: space-between;
    width: 100%;
    position: absolute;
    top: 27.7cm !important;
    padding-right: 2cm !important;

因为我知道 A4 页面不会超过 29.7 厘米,所以很容易将元素设置到底部,同时使其从顶部绝对定位 top: 27.7cm

关于javascript - 打印 : How to stick footer on every page to the bottom?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505164/

相关文章:

javascript - 如何告诉javascript减少50px

javascript - Z 索引不会很好玩

node.js - 如何在 Mongoose 中创建和使用枚举

javascript - MongoClient 连接中的语法错误

javascript - 强制浏览器在更改 CSS 时触发重排

javascript - 对服务发出 http 请求,在 Controller 中获取响应

javascript - 从左到右然后向下动画图像

javascript - 无法使用 dynamodb-data-mapper-js 通过 LSI 创建 DynamoDB 表

javascript - 如何使 JQuery 事件适用于 2 个类?

css - firefox 中的按钮标签与 anchor 标签,文本问题