如何在打印机中打印图像文件

时间:2010-04-30 03:37:21

标签: javascript image printing

我编写了一个简单的程序来在JSF中打印图像....

我有一张图片(sampleImage.png)..我已经将我的电脑连接到打印机......

手动我打开图像并选择打印选项,然后我从打印机获得图像....

现在我想使用javascript打印图片....

文件名:imagePrint.jsp

                                                   

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Printer</title>          
        <script type="text/javascript">
             function printImage()
            {                                       
               // Here i want write a script for print image                              
            }
        </script>

    <body>
        <h:form id="fileViewerForm">
            <rich:panel>
                <f:facet name="header">
                    <h:outputText value="Printer"/>
                </f:facet>

                <h:commandButton value="PrintImage" onclick="printImage();"/>

                   <rich:panel id="imageViewerPanel">                               
                       <h:graphicImage id="imageViewer" value="sampleImage.png" url="sampleImage.png"/>             
                  </rich:panel>                                       
            </rich:panel>
        </h:form>
    </body>
</html>

帮我解决这个问题..

以下脚本将textarea打印到打印机中..... 所以我需要打印图像

            function printText()
            {
                alert("Text Area print to printer start....");
                var textElem = document.getElementById("fileViewerForm:textAreaGrid1").innerHTML;
                alert("Text Area Content : " + textElem);

                if(textElem.toLowerCase().indexOf("<textarea", 0) != -1)
                {
                    textElem = document.getElementById("fileViewerForm:fileContent1").value;
                    var regExp = /\n/gi;
                    textElem = textElem.replace(regExp,'<br>');
                }
                popup = window.open('','popup','toolbar=no,menubar=no,width=200,height=150');
                popup.document.open();
                popup.document.write("<html><head></head><body onload='print()'>");
                popup.document.write(textElem);
                popup.document.write("</body></html>");
                popup.document.close();
            }             

3 个答案:

答案 0 :(得分:1)

您将无法使用javascript进行打印,因为您无法从浏览器管理硬件设备并在那里执行。

答案 1 :(得分:1)

您将同时使用两者获得图片ID  h:form和h:graphicImage tag id's

Java脚本是:

 function printImage()         
 {            
   var iamgeId = document.getElementById('fileViewerForm:imageViewer');

   var imagObject = new Image();
   imagObject = iamgeId;
   var originalImage = '<img id="imageViewer" src="'+imagObject.src+'" 
                        height="'+imagObject.height+'"
                         width="'+imagObject.width+'" />';

   popup =  window.open('','popup','toolbar=no,menubar=no,width=200,height=150');
   popup.document.open();
   popup.document.write("<html><head></head><body onload='print()'>");
   popup.document.write(originalImage);
   popup.document.write("</body></html>");
   popup.document.close();           
}

JSF代码是:

  <h:commandButton value="Print" onclick="printImage();"/><br>
       <rich:panel id="imageViewerPanel">                

            <h:graphicImage id="imageViewer" url="sampleImage.png"
                            value="sampleImage.png" width="200"
                                                     height="200" />
       </rich:panel>
  </h:panelGrid>

适用于FireFox 3.0.18。

通过,
 Eswara Moorthy,NEC。

答案 2 :(得分:0)

您可以将其发送到浏览器

   window.print();

由浏览器决定做什么。

要打印页面的特定部分,请考虑打印样式表。使用media属性可以使某些文件仅打印样式。

<link rel="stylesheet" href="/assets/css/print.css" media="print" />