打印时如何在一个 A4 页面上放置两个 HTML div?



我有一个HTML页面,里面有大约1000个类似的div。

在这种情况下,我想做的是,当用户想要打印页面时,将其中两个放在一个 A4 页面上,类似于"适合页面"功能。

如果很重要,页面是使用 PHP 生成的。

关于我该怎么做的任何线索?

提前谢谢。

试试这个。 A4 在 300ppi 中的尺寸为 2480 x 3508 用CSS取一个主div:

.main{
display grid;grid-gap:10px;
grid-template-columns:repeat(auto-fill,minmax(1734px,1fr);
/* 1734 = 3508/2 -20px for grid-gap */
}

让我知道天气它有效。它只是基于理论。

我假设 2 个div 的内容始终适合 A4 页面而不会改变字体大小。

基于 CSS 设置 A4 纸张大小。

添加了 css 以每 2 个div 添加分页符。

我使用在Firefox中打印到pdf文件作为检查布局的快速打印机免费方法。

<!DOCTYPE html>
<html>
<head>
<style>
/* 
based on 
https://stackoverflow.com/a/30345808/1692270 *
then added css to add page break every 2 items
*/
/*ie last in every group of 2*/
/*page break every 2nd item */
div:nth-of-type(2n-0) {
background: red;
page-break-after: always;
}
/*ie 1st in every group of 2*/
div:nth-of-type(2n-1) {
background: blue;
}
body {
background: rgb(204,204,204); 
}
page[size="A4"] {
background: white;
width: 21cm;
height: 29.7cm;
display: block;
margin: 0 auto;
margin-bottom: 0.5cm;
box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
}
@media print {
body, page[size="A4"] {
margin: 0;
box-shadow: 0;
}
}
</style>
</head>
<body>
<div>The first div.</div>
<div>The second div.<br>more stuff here</div>
<div>The third div.</div>
<div>The fourth div.</div>
<div>The fifth div.</div>
<div>The sixth div.</div>
<div>The seventh div.</div>
<div>The eight div.</div>
<div>The ninth div.</div>
<div>The 10th div.</div>
<div>The 11th div.</div>
<div>The 12 div.</div>
<div>The 13 div.</div>
<div>The 14 div.</div>
<div>The 15 div.</div>
<div>The 16th div.</div>
<div>The 17th div.</div>
</body>
</html>

最新更新