最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

printing - How to print image from browser using JavaScript - Stack Overflow

programmeradmin6浏览0评论

I'm able to print the webpage with the image using JavaScript. But it will print out the whole page with white spaces. What can I do to be able to just print out the Image using JavaScript?

I'm able to print the webpage with the image using JavaScript. But it will print out the whole page with white spaces. What can I do to be able to just print out the Image using JavaScript?

Share asked Oct 19, 2018 at 3:56 owenowen 1356 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Load images on your page with just the necessary resolution you need. In your javascript, pass the highest resolution image url to Print.js for a better print quality.

Import printjs library: printJS('images/print-01-highres.jpg', 'image')

Check this JavaScript library which provide much more options to print from html view.

http://printjs.crabbly./

https://www.cssscript./javascript-library-printing-elements-page-print-js/

Use window.open to open the image in a separate window, then print that.

Alternately, hide everything except the image (using either JavaScript or @media CSS) before printing.

You can use html2canvas (https://html2canvas.hertzen./) to capture the image (s) of your choice from html page and print them. Below is is simple code for capturing image or check fork of one of such example on codepen

<div id="capture" style="padding: 10px; background: #f5da55">
    <h4 style="color: #000; ">Hello world!</h4>
    <img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" />
</div>

html2canvas(document.querySelector("#capture")).then(canvas => {
    document.body.appendChild(canvas)
});
发布评论

评论列表(0)

  1. 暂无评论