Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blank sheet error react to print #410

Closed
lizarragadev opened this issue Aug 24, 2021 · 15 comments
Closed

Blank sheet error react to print #410

lizarragadev opened this issue Aug 24, 2021 · 15 comments
Labels

Comments

@lizarragadev
Copy link

Hi everyone, I have a problem that has already been with me for two days. I'm using pure functional components, I found the following example, but when I press the button, I only get a blank sheet in the print preview, I tried to do it with classes, but it doesn't give me either, I don't know if I'm doing something wrong , all this in React JS, my files have a .jsx extension, I'm not sure what the problem is, I've already looked for too many solutions and I can't find what I'm doing wrong, thanks.

I'm using: "react": "^ 16.8.6", "react-to-print": "^ 2.13.0"

import React, { useRef } from "react";
import ReactToPrint from "react-to-print";

const ComponentToPrint = React.forwardRef((props, ref) => (
      <table ref={ref}>
        <thead>
          <th>column 1</th>
          <th>column 2</th>
          <th>column 3</th>
        </thead>
        <tbody>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
        </tbody>
      </table>
));

const Example = () => {
    const componentRef = useRef();
    return (
      <div>
        <ReactToPrint
          trigger={() => <a href="#">Print this out!</a>}
          content={() => componentRef.current}
        />
        <ComponentToPrint ref={componentRef} />
      </div>
    );
}

export default Example;
@lizarragadev
Copy link
Author

This is the view when I press print...

Captura de Pantalla 2021-08-24 a la(s) 18 17 24

@philhaynes337
Copy link

I am currently getting the same issue with my project. Found anything yet?

@lizarragadev
Copy link
Author

Hi @philhaynes337 I still couldn't find a solution. Clicking on the button opens the print window with the sheet and everything but everything blank, I tried with a lot of content in the table, static data just to test, but also, the strange thing is that several pages are put, since it detects that it is a lot of content but still everything in white.
I think there is a problem in:

const componentRef = useRef ();

since in another example using a Calendar in full screen, the view was not complete, and I noticed that I didn't grab the value of useRef, but when somehow something in the view was modified or there was a small status update, with some button or something, it was just updated and everything was displayed normal.
Hopefully someone can give us a hand with the problem.

@MatthewHerbst
Copy link
Owner

Hello. I've run into problems using useRef and forwardRef before as well, though in theory it should work. Are you able to convert ComponentToPrint to a class component by chance to see if it works that way? Then you would know if it was an issue with forwardRef or not

@lizarragadev
Copy link
Author

Hi @MatthewHerbst , here is a fragment of code in class, I also tried it and it doesn't work, maybe you will have more luck and tell us how.

class ComponentToPrint extends React.PureComponent {
  render() {
    return (
      <table>
        <thead>
          <th>column 1</th>
          <th>column 2</th>
          <th>column 3</th>
        </thead>
        <tbody>
          <tr>
            <td>data 1</td>
            <td>data 2</td>
            <td>data 3</td>
          </tr>
        </tbody>
      </table>
    );
  }
}

@philhaynes337
Copy link

philhaynes337 commented Sep 4, 2021 via email

@jncoates
Copy link

Hi @philhaynes337 and @MatthewHerbst, I am experiencing the same issue were you able to find a solution?

@babin93
Copy link

babin93 commented Oct 13, 2021

Same issue here, The only way I can see something in the print preview is by setting copyStyles={false} which gives an ugly rendering of my content.

Edit:

I had an import of a specifc css file in my js file. By removing that import it started working properly.

@MatthewHerbst
Copy link
Owner

@babin93 could you explain that import a bit more please? Might help others here

@babin93
Copy link

babin93 commented Oct 15, 2021

Alright, the styling issue was actually related to this problem: #11 (comment) (mostly a lack of understanding on my side)

For the blank page:

I had this import in my file
import 'react-toastify/dist/ReactToastify.css'; (not sure why this was there)

By removing it, there was no longer a STYLE tags in my page's HEAD so everything started working properly even with copyStyles={true}.

Before removing this import, I was only able to seem something when I would set copyStyles={false}.

Not sure this will help a lot...

@MatthewHerbst
Copy link
Owner

Ok, thanks for the explanation @babin93! I'll see about adding something to the README regarding #11 (comment)

@gaisuke
Copy link

gaisuke commented Nov 10, 2021

i'm still experiencing this issue, is anyone had an update to this? my code looks exactly like @lizarragadev . Thanks in advance.

Update 2 hours after i commented this :
turns out mine is already showed, but because of my component is white text, it shows a blank preview which is actually there is text on it with white color. so i add copyStyles={false} prop to negate the css

@MatthewHerbst
Copy link
Owner

I added a note about this issue to the Common Pitfalls section of the README, hopefully that helps others in the future. Thanks all

@fosteman
Copy link

fosteman commented Jan 10, 2022

Having the same issue.

Exactly the same setup as of @lizarragadev, created #455

@dennemark
Copy link

Custom-font might not be loaded correctly. Using font-display: swap in @font-face might solve this: #281 (comment)
At least it worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants