Material-ui: [Dialog] Print support

Created on 25 Feb 2019  路  6Comments  路  Source: mui-org/material-ui

I am trying to add Print functionality to a fullscreen dialog with a table inside

  • [x] This is not a v0.x issue.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior 馃

It should print all pages(rows) of the table

Current Behavior 馃槸

It only prints the first page (23 rows if portrait, 15 if landscape)...

Steps to Reproduce 馃暪

Go to this sandbox and try to print the dialog... you'll see only one page 'printed' and that's it, even though the table has at least 2-3 pages...
https://codesandbox.io/s/x3qonjjzjw?fontsize=14

Context 馃敠

I am trying to add Print functionality (window.print()) to a fullscreen dialog with a table inside, and it doesn't work. it only displays the first page of the dialog(table) and that's it. it would be useful if this 'feature' would work out of the box, without any hacks or additional code...

Your Environment 馃寧

| Tech | Version |
|--------------|---------|
| Material-UI | v3.9.2 - latest |
| React | 16.6.3 |
| Browser | Chrome |
| TypeScript | 3.1.3 |
| etc. | |

Dialog enhancement good first issue

Most helpful comment

Ok, that works, but when I do this:

container: { display: "none", '@media print': { display: "block" }, '@page': { size: "auto" } }

It throws an error: Cannot read property 'addRule' of null. Is beacuse of the addition of @page.
Is there an easy fix for this?

All 6 comments

The following changes should introduce a complete print support for the dialog (scroll body, scroll container and fullscreen):

--- a/packages/material-ui/src/Dialog/Dialog.js
+++ b/packages/material-ui/src/Dialog/Dialog.js
@@ -14,7 +14,11 @@ import Paper from '../Paper';

 export const styles = theme => ({
   /* Styles applied to the root element. */
-  root: {},
+  root: {
+    '@media print': {
+      position: 'absolute',
+    },
+  },
   /* Styles applied to the root element if `scroll="paper"`. */
   scrollPaper: {
     display: 'flex',
@@ -29,6 +33,9 @@ export const styles = theme => ({
   /* Styles applied to the container element. */
   container: {
     height: '100%',
+    '@media print': {
+      height: 'auto',
+    },
     // We disable the focus ring for mouse, touch and keyboard users.
     outline: 'none',
   },
@@ -39,6 +46,10 @@ export const styles = theme => ({
     margin: 48,
     position: 'relative',
     overflowY: 'auto', // Fix IE 11 issue, to remove at some point.
+    '@media print': {
+      overflowY: 'visible',
+      boxShadow: 'none',
+    },
   },
   /* Styles applied to the `Paper` component if `scroll="paper"`. */
   paperScrollPaper: {

@emildatcu Do you want to submit a pull request? :)

Will do.
I hope this works as intended. I've wrecked my brains trying to find a solution for this.

Thanks :)

@oliviertassinari thanks for the fix. You can also override the CSS like so:

import { MuiThemeProvider } from '@material-ui/core/styles';

const outerTheme = createMuiTheme({
  overrides: {
    MuiDialog: {
      root: {
        '@media print': {
          position: 'absolute',
        },
      },
       /* Styles applied to the container element. */
      container: {
        height: '100%',
        // We disable the focus ring for mouse, touch and keyboard users.
        outline: 'none',
        '@media print': {
          height: 'auto',
        },
      },
      /* Styles applied to the `Paper` component. */
      paper: {
        display: 'flex',
        flexDirection: 'column',
        margin: 48,
        position: 'relative',
        overflowY: 'auto', // Fix IE 11 issue, to remove at some point.
        '@media print': {
          overflowY: 'visible',
          boxShadow: 'none',
        },
      },
  }
}
});

And then define your render() like:

<MuiThemeProvider theme={outerTheme}> <Dialog><Dialog/> </MuiThemeProvider>

I have been looking for this solution for couple days now. Gold @oliviertassinari.

@ghoshabhi Do you mean that upgrading was enough to solve your problem?

Ok, that works, but when I do this:

container: { display: "none", '@media print': { display: "block" }, '@page': { size: "auto" } }

It throws an error: Cannot read property 'addRule' of null. Is beacuse of the addition of @page.
Is there an easy fix for this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rbozan picture rbozan  路  3Comments

newoga picture newoga  路  3Comments

ryanflorence picture ryanflorence  路  3Comments

revskill10 picture revskill10  路  3Comments

sys13 picture sys13  路  3Comments