Send canvas straight to printer/print preview

I had a canvas with an image on it, I wanted to 1) open it into print preview and 2) straight print it. Does anyone have any ideas, this was my idea so far but I dont like it: I open a new tab and print that tab. :frowning:

I did find PrintUtils.js but they all seem to rely on a window: https://dxr.mozilla.org/mozilla-central/source/toolkit/components/printing/content/printUtils.js

Hey all i made progress on this. I can send it straight to print dialog, but i cant send it to print preview.

What i do is to hiddenDOMWindow i add an iframe then listen to load event on that (i think this is e10s safe because hiddenDOMWindow is not a sperate process), then i after sending to print i delete the image there (for cross os reasons i cant destroy the iframe, it causes problems)

But does anyone have any ideas on how to send the iframe to print preview?

I can only find methods to send a tab to print preview like this:

                let l = window.PrintPreviewListener;
                window.PrintUtils.printPreview(l);

But downside of this, is it turns the whole dang browser into a print preview. My post below is better as it turns only single tab into a print preview.

I also found this function, however aWindow and aTargetWindow have to be the same. And when they are, it turns the docShell of that window to print preview, instead of opening in new window, which is really weird.

function noida_printpreview(aWindow, aTargetWindow) {
    var wbp = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIWebBrowserPrint);
    var listener = {
        onLocationChange: function(webProgress, request, location, flags) {},
        onProgressChange: function(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {},
        onSecurityChange: function(webProgress, request, state) {},
        onStateChange: function(webProgress, request, stateFlags, status) {},
        onStatusChange: function(webProgress, request, status, message) {},
        QueryInterface: function(iid) {
            if (iid.equals(Ci.nsIWebProgressListener) || iid.equals(Ci.nsISupportsWeakReference)) {
                return this;
            }
            throw Cr.NS_NOINTERFACE;
        }
    }
    //Services.prefs.setBoolPref('print.show_print_progress', false);
    //XXX I would have thought this would work, instead I'm forced to use prefs service
    //wbp.globalPrintSettings.showPrintProgress = false;
    wbp.printPreview(wbp.globalPrintSettings, aTargetWindow, listener);
    //Services.prefs.clearUserPref('print.show_print_progress');
}

However this is the closest I get, im thinking ill load a new tab instead ofa hidden iframe, and then use this function to convert its docShell to the print preview. However the print preview has no GUI elements, and back button doesnt work. So need help on this please.

I also tried this, but I keep getting rv is undefined:

Source: https://dxr.mozilla.org/mozilla-central/source/toolkit/components/printing/content/printUtils.js#103

    gBrowser.selectedBrowser.messageManager.sendAsyncMessage("Printing:Print", {
      windowID: gBrowser.selectedBrowser.outerWindowID,
    });
In Printing:Print:Done handler, got unexpected rv
                        undefined.

I hope the seperate posts are ok, i wanted to emphasize the different approaches I got to try to send something to print preview