使用Thymeleaf导出PDF,页眉插入图片与内容重叠?

CSS 打印分页功能

需求:打印 在第一页的内容被挤到第二页的时候,又想每一页页头都有相同的样式,使用页眉。

问题:第二页的内容与页眉重叠了?

查各路找出的原因:header 页眉不占空间

解决:不需要写死内容的高度,需要把页眉空间设出来,内容从哪里开始设好

  1. Thymeleaf设置页头、页尾
html 复制代码
<style>    
    @page {
        @top-center {
            content: element(header);
        }
    }

    .header {
        position: running(header);
    }
</style>

在上面的示例中,@page指令用于定义页面布局,@top-center选择器用于指定页眉的位置。content: element(header);表示将名为header的元素内容作为页眉插入到@top-center位置。.header类用于定义页眉的样式,并使用position: running(header);将其与@top-center位置关联起来。

  1. 设置高度
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF with Fixed Header and Footer</title>
<style>
  /* 页面中的内容 */
  .content {
    margin-top: 100px; /* 留出页眉空间 */
    margin-bottom: 100px; /* 留出页脚空间 */
  }

  /* 固定页眉 */
  @page {
    margin-top: 100px; /* 页眉高度 */
    margin-bottom: 100px; /* 页脚高度 */
  }
  .header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100px; /* 页眉高度 */
    background-color: #ccc;
    text-align: center;
    line-height: 100px; /* 垂直居中 */
  }

  /* 固定页脚 */
  .footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100px; /* 页脚高度 */
    background-color: #ccc;
    text-align: center;
    line-height: 100px; /* 垂直居中 */
  }
</style>
</head>
<body>
  <!-- 固定页眉 -->
  <div class="header">
    Header
  </div>

  <!-- 页面中的内容 -->
  <div class="content">
    <h1>Main Content</h1>
    <p>This is the main content of the page.</p>
    <!-- 这里可以放更多内容 -->
  </div>

  <!-- 固定页脚 -->
  <div class="footer">
    Footer
  </div>
</body>
</html>

@media print

@Page

都是专用于页面设计

页码和页面显示只能用JS才能实现?no no no

用纯CSS也能实现

页码实现

html 复制代码
<div class="footer"><span id="pagenumber"></span>
<span id="pagecount"></span></div>


#pagenumber:before {
            content: counter(page);
        }

#pagecount:before {
            content: counter(pages);
}


        @page {
            @bottom-left {
                content: element(footer);
                vertical-align: top;
                padding-top: 0px;
            }

footer {
            display: block;
            margin-top: 0.5cm;
            position: running(footer);
        }

避免分割元素:使用 page-break-inside 属性来避免将元素分割到不同的页面上。 【放在要分割的元素上】

控制分页位置:page-break-after: auto;

html 复制代码
.details-box{
    position: absolute;
    margin-top: 21px;
    padding-top: 1px;
    page-break-after: auto;
}

参考:

复制代码
关于CSS 打印你应该知道的样式配置 - 掘金 (juejin.cn)

有空格就会另起一行

html 复制代码
white-space: nowrap;
white-space:pre-line"
相关推荐
C.果栗子8 小时前
Blob格式的PDF文件调用打印,浏览器文件打印(兼容)
前端·javascript·pdf
Highcharts.js13 小时前
Highcharts常见问题解析(5):如何将多个图表导出到同一张图片或 PDF?
pdf·highcharts
麦烤楽鸡翅17 小时前
pdf(攻防世界)
网络安全·pdf·ctf·misc·杂项·攻防世界·信息竞赛
Less is moree18 小时前
PDF无法打印怎么解决?
pdf
lijun_xiao20091 天前
Python-将身份证正反面图片-生成PDF
pdf
A尘埃1 天前
项目七:PDF智能公式与计算(金融机构信贷报告自动解析与风险评估)
pdf
百事牛科技1 天前
PDF如何设置密码?3种方法保护文件安全
windows·pdf
mysusheng2 天前
2025 批量下载微博内容/图片/视频,导出word和pdf,微博点赞/评论/转发等数据导出excel
pdf·word·excel
lijun_xiao20092 天前
Python-PDF文件生成水印
pdf
zongxingfengyun2 天前
图片转pdf接口
pdf