Javascript 打印函数 : Remove space from preview page

标签 javascript jquery css html printing

您好,我正在使用 HTML5 以及 Javascript 和 jQuery 1.10 开发一个网络应用程序。我目前正在使用 javascript 打印功能。此时打印预览看起来像这样(我在下面用红色箭头描述):

enter image description here

我想要实现的是正确格式化页面以 100% 的宽度和高度显示,换句话说,尽可能多地删除空白(红色箭头)。

我的 CSS 代码:

@media print
{   
    * {-webkit-print-color-adjust:exact;}

    .no-print, .no-print *
    {
        display: none !important;
    }

    .print {
        position: fixed;
        overflow: auto;
        z-index: 100000; /* CSS doesn't support infinity */

        /* Any other Print Properties */
    }
}

Javascript 代码:

    function exportaPDF(){
        var mywindow = window.open('', 'CV', 'height=400,width=600');
        mywindow.document.write('<html><head><title>Mi CV</title>');
        mywindow.document.write('</head><body>');
        mywindow.document.write($("body").html());
        mywindow.document.write('</body></html>');

        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10

        mywindow.print();
        mywindow.close();

        return true;
    }

请注意,该网站有两列,但使用 css,我在发送打印时隐藏了正确的部分。

enter image description here

我做错了什么?

如有任何建议或帮助,我们将不胜感激!

提前致谢

更新:解决方案

按照@Jeroen Noten 的建议,我解决了这个在打印前更改 CSS 属性以删除空格的问题。我刚刚添加了这些行:

        $("#ContenedorIzquierdo").css({
            "width": "100%",
            "margin-top": "0px"
        });

最佳答案

尝试以下操作:为要包含在打印品中的部分提供一个 ID(例如 <div id="printedSection">)并引用它而不是完整的正文,如下所示:

mywindow.document.write('<html><head><title>Mi CV</title>');
mywindow.document.write('</head><body>');
mywindow.document.write($("#printedSection").html()); // get only the printed section, not the full body
mywindow.document.write('</body></html>');

并确保在该部分中,内容的宽度没有限制。

关于Javascript 打印函数 : Remove space from preview page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662117/

相关文章:

css - 容器类在 Twitter Bootstrap 3 中做了什么?

html - 覆盖 div 内所有子项的内联样式

javascript - 使用节点 js (express) 从 url 导入 json 文件

JavaScript 函数事件

javascript - 如何使用 split 但跳过 html/javascript/php 和其他内部标签?

javascript - 使用更改功能

javascript - Web Share Target API 不适用于 Android 9 操作系统版本

javascript - 在 Angular 的多个应用程序中,将影响多个组件的 scs 放在哪里?

javascript - .on() 事件处理程序未触发?

css - 应用特定于屏幕分辨率的 css 样式