Cannot window.print() in runJS

Hi, it’d be helpful if we could access window.print() in runJS. thanks

js action:
const win = document.querySelector(‘body’)?.ownerDocument?.defaultView;
win.print()

js block:
ctx.element.ownerDocument.defaultView.print();

1 Like

yes it does print, but it also print the whole screen, and not just the rendered JS block. how can I limit it?

window.print() prints the current document, so limit the output with print CSS rather than a different print() call. Give the rendered JS block a class such as my-print-area, then add:

@media print {
body * { visibility: hidden; }
.my-print-area, .my-print-area * { visibility: visible; }
.my-print-area { position: absolute; inset: 0; width: 100%; }
}

Then call ctx.element.ownerDocument.defaultView.print() as before. The selector must target the JS block’s rendered container. If its DOM container differs, please share that structure or a screenshot so the selector can be adjusted.

1 Like

thanks but this works better.

@media print {
                html, body, #root, [class*="layout"], [class*="content"], [class*="container"], :has(.my-print-area) {
                    height: auto !important;
                    min-height: 0 !important;
                    overflow: visible !important;
                    position: static !important;
                }
                body :not(:has(.my-print-area)):not(.my-print-area):not(.my-print-area *) {
                    display: none !important;
                }
                body * {
                    visibility: hidden;
                }
                .my-print-area, .my-print-area * {
                    visibility: visible;
                }
            }