[前端]简要记录css控制打印分页

参考链接
page-break-before和page-break-after

在调用window.print()时,可以实现打印效果,但内容太多时要进行分页打印。

在样式中有规定几个打印的样式

page-break-before和page-break-after CSS属性并不会修改网页在屏幕上的显示,这两个属性是用来控制文件的打印方式。

每个打印属性都可以设定4种设定值:auto、always、left和right。其中Auto是默认值,只有在有需要时,才需设定分页符号 (Page breaks)。

page-break-before若设定成always,则是在遇到特定的组件时,打印机会重新开始一个新的打印页。

page-break-before若设定成left,则会插入分页符号,直到指定的组件出现在一个左边的空白页上。

page-break-before若设定成right,则会插入分页符号,直到指定的组件出现在一个右边的空白页上。

page-break-after属性会将分页符号加在指定组件后,而非之前。

实例:

<table style="width: 880px;table-layout:fixed;margin: 34px 50px 0;">
	<tr>
		<td width="80" class="table-header">单位<br/>名称</td>
		<td width="480" colspan="6" class="table-text">哔哩哔哩矿业无限公司</td>
		<td width="80" class="table-header">企业<br>类别</td>
		<td width="240" colspan="3">
			<div class="table-text">
				<input type="checkbox" disabled checked />一类
				<input style="margin-left: 10px;" type="checkbox" disabled />二类
				<input style="margin-left: 10px;" type="checkbox" disabled />三类
			</div>
			</td>
	</tr>
	<tr>
		<td class="table-header"> 经营<br>地址</td>
		<td colspan="6" class="table-text">上海浦东大道720号12楼</td>
		<td class="table-header">邮政<br>编码</td>
		<td colspan="3" class="table-text">123456</td>
	</tr>
	<tr>
		<td class="table-header">公司服<br>务电话</td>
		<td colspan="5" class="table-text">0711-1234567</td>
		<td class="table-header">传真<br>号码</td>
		<td colspan="4" class="table-text">0711-1234567</td>
	</tr>
	<tr>
		<td class="table-header">网 址</td>
		<td colspan="5" class="table-text">https://www.bilibili.com/</td>
		<td class="table-header">电子<br>邮箱</td>
		<td colspan="4" class="table-text">11111111111@qq.com</td>
	</tr>
	<tr>
		<td rowspan="3" class="table-header"><br><br><br><br></td>
		<td colspan="5" class="table-child-header"><span class="table-header">姓名:</span>陈睿</td>
		<td rowspan="3" class="table-header"><br><br><br><br><br><br><br></td>
		<td colspan="4" class="table-child-header"><span class="table-header">姓名:</span>碧诗</td>
	</tr>
	<tr>
		<td class="table-header">职务</td>
		<td>董事长</td>
		<td colspan="2" class="table-header">职称</td>
		<td>CEO</td>
		<td class="table-header">职务</td>
		<td>董事</td>
		<td class="table-header">职称</td>
		<td>站长</td>
	<tr>
		<td colspan="5" class="table-child-header"><span class="table-header">办公电话:</span>0711-1234567<br><span class="table-header">&nbsp;&nbsp;机:</span>13433333333</td>
		<td colspan="4" class="table-child-header"><span class="table-header">办公电话:</span>0711-1234567<br><span class="table-header">&nbsp;&nbsp;机:</span>13433333333</td>
	</tr>
	<tr>
		<td rowspan="4" class="table-header"><br><br><br></td>
		<td colspan="2" class="table-header">管理干部人数</td>
		<td colspan="3">500</td>
		<td rowspan="2" class="table-header">主修<br>车型</td>
		<td colspan="4" rowspan="2">法拉利、兰博基尼、保时捷</td>
	</tr>
	<tr>
		<td colspan="2" class="table-header">职工人数</td>
		<td colspan="3">5000</td>
	<tr>
		<td colspan="2" rowspan="2" class="table-header">资产总额</td>
		<td colspan="2" class="table-header">固定资产</td>
		<td colspan="6">100000<span class="table-header">万元</span></td>
	</tr>
	<tr>
		<td colspan="2" class="table-header">流动资产</td>
		<td colspan="6">200000<span class="table-header">万元</span></td>
	</tr>
	<tr>
		<td style="height:260px;" class="table-header"><br><br><br></td>
		<td colspan="10" style="text-indent: 2em;text-align: left">
			秦孝公据崤函之固,拥雍州之地,君臣固守以窥周室,有席卷天下,包举宇内,囊括四海之意,并吞八荒之心。当是时也,商君佐之,内立法度,务耕织,修守战之具,外连衡而斗诸侯。于是秦人拱手而取西河之外。 孝公既没,惠文、武、昭襄蒙故业,因遗策,南取汉中,西举巴、蜀,东割膏腴之地,北收要害之郡。诸侯恐惧,会盟而谋弱秦,不爱珍器重宝肥饶之地,以致天下之士,合从缔交,相与为一。当此之时,齐有孟尝,赵有平原,楚有春申,魏有信陵。此四君者,皆明智而忠信,宽厚而爱人,尊贤而重士,约从离衡,兼韩、魏、燕、楚、齐、赵、宋、卫、中山之众。
		</td>
	</tr>
</table>
<div class="pageSeparator"></div>
<table style="width: 880px;table-layout:fixed;">
	<tr>
		<td rowspan="2" class="table-header" style="height:300px;"><br><br><br></td>
		<td colspan="10" class="table-child-header"><span class="table-header">申请加入:</span><span>普通会员<span class="table-header">(√)</span></span></td>
	</tr>
		<td colspan="10">
			<div style="text-indent: 2em;text-align: left;font-weight: bold;height: 280px;padding-top: 30px;">秦孝公据崤函之固,拥雍州之地,君臣固守以窥周室,有席卷天下,包举宇内,囊括四海之意,并吞八荒之心。当是时也,商君佐之,内立法度,务耕织,修守战之具,外连衡而斗诸侯。于是秦人拱手而取西河之外。</div>
			<div style="text-indent: 2em;text-align: left;">
				<span class="table-header">单位负责人签名:</span>陈睿
				<span style="position: relative;left: 300px;">
					<span class="table-header" >单位:</span>哔哩哔哩矿业无限公司
				</span>
			</div>
			<div style="position: relative;left: 208px;margin-top: 30px;">
				2021<span style="margin:0 10px;font-weight: bold;"></span >
				01<span style="margin:0 10px;font-weight: bold;"></span>
				28<span style="margin:0 10px;font-weight: bold;"></span>
			</div>
		</td>
	<tr>
	</tr>
	<tr>
		<td style="height:320px" class="table-header"><br><br><br><br><br><br></td>
		<td colspan="10" height="320px" >
			<div style="text-indent: 2em;text-align: left;height: 280px;padding-top: 30px">同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意。</div>
			<div style="position: relative;left: 162px;">
				<span class="table-header" >秘书处:</span>张三
			</div>
			<div style="position: relative;left: 208px;margin: 30px 0 40px 0;">
				2021<span style="margin:0 10px;font-weight: bold;"></span >
				01<span style="margin:0 10px;font-weight: bold;"></span>
				28<span style="margin:0 10px;font-weight: bold;"></span>
			</div>
		</td>
	</tr>
	<tr>
		<td style="height:320px" class="table-header"><br><br><br><br><br></td>
		<td colspan="10" height="320px" >
			<div style="text-indent: 2em;text-align: left;height: 280px;padding-top: 30px">同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意同意。</div>
			<div style="position: relative;left: 170px;">
				<span class="table-header" >会长签字:</span>李四
			</div>
			<div style="position: relative;left: 208px;margin: 30px 0 40px 0;">
				2021<span style="margin:0 10px;font-weight: bold;"></span >
				01<span style="margin:0 10px;font-weight: bold;"></span>
				28<span style="margin:0 10px;font-weight: bold;"></span>
			</div>
		</td>
	</tr>
</table>
printPage () {
            var iframe = document.getElementById('print-iframe')
            if (iframe) {
                document.body.removeChild(iframe)
            }
            var el = document.getElementById('print-preview')
            iframe = document.createElement('iframe')
            iframe.setAttribute('id', 'print-iframe')
            iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;')
            document.body.appendChild(iframe)
            iframe.contentDocument.body.innerHTML = `
                <style>
                    @page {margin-bottom: 0mm;margin-top: 0mm;}
                    #print-preview .pageSeparator{visibility:hidden; page-break-after:always; overflow:hidden; height:0px;}
                    #print-preview {padding: 10px 97x;text-align:center;color:#333333;font-family: "宋体";}
                    #print-preview .top-text{position: relative;left: 350px;font-size: 18px;margin: 130px 0 50px 0;}
                    #print-preview .title {font-size:35px;color:#333333;line-height:44px;font-weight: bold;margin-top:140px;}
                    #print-preview .cover-title1 {font-size:52px;font-weight: bold;margin-top:30px;}
                    #print-preview .cover-title2 {font-size:52px;font-weight: bold;margin: 80px 0 250px;}
                    #print-preview .cover-body {font-size: 32px;margin-left: 60px;padding-left: 140px;text-align: left;font-weight: bold;}
                    #print-preview .cover-body .text {margin: 0px 20px;}
                    #print-preview .cover-body .date {margin:0 30px}
                    #print-preview .cover-body .line {margin: -4px 0 60px 160px;width: 550px;background-color: #666;height: 2px;border: 0;}
                    #print-preview .cover-bottom {font-size:28px;font-weight: bold;margin:570px 0 18px 0;padding-bottom: 220px;}
                    #print-preview .page2-title {font-size: 48px;font-weight: bold;padding: 100px 0 30px 0;}
                    #print-preview .page3-title {font-size: 36px;font-weight: bold;margin-top: 550px;padding-top: 70px}
                    #print-preview .fr {float: right;}
                    #print-preview .table-header {font-size: 16px;font-weight: bold;}
                    #print-preview .table-child-header {text-align: left;padding-left: 20px;}
                    #print-preview table {width: 906px;border-bottom:1px solid #999999;border-left:1px solid #999999;text-align:center;margin: 80px 50px 50px;}
                    #print-preview table td {border-top:1px solid #999999;border-right:1px solid #999999;box-sizing: border-box;height: 50px;line-height: 50px;}
                    #print-preview .TI2 {text-indent: 2em;text-align: left;}
                    #print-preview .agreement {text-align: left;font-size: 24px;padding-left: 20px;margin:24px 0 15px 0;}
                    #print-preview .indent {text-indent: 1.5em;}
                    #print-preview .agreement-indent {margin:24px 0 15px 0;text-indent: 1.5em;}
                    #print-preview .whole-text {border-bottom: 1px solid #999999;line-height: 36px;height: 36px;margin-left:20px;text-align: left;font-size: 16px;}
                    #print-preview .whole-text span {position: relative;left: -20px;}
                    #print-preview .text-line {display: inline-block;border-bottom: 1px solid #999999;text-align: center;text-indent: 0;height: 26px;vertical-align: text-bottom;}
                    #print-preview .insert-text {line-height: 32px;}
                </style>
                <div id="print-preview">${el.innerHTML}</div>
            `
            let resumeWindow = iframe.contentWindow
            if (!window.showModalDialog) {
                setTimeout(function () {
                    resumeWindow.print()
                }, 100)
            } else {
                // 兼容IE8,不加前面这两句,只能打印出整个页面的预览效果,并不是简历原件的内容
                resumeWindow.document.body.className += ' ext-ie'
                resumeWindow.document.execCommand('print', false, null)
                setTimeout(function () {
                    resumeWindow.print()
                }, 100)
            }
        }

打印预览效果
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端代码(HTML、JavaScript、JSP): ```html <!-- 文件列表展示 --> <table> <thead> <tr> <th>文件名</th> <th>文件大小</th> <th>上传时间</th> <th>操作</th> </tr> </thead> <tbody id="fileList"></tbody> </table> <!-- 分页 --> <div class="pagination"> <ul> <li><a href="javascript:void(0);" onclick="getPageData(1)">首页</a></li> <li><a href="javascript:void(0);" onclick="getPageData(currentPage-1)">上一页</a></li> <li><a href="javascript:void(0);" onclick="getPageData(currentPage+1)">下一页</a></li> <li><a href="javascript:void(0);" onclick="getPageData(totalPages)">末页</a></li> <li><a href="javascript:void(0);">共<span id="totalPages"></span>页</a></li> <li><a href="javascript:void(0);">当前第<span id="currentPage"></span>页</a></li> </ul> </div> <!-- 上传文件 --> <form id="uploadForm" enctype="multipart/form-data"> <input type="file" name="file" /> <button type="submit" onclick="uploadFile()">上传</button> </form> <!-- 删除文件 --> <button onclick="deleteFile()">删除</button> <!-- 下载文件 --> <a id="downloadLink" href="#" download></a> <script> var currentPage = 1; // 当前页码 var totalPages = 0; // 总页数 // 获取文件列表数据 function getPageData(page) { currentPage = page; $.ajax({ url: "/file/list", type: "GET", data: { pageNum: currentPage }, success: function(result) { // 处理返回的数据 $("#fileList").html(""); $.each(result.records, function(index, item) { var tr = "<tr>" + "<td>" + item.fileName + "</td>" + "<td>" + item.fileSize + "</td>" + "<td>" + item.uploadTime + "</td>" + "<td><input type='checkbox' name='fileId' value='" + item.id + "' /></td>" + "</tr>"; $("#fileList").append(tr); }); // 更新分页信息 totalPages = result.pages; $("#totalPages").text(totalPages); $("#currentPage").text(currentPage); } }); } // 上传文件 function uploadFile() { var formData = new FormData($("#uploadForm")[0]); $.ajax({ url: "/file/upload", type: "POST", data: formData, contentType: false, processData: false, success: function() { alert("上传成功!"); getPageData(currentPage); }, error: function() { alert("上传失败!"); } }); } // 删除文件 function deleteFile() { var fileIds = $("input[name='fileId']:checked").map(function() { return $(this).val(); }).get(); if (fileIds.length == 0) { alert("请选择要删除的文件!"); return; } $.ajax({ url: "/file/delete", type: "POST", data: { fileIds: fileIds.join(",") }, success: function() { alert("删除成功!"); getPageData(currentPage); }, error: function() { alert("删除失败!"); } }); } // 下载文件 function downloadFile(fileId) { $("#downloadLink").attr("href", "/file/download?fileId=" + fileId); $("#downloadLink").get(0).click(); } // 页面加载完成时获取第一页数据 $(function() { getPageData(1); }); </script> ``` 后端代码(Java): ```java @Controller @RequestMapping("/file") public class FileController { @Autowired private FileSystem fileSystem; // 分页展示文件列表 @GetMapping("/list") @ResponseBody public ResultVO<PageInfo<FileVO>> listFiles(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { List<FileVO> fileVOList = new ArrayList<>(); try { RemoteIterator<LocatedFileStatus> fileStatusList = fileSystem.listFiles(new Path("/"), true); while (fileStatusList.hasNext()) { LocatedFileStatus fileStatus = fileStatusList.next(); FileVO fileVO = new FileVO(); fileVO.setId(UUID.randomUUID().toString()); fileVO.setFileName(fileStatus.getPath().getName()); long fileSize = fileStatus.getLen(); if (fileSize < 1024) { fileVO.setFileSize(fileSize + " B"); } else if (fileSize < 1048576) { fileVO.setFileSize(fileSize / 1024 + " KB"); } else { fileVO.setFileSize(fileSize / 1048576 + " MB"); } fileVO.setUploadTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(fileStatus.getModificationTime()))); fileVOList.add(fileVO); } } catch (IOException e) { e.printStackTrace(); } PageInfo<FileVO> pageInfo = new PageInfo<>(fileVOList, pageNum, pageSize); return ResultVO.success(pageInfo); } // 上传文件 @PostMapping("/upload") @ResponseBody public ResultVO<?> uploadFile(@RequestParam("file") MultipartFile file) { try { FSDataOutputStream outputStream = fileSystem.create(new Path("/" + file.getOriginalFilename())); IOUtils.copyBytes(file.getInputStream(), outputStream, 4096, true); return ResultVO.success(); } catch (IOException e) { e.printStackTrace(); return ResultVO.error("上传文件失败!"); } } // 删除文件 @PostMapping("/delete") @ResponseBody public ResultVO<?> deleteFile(@RequestParam("fileIds") String fileIds) { String[] fileIdArray = fileIds.split(","); for (String fileId : fileIdArray) { try { fileSystem.delete(new Path("/" + fileId), false); } catch (IOException e) { e.printStackTrace(); return ResultVO.error("删除文件失败!"); } } return ResultVO.success(); } // 下载文件 @GetMapping("/download") public void downloadFile(@RequestParam("fileId") String fileId, HttpServletResponse response) { try { FSDataInputStream inputStream = fileSystem.open(new Path("/" + fileId)); response.setHeader("Content-Disposition", "attachment; filename=" + fileId); IOUtils.copyBytes(inputStream, response.getOutputStream(), 4096, true); } catch (IOException e) { e.printStackTrace(); } } } ``` 其中,`FileVO` 类用于封装文件信息,`ResultVO` 类用于统一接口返回格式,`PageInfo` 类用于封装分页信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值