这篇文章主要介绍了关于web 打印功能,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
在项目开发中有时候会碰到要求打印页面中的数据的功能需求。需求原因主要有两点吧,一是需要打印的数据只是页面的一部分即页面的区域打印,比如只需要打印页面中表格里面选中的数据等,二是需要打印出来的样式和页面展示的样式有差别,比如需要将表格中的列竖着打印出来。
我在开发中主要用到的就是window.print()这个js函数,这个函数相当于在页面中右键打印那个功能,但如果直接使用这个函数会将整个页面打印出来。所以我们对页面数据的隐藏或者重写一个页面,得到我们想要打印的数据和样式。
1、隐藏不需要打印的数据
这种方式只能针对打印出来的样式和页面样式没什么差别的
贴代码
<style type="text/css" media="print"> /* 不需要打印的区域*/ .notPreview{ display: none; } </style>
然后调用window.print()即可
2、得到需要打印的数据,按照打印的样式,重写一个页面进行打印
先贴代码
bdhtml = window.document.body.innerHTML; window.document.body.innerHTML = prnhtml; window.print(); window.document.body.innerHTML = bdhtml; window.document.location.reload();
在这个里面,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
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中文网!
相关推荐:
The above is the detailed content of web printing function. For more information, please follow other related articles on the PHP Chinese website!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
