Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing viewable contents of browser window

I have a website I am currently developing and the client has a very unique request. They would like the user to be able to hit a button and print the contents of the browser window. I wanted to know if anyone has implemented similar functionality or knows any strategy to develop something like this as I do not have the first clue.

Example: I have 30 images on a page but only 4 fit in the viewable area or browser window. I would like to only print the exact content of the browser window / or elements that are viewable area.

Thanks in advance,

JN

like image 417
jeffreynolte Avatar asked Jul 29 '11 17:07

jeffreynolte


People also ask

How do I print just the content of a Web page?

Just select the desired text on the current page and press CTRL+P. This will bring up the Print dialog, where you can simply select the "Selection" option there. This will print out only the selected text.

How do I print the content of a current window?

The task is to print the content of the current window by using the window. print() method in the document. It is used to open the Print Dialog Box to print the current document.


1 Answers

This method requires jQuery, but might be able to be rewritten in plain javascript.

With a bookmarklet app of mine I found that the popup window was the most reliable way to print dynamic content and allowed the user to see the content before printing it. I also found that reducing the font size allowed fitting all the content on the page while sill be readable. You might try shrinking the images as well if that is an option. I had tried targeting media types with CSS and some jQuery print plugins but found them unreliable at best.

Here's the function I use to pass a jQuery object to be printed. I maximized the window on open and changed the title/font size. If you have more than images you'll need to clone your CSS as well, mine just happened to be stored in a variable from the bookmarklet loading.

function printElement(oElement) {

    var oPopupWindow = window.open('', 'newwin', 'width=500,height=500');
    oPopupWindow.moveTo(0,0);
    oPopupWindow.resizeTo(screen.availWidth,screen.availHeight);
    oPopupWindow.document.open();

    var sHTML = '<html><head><title>TBA Enhanced User Interace</title>' +
        _EUI.sCSS + '<style>img {display:none!important}table{font-size:8pt!important}</style></head><body>'
        + $('<div />').append(oElement.clone()).html() + '</body></html>';

    oPopupWindow.document.write(sHTML);
    oPopupWindow.document.close();
    oPopupWindow.print();
    oPopupWindow.close();

}

function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

function printVisibleImages() {

    var oImageElement = $('<div />');

    $('img').each(function() {

        if (isScrolledIntoView(this)) {
            oImageElement.append(this);
        }

    });

    printElement(oImageElement);
}

Using the isScrolledIntoView function from this question: Check if element is visible after scrolling, something like the code above might work with some tweaking. Call printVisibleImages(), you might need to add some CSS for padding or pass the style sheets from the main page.

like image 120
Jeff Avatar answered Oct 24 '22 01:10

Jeff