php 如何通过代码调整打印选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19028419/
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-08-25 18:41:43  来源:igfitidea点击:

How to adjust print options through code?

javascriptphpjoomla

提问by Amol Chakane

I have a ticket booking system in Joomla.

我在 Joomla 有一个订票系统。

When user clicks on ticket link a ticket is shown on the site.

当用户单击票证链接时,网站上会显示票证。

I am using a barcode.phpfile to generate the barcode image for ticket number.

我正在使用一个barcode.php文件来生成票号的条形码图像。

Now there are 2 scenarios I used to print ticket.

现在有两个场景我用来打印票。

  1. When I print that ticket using window.print()or Ctrl+P, 2 pages get printed even though my ticket content is only single page.

  2. When I use following javascript code to print specific part of the page, barcode image is not generated.

    function print_specific_div_content(){
        var content = "<html>";
        content += document.getElementById("divToPrint").innerHTML ;
        content += "</body>";
        content += "</html>";
    
        var printWin = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');
        printWin.document.write(content);
        printWin.document.close();
        printWin.focus();
        printWin.print();
        printWin.close();
    }
    
  1. 当我使用window.print()Ctrl+P打印该票证时,即使我的票证内容只有单页,也会打印 2 页。

  2. 当我使用以下 javascript 代码打印页面的特定部分时,不会生成条形码图像。

    function print_specific_div_content(){
        var content = "<html>";
        content += document.getElementById("divToPrint").innerHTML ;
        content += "</body>";
        content += "</html>";
    
        var printWin = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');
        printWin.document.write(content);
        printWin.document.close();
        printWin.focus();
        printWin.print();
        printWin.close();
    }
    

My requirements are:

我的要求是:

Ticket should be printed only on single page.

票只应打印在单页上

Any help or suggestion will be appreciated.

任何帮助或建议将不胜感激。

Thanks.

谢谢。

EDIT 1 :

编辑 1:

I modified my function as follow, but unfortunately it shows new window but no print dialog. :(

我修改了我的函数如下,但不幸的是它显示了新窗口但没有打印对话框。:(

function print_specific_div_content(){
    var win = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');
    var handler = function() {
      win.print();
      win.close();
    };
    if(win.addEventListener)
        win.addEventListener('load', handler, false);
    else if(win.attachEvent)
        win.attachEvent('onload', handler, false);

    var content = "<html>";
    content += document.getElementById("divToPrint").innerHTML ;
    content += "</body>";
    content += "</html>";
    win.document.write(content);
    win.document.close();
}

回答by Dark Falcon

When printing just the content, you need to wait for the document to load before printing so that the images will be loaded:

仅打印内容时,您需要等待文档加载后再打印,以便加载图像:

function print_specific_div_content(){
    var win = window.open('','','left=0,top=0,width=552,height=477,toolbar=0,scrollbars=0,status =0');

    var content = "<html>";
    content += "<body onload=\"window.print(); window.close();\">";
    content += document.getElementById("divToPrint").innerHTML ;
    content += "</body>";
    content += "</html>";
    win.document.write(content);
    win.document.close();
}