html导出图片和打印

html导出图片和打印

 

打印用js自带的print即可,导出图片需要引入http://image.niunan.net/html2canvas.min.js

 

 

	<script src="html2canvas.min.js"></script>
 
	<script type="text/javascript">	
function printContent() {
			var content = document.getElementById('print-content').innerHTML;
			var newWindow = window.open('', '打印窗口');
			newWindow.document.write('<html><head><title>打印页面</title></head><body>' + content + '</body></html>');
			newWindow.document.close();
			newWindow.print();
			newWindow.close();
		}

		function saveImg() {



			// 选择 HTML 元素
			let element = document.getElementById("print-content");

			html2canvas(element, {
				allowTaint: true,
				scale: 2,
				background: "#F5F5F5"
			}).then(function (canvas) {
				document.body.appendChild(canvas);

				let dataURL = canvas.toDataURL();
				let a = document.createElement("a");
				a.setAttribute("download", "aaa.png");
				a.setAttribute("href", dataURL);
				a.click();
				document.body.removeChild(canvas)
			});


		}
        
</script>

 

posted @ 2023-07-14 09:54  牛腩  阅读(446)  评论(0)    收藏  举报