Home Web Front-end JS Tutorial web printing function

web printing function

Jul 07, 2018 am 10:44 AM

这篇文章主要介绍了关于web 打印功能,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

在项目开发中有时候会碰到要求打印页面中的数据的功能需求。需求原因主要有两点吧,一是需要打印的数据只是页面的一部分即页面的区域打印,比如只需要打印页面中表格里面选中的数据等,二是需要打印出来的样式和页面展示的样式有差别,比如需要将表格中的列竖着打印出来。

    我在开发中主要用到的就是window.print()这个js函数,这个函数相当于在页面中右键打印那个功能,但如果直接使用这个函数会将整个页面打印出来。所以我们对页面数据的隐藏或者重写一个页面,得到我们想要打印的数据和样式。

1、隐藏不需要打印的数据

    这种方式只能针对打印出来的样式和页面样式没什么差别的

    贴代码

<style type="text/css" media="print">
	/* 不需要打印的区域*/
	.notPreview{
		display: none;
	} 
</style>
Copy after login

然后调用window.print()即可

2、得到需要打印的数据,按照打印的样式,重写一个页面进行打印

先贴代码

bdhtml = window.document.body.innerHTML;
window.document.body.innerHTML = prnhtml;
window.print();
window.document.body.innerHTML = bdhtml;
window.document.location.reload(); 
Copy after login

在这个里面,bdhtml为原始的页面。prnhtml为真正调用右键打印的页面,需要根据实际情况进行拼接。在页面调用打印之后将页面恢复为原始页面,并reload,防止js函数不能正常使用。

在这之后还会遇到的问题 就是页眉、页脚、边距等这些了,这些实际上都是写在注册表里面的,所以需要修改注册表。

function PageSetup(name,value) {
	try{
		var HKEY_Path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
		var Wsh = new ActiveXObject("WScript.Shell");
		Wsh.RegWrite(HKEY_Path+name,value);
	}catch(e){
		alert("需要运行ActiveX才能进行打印");
	}
}
PageSetup("header","");	// 页眉为空
PageSetup("footer","");//页脚为空
PageSetup("margin_left","0mm");//左边距为0
PageSetup("margin_top","0mm");//上边距为0
Copy after login

name 大致有这些:header(页眉),footer(页脚),margin_bottom(下边距),margin_left(左边距),margin_right(右边距),margin_top(上边距)

value的一些常用的大致有这些: &w 窗口标题 、&u 网页地址 (URL) 、&d 短日期格式(由“控制面板”中的“区域设置”指定)、&D 长日期格式(由“控制面板”中的“区域设置”指定) 、&t 由“控制面板”中的“区域设置”指定的时间格式 、&T 24 小时时间格式 、&p 当前页号 、&P 网页总数 、&& 单个 & 号 (&) 、&b 紧跟在这些字符之后的文本居中打印。 、 &b&b 紧跟在第一个 "&b" 之后的文本居中打印,跟在第二个 "&b" 之后的文本按右对齐方式打印。 

大概能满足大部分的基本要求吧,我也是个了解的很浅,还请大家多多指教!

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

 Vue+Electron实现简单桌面应用

koa2实现拦截器进行登录前session的校验

The above is the detailed content of web printing function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

How do I optimize JavaScript code for performance in the browser? How do I optimize JavaScript code for performance in the browser? Mar 18, 2025 pm 03:14 PM

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

How do I debug JavaScript code effectively using browser developer tools? How do I debug JavaScript code effectively using browser developer tools? Mar 18, 2025 pm 03:16 PM

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How do I use source maps to debug minified JavaScript code? How do I use source maps to debug minified JavaScript code? Mar 18, 2025 pm 03:17 PM

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

See all articles