javascript codeigniter 加载没有任何用于打印的 html 标题的视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14325013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:18:44  来源:igfitidea点击:

codeigniter load a view without any html header for printing

phpjavascriptcodeigniterprintingcodeigniter-2

提问by Zalaboza

php

php

function print(){
    $data=$this->session->userdata('print_data');
    $this->load->view('printer',$data);
}

Html (printer view)

Html(打印机视图)

<div id='printerwrapper'>
......
</div>
<style>.....</style>
<script>windows.print();</script>

when i view this and print it out, it print the whole page and the div is resized to become small part of page , so i checked the html of page found that ci didnt just render the view but it also added all meta tags and basic html page structure.

当我查看并打印出来时,它会打印整个页面,并且 div 的大小被调整为页面的一小部分,所以我检查了页面的 html 发现 ci 不仅呈现视图,而且还添加了所有元标记和基本html页面结构。

<html><head><title></title></head><body>......</body></html>

here is example of the page im trying to print out enter image description hereall i want to print is this Div, not whole page !

这是我试图打印的页面示例, 在此处输入图片说明我想打印的只是这个 Div,而不是整个页面!

is there something i should know about how to make a printable pages ?

关于如何制作可打印页面,我应该知道些什么?

回答by Sharif

You can easily print a Selected divby JavaScript. Have a look the following code....

您可以通过JavaScript轻松打印Selected div。看看下面的代码......

First add this JavaScript code in the head...

首先在头部添加这段 JavaScript 代码......

<script type="text/javascript">
function printDiv(divName) {
    var printContents = document.getElementById(divName).innerHTML;
    var originalContents = document.body.innerHTML;
    document.body.innerHTML = printContents;
    window.print();
    document.body.innerHTML = originalContents;
}
</script>

and then the view page are as follows...

然后查看页面如下...

<div id="printableArea">
       Your Content here.....
</div>


<input type="button" onclick="printDiv('printableArea')" value="Print Invoice" />

When you click the Print Invoice Button. Then it will print out with the selected DIV id Printablearea

当您单击“打印发票”按钮时。然后它会用选定的 DIV id Printablearea打印出来

Try this. Hope it will work. Best of luck.

试试这个。希望它会起作用。祝你好运。

回答by Wesley

I think it's the browser's goal to make a print look similar to the way it looks on-screen, unless it's told otherwise. With that said, maybe you should consider just using a CSS stylesheet specifically for printing by using @media print. I went and found an articlefor you that I remember seeing quite some time ago in regards to print media queries.

我认为浏览器的目标是使打印看起来与屏幕上的外观相似,除非另有说明。话虽如此,也许您应该考虑使用专门用于打印的 CSS 样式表,使用@media print. 我去找了一篇关于印刷媒体查询的文章,我记得很久以前看过。

It really depends on your objective though. If you want the on-screen version to look like it does now, but the print version to look different, I think this is the way to go. If it doesn't matter how it looks on-screen, maybe you could get rid of your printerwrapper altogether and just float things left and right.

不过这真的取决于你的目标。如果您希望屏幕版本看起来像现在一样,但印刷版本看起来不同,我认为这是要走的路。如果它在屏幕上的外观无关紧要,也许您可​​以完全摆脱打印机包装器,而只是左右浮动。