这里有一些从我尚未发布的项目中提取出来的技巧,用于生成简历...
div[class*="resume-section-experience-"] {
display: table;
width: 100%
}
div[class*="resume-section-experience-"] > div {
display: table-cell;
overflow: hidden;
}
.resume-section-experience-professional ~ .resume-section-experience-professional > div:before {
content: "";
display: block;
margin-bottom: -3em;
}
.resume-section-experience-volunteer ~ .resume-section-experience-volunteer > div:before {
content: "";
display: block;
margin-bottom: -3em;
}
.resume-headings {
height: 3em;
}
div[class*="resume-section-experience-"] > div > div {
display: inline-block;
width: 100%;
vertical-align: top;
}
@media print {
h2, h3, h4, ul, .professional-experience {
page-break-after: avoid;
}
pre, blockquote, ul, div[class*="-training"], div[class*="-requirements"], div[class*="-experience"], div[class*="-skills"], div[class*="resume-section-experience-"] {
page-break-inside: avoid;
}
}
如果你想要调整目标元素的名称/类名,并且可能需要在段落之间调整间距,但以上代码可以满足相似的要求。
尽管如此,如果你知道段落将在某些时候分页,那么缩小容器的可用高度,以便有空间放置固定定位的页眉和页脚,可能会更好。
如果您编辑问题以包括一些标记(并通知我),我将尝试再次进行编辑,以更好地解决您特定的用例。
关于上述CSS的另一个注意事项; 对于我的用例,我发现重复页眉/页脚数据,然后在不在页面顶部/底部时选择性地隐藏它们更容易在Web和打印视图之间进行格式化。
@dhiraj
...主要问题是如何生成页码
如果页码是主要问题,那么应该可以利用 CSS 的计数器技巧来实现 (删除了“display: table”,“display: table-footer-group”)。
print.css
@media print {
.page_content__print_break {
page-break-after: always;
break-after: always;
}
footer.footer_content {
visibility: show;
opacity: 1;
filter: alpha(opacity=100);
position: fixed;
bottom: 0;
}
footer.footer_content::after {
counter-increment: page_number;
content: "Page:" counter(page_number);
}
}
index.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Table based CSS footer example</title>
<style media="screen">
footer.footer_content,
.page_content__print_break {
visibility: hidden;
opacity: 0;
filter: alpha(opacity=0);
}
</style>
<link rel="stylesheet" href="print.css">
</head>
<body>
<section class="page_content">
<div class="page_content__row">
<p>This is where page content should be placed</p>
<p>
Thanks be to:
<a href="https://dev59.com/3mIj5IYBdhLWcg3wn2do">
Print page numbers on pages when printing html
</a>
</p>
</div>
<!--
Note, following element is only for this example and should
be removed for the use-case of this posted quesiton.
-->
<div class="page_content__print_break"></div>
<div class="page_content__row">
<p>
More example content to perhaps force a second page to be printed...
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</section>
<footer class="footer_content" role="contentinfo"></footer>
</body>
</html>
顺便提一下,通过查看上面内部链接的问答,我发现您在当前问题中使用的
@page
可能不被CSS完全支持,要获得更多的“花哨效果”,需要使用
PagedJS。另外,请查看官方CSS文档以获取支持的
@page
功能。
...然而,经过一些测试,似乎浏览器的支持在近年来已经发生了变化;Firefox和Chrome似乎在打印时不执行
counter
操作,而
page-break-*
已经改为
break-*
,尽管这个属性现在似乎被两个浏览器忽略了 :-|
编辑:根据
@contm的评论,链接样式表与元素中的样式可能会产生不同的结果 X-\
实际上,在查看
Mozilla关于break-after
兼容性图表后,该功能已被Internet Explorer支持。
如果我找到任何解决这些问题的方法,我一定会再次编辑此答案。
更新:
根据CSS规范,
@page
目前仍处于WIP(草案或工作进展)阶段,因此截至2020年,不应期望任何供应商实施该规范。
固定而不是重复的页脚仅会计算一次,在所有增量完成后;以下是一个示例,每页都将报告
Page: 2
,因为有两个
page_content__row
...
print.css
@media print {
:root {
counter-reset: page_count;
}
.page_content__row {
counter-increment: page_count;
}
.page_content__print_break {
page-break-after: always;
break-after: always;
}
footer.footer_content {
visibility: show;
opacity: 1;
filter: alpha(opacity=100);
position: fixed;
bottom: 0;
}
footer.footer_content::after {
content: "Page: " counter(page_count);
}
}
除了第三方库外,我能想到的唯一自定义页脚信息的选项是在动态生成内容时计算页面断点,然后在需要的地方插入页脚元素。当然,这有一个巨大的缺点,即客户端字体/缩放设置将破坏页脚自定义或必须被忽略,如果您选择这条路,可能更简单的方法是在服务器端生成PDF文档并提供下载链接。