从Javascript打印图像 [英] Print image from Javascript

查看:102
本文介绍了从Javascript打印图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些图像....每个图像的大小不同
例如:

  img1 = 200 x 1900 
img2 = 1800 x 400
img3 = 600 x 800

// HTML

 < html> 
< body>
< img src ='img1.jpg'>
< img src ='img2.jpg'>
< img src ='img3.jpg'>
< / body>
< / html>

// javascript

  var mywindow = window.open('','my div'); 
mywindow.document.write('< html>< head>< title>我的div< / title>');
mywindow.document.write('< / head>< body>');
data =< img src ='img1.jpg'>< img src ='img2.jpg'>< img src ='img3.jpg'>;
mywindow.document.write(data); // data = all image
mywindow.document.write('< / body>< / html>');
mywindow.print();
mywindow.close();

现在输出的是所有打印的图像,但尺寸已损坏
如何打印每个图片在A4页并打印在A4页面中。 解决方案

你不能。网站是为屏幕创建的,而不是打印机。



但是,您可以强制Web浏览器显示与A4相同像素尺寸的页面。但是,渲染事物时可能会出现一些怪癖。

 <!DOCTYPE html> 
< html>
< head>
< style>
body {
height:842px;
width:595px;
/ *到屏幕上的中间页面* /
margin-left:auto;
margin-right:auto;
}
< / style>
< head>
< body>
< / body>
< / html>

打印的最佳解决方案是使用PDF,这就是它们的用途。创建一个具有相同数据的PDF。在这种情况下,您可以完全控制打印格式。


i have some images .... the size of each image is different For example:

img1 = 200 x 1900
img2 = 1800 x 400
img3 = 600 x 800

// HTML

<html>
<body>
    <img src='img1.jpg'>    
    <img src='img2.jpg'>
    <img src='img3.jpg'>
</body>
</html>

// javascript

            var mywindow = window.open('', 'my div');
            mywindow.document.write('<html><head><title>my div</title>');
            mywindow.document.write('</head><body >');
            data = "<img src='img1.jpg'><img src='img2.jpg'><img src='img3.jpg'>";
            mywindow.document.write(data);  // data = all image
            mywindow.document.write('</body></html>');
            mywindow.print();
            mywindow.close();

Now the output is all images printed but the sizes is corrupted how can i print each image in page A4 and print it fitted in page (A4)

解决方案

You can't. Websites are created for screens, not printers.

However you could force the web browser to display the page with the same pixel dimensions as A4. However, there may be a few quirks when things are rendered.

<!DOCTYPE html>
<html>
  <head>
    <style>
    body {
        height: 842px;
        width: 595px;
        /* to centre page on screen*/
        margin-left: auto;
        margin-right: auto;
    }
    </style>
  <head>
  <body>
  </body>
</html>

The best solution for printing, is to use PDFs, that's what they are for. Create a PDF that has the same data. You have full control over the print format in that case.

这篇关于从Javascript打印图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆