dou9022 2018-03-12 18:41
浏览 143
已采纳

从div输出特定数据并通过打印机打印

I've spent all day in getting a solution for my need but since i'm not an experienced coder it's time for me to ask you guys for a little help.

My scenario is: I have some php code like:

<div id="unique_name">
<?php function_show_qrcode()?>
<?php function_show_text1()?>
<?php function_show_text2()?>
<?php function_show_text3()?>
</div>

What i want to achieve is to print the content of "unique_name" div (qrcode and some additional info) directly to a label printer attached via wireless on my smartphone and PC, printer that prints on continuous paper with a width of 62mm.

I have tried a lot of codes that i found but with no success because of browsers behavior regarding window.print()

The following code is working on firefox and chrome on pc but it is not working on safari mobile

<script>
        function printDiv(unique_name) {
        var printContents = document.getElementById(unique_name).innerHTML;
        var originalContents = document.body.innerHTML;
        document.body.innerHTML = printContents;
        window.print();
        document.body.innerHTML = originalContents;
            }
</script>

<a href="#" rel="nofollow" onclick="printDiv('unique_name')">Print label</a>

I am stuck on that and i think that something like converting the content of unique_name div into an image, save it and print it after that should work but there are a lot of steps to do and we want to do it more easy something like click and print or click and popup and then print and the most important thing is that it needs to work on mobile browsers

Thank you in advance!

  • 写回答

2条回答 默认 最新

  • douci1541 2018-03-13 01:43
    关注

    try this :

    <script>
    function printDiv(myId){
    
        var HiddenElements = document.querySelectorAll( 'body *' ); // get all the elements in the body
        var VisibleElements = document.querySelectorAll( '#' + myId + ' *' ); // gets the elements inside the div to be printed
    
        var index = 0, length = HiddenElements.length;
        for (  ; index < length; index++) {
            HiddenElements[index].style.visibility = "hidden"; // hide all the elements in the body             
        }   
    
        index = 0;
        length = VisibleElements.length;
        for (  ; index < length; index++) {
            VisibleElements[index].style.visibility = "visible"; // show all the elements inside the div to be printed      
        }
    
        // display the element to be printed
    
        myElement = document.getElementById(myId);
        myElement.style.visibility = "visible"
    
        var oldPos = myElement.style.position;
        myElement.style.position = "absolute";
        myElement.style.left = 0;
        myElement.style.top = 0;
    
        setTimeout(window.print, 1000); // Wait a bit for the DOM then Print ( Safari :/ )
    
        // wait for the data to be sent to the printer then display the previous content
        setTimeout(function(){      
            index = 0;
            length = HiddenElements.length;
            for (  ; index < length; index++) {
                HiddenElements[index].style.visibility = "visible";             
            }   
    
            myElement.style.position = oldPos;
    
        }, 5000);
    
    }
    </script>
    
    <a href="#" rel="nofollow" onclick="printDiv('unique_name')">Print label</a>
    

    JsFiddle : https://jsfiddle.net/2m5ha1ta/67/

    i don't know why on JsFiddle the onclick doesn't work, so i added an id to the a and added an eventListener to it and it's working ( check the fiddle )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用