Home  >  Article  >  Backend Development  >  How to implement print preview function in php

How to implement print preview function in php

藏色散人
藏色散人Original
2020-08-21 09:42:573240browse

php method to implement the print preview function: first get the html code of the current page; then set the printing start area and printing end area; then fetch the html from the start and end codes backward; it is best to determine the content to be printed That’s it.

How to implement print preview function in php

Recommended: "PHP Video Tutorial"

php realizes the print preview function

<inputid="btnPrint" type="button" value="打印"onclick="javascript:window.print();" />
<inputid="btnPrint" type="button" value="打印预览" onclick=preview(1)/>
<styletype="text/css" media=print>
.noprint{display : none}
</style>
<pclass="noprint">不须要打印的地方</p>
<script>
functionpreview(oper)
{
if (oper <10)
{
bdhtml=window.document.body.innerHTML;//获取当前页的html代码
sprnstr="<!–startprint"+oper+"–>";//设置打印開始区域
eprnstr="<!–endprint"+oper+"–>";//设置打印结束区域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18);//从開始代码向后取html
prnhtmlprnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
</script>
<p>XXXXX</p>
<!–startprint1–>要打印的内容<!–endprint1–>
<inputid="btnPrint" type="button" value="打印"onclick="javascript:window.print();" />
<inputid="btnPrint" type="button" value="打印预览" onclick=preview(1)/>
<styletype="text/css" media=print>
.noprint{display : none}
</style>
<pclass="noprint">不须要打印的地方</p>
<script>
functionpreview(oper)
{
if (oper <10)
{
bdhtml=window.document.body.innerHTML;//获取当前页的html代码
sprnstr="<!–startprint"+oper+"–>";//设置打印開始区域
eprnstr="<!–endprint"+oper+"–>";//设置打印结束区域
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+18);//从開始代码向后取html
prnhtmlprnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
</script>
<p>XXXXX</p>
<!–startprint1–>要打印的内容<!–endprint1–>

The above is the detailed content of How to implement print preview function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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