TonyGermaneri / canvas-datagrid

Canvas based data grid web component. Capable of displaying millions of contiguous hierarchical rows and columns without paging or loading, on a single canvas element.
BSD 3-Clause "New" or "Revised" License
1.41k stars 184 forks source link

Feature request: print view of canvas #101

Open AviAkGlobal opened 6 years ago

AviAkGlobal commented 6 years ago

Feature request: print view of canvas, which might contain, for example:

  1. Selection might not be relevant in print view, as well as, row/column headers (A,B,C,1,2,3...)
  2. Frozen columns/rows should be kept between pages
  3. Scrollbars are irrelevant for print view
  4. The canvas should stretch through the whole screen and (even) more

Currently, I'm doing something like this - basically preparing the canvas as an image, rendering it in a new page and executing print():

    $scope.printReport = function () {        
        grid.attributes.showRowHeaders = false
        grid.attributes.showColumnHeaders = false
        var element = grid.canvas;
        var data = element.toDataURL() //attempt to save base64 string to server using this var  
        var windowContent = '<!DOCTYPE html>';
        windowContent += '<html>'
        windowContent += '<head><title>Print canvas</title></head>';
        windowContent += '<body>'
        windowContent += '<img src="' + data + '">';
        windowContent += '</body>';
        windowContent += '</html>';
        var printWin = window.open('', '', 'width=340,height=260');
        printWin.document.open();
        printWin.document.write(windowContent);
        printWin.document.close();
        printWin.focus();
        $timeout
        $timeout(function () {
            var is_chrome = function () { return Boolean(window.chrome); }
            if (is_chrome) {
                printWin.print();
                $timeout(function () {
                    printWin.close();
                    grid.attributes.showRowHeaders = true
                    grid.attributes.showColumnHeaders = true
                }, 10000);
                //give them 10 seconds to print, then close
            }
            else {
                printWin.print();
                printWin.close();
            }
            grid.attributes.showRowHeaders = true
            grid.attributes.showColumnHeaders = true
        }, 0)
    }

This approach has a lot of issues to deal with.

TonyGermaneri commented 6 years ago

I've tried this too. If I recall the biggest problem is spanning pages. It was a long time ago though, maybe things are easier now. You can move the grid to specific locations with grid.scrollTo(x, y) and you can give the grid a height and width that it should respect. Then you just need to set the background colors of the various scroll bar elements to transparent. That should allow you to see what's under them.

Be careful about DPI when you're printing. You may need to allow for a very high DPI ratio https://tonygermaneri.github.io/canvas-datagrid/docs/#canvasDatagrid.args.maxPixelRatio for printing on some printers. To do that it may require some hacking or changes to the lib, I'm not sure.