Cimbali/markdown-viewer

Native print shortcut prints outer document, resulting in document being cut off after first page

Opened this issue · 2 comments

When I did Ctrl+P to print a document after it got rendered by this extension, I noticed that it only showed one page in the preview, cutting off all content after the first page. After panicking about if it was my custom styles causing it, I figured out that the HTML layout of the extension's viewer page has changed since the last time I used it. There's now an iframe at the root of the document, and that is what contains the proper rendered markdown elements. Because it's an iframe, it doesn't participate in fancy CSS Print rules and such, and it gets cut off after it fills a page.

A workaround for now is to right-click the page, select "This Frame" and select "Print Frame" from the context menu.

A solution to this would be to maybe prevent default on the print shortcut and call .print on the iframe document.

That’s an annoying unforeseen side-effect. There’s an upstream firefox bug that tracks correcting this, which was opened 8 years ago and has seen on activity so we’ll have to investigate workarounds as you suggest.

Something like

window.addEventListener("beforeprint", (event) => {
  event.preventDefault();
  iframe.focus();
  iframe.print();
});

However it seems print events aren’t cancellable. 🤔