MatthewHerbst / react-to-print

Print React components in the browser. Supports Chrome, Safari, Firefox and EDGE
MIT License
2.06k stars 221 forks source link

DATA OUTSIDE TABLE IS NOT PRINTED OR VIEWED IN PRINT PREVIEW #648

Closed Trikcode closed 12 months ago

Trikcode commented 12 months ago

When i print, data inside the table is displayed in the print preview but data out side is not for example this line is not displayed <h2 style={{ color: "green" }}>Attendance</h2>

class ComponentToPrint extends React.Component {
  // get props
  render() {
    return (
      <div>
        <h2 style={{ color: "green" }}>Attendance</h2>
        <table>
          <thead>
            <th>S/N</th>
            <th>Name</th>
            <th>Email</th>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td>Njoku Samson</td>
              <td>samson@yahoo.com</td>
            </tr>
            <tr>
              <td>2</td>
              <td>Ebere Plenty</td>
              <td>ebere@gmail.com</td>
            </tr>
            <tr>
              <td>3</td>
              <td>Undefined</td>
              <td>No Email</td>
            </tr>
          </tbody>
        </table>
      </div>
    )
  }
}
 <div style={{ display: "none" }}>
  <ComponentToPrint
    ref={el => (componentRef = el)}
  />
</div>
<img width="725" alt="attach" src="https://github.com/gregnb/react-to-print/assets/73602565/a4b0057c-ae2a-43da-98e7-b0a00b3923bd">
siaikin commented 12 months ago

Hello, could you please complete the code snippet? I can only find limited information to identify the problem from this incomplete code snippet.

Of course, it would be best to provide a Codesandbox or stackblitz example.

Trikcode commented 12 months ago

Here is the code distribution in my two components pdf

Trikcode commented 12 months ago

only data contained in the Table component is displayed on print preview, for the first screenshot i shared (i was testing on small table data and it was doing the same)

Trikcode commented 12 months ago
new capt
Trikcode commented 12 months ago

i don't think this was the best fix but it worked for me (wraping the component in a table tag

`

      </div>`
siaikin commented 12 months ago

Hi @Trikcode

I suspect that there is CSS in your project that hides elements that are not wrapped in a table element when printing. You can enable print preview mode in Chrome's console to help identify the problem (https://stackoverflow.com/a/29962072).