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

Always call on after print #490

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,22 @@ const componentRef = useRef(null);

## FAQ

### Can `react-to-print` print a PDF?

We simply open the browser's print preview window, so we aren't able to print a PDF as we lose control once the print preview window opens. However, it should be very easy to use `react-to-print` to take the information you need an pass it to a library that can generate a PDF.

```tsx
const handlePrint = useReactToPrint({
...,
print: async (printIframe: HTMLIframeElement) => {
// Do whatever you want here, including asynchronous work
await generateAndSavePDF(printIframe);
},
});
```

For examples of how others have done this, see [#484](https://github.com/gregnb/react-to-print/issues/484)

### Can the `ComponentToPrint` be a functional component?

Yes, but only if you wrap it with [`React.forwardRef`](https://reactjs.org/docs/forwarding-refs.html). `react-to-print` relies on refs to grab the underlying DOM representation of the component, and functional components [cannot take refs by default](https://reactjs.org/docs/refs-and-the-dom.html#accessing-refs).
Expand Down
7 changes: 4 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ export default class ReactToPrint extends React.Component<IReactToPrintProps> {
}
}

if (onAfterPrint) {
onAfterPrint();
}
} else {
// Some browsers, such as Firefox Android, do not support printing at all
// https://developer.mozilla.org/en-US/docs/Web/API/Window/print
this.logMessages(["Printing for this browser is not currently possible: the browser does not have a `print` method available for iframes."]);
}

if (onAfterPrint) {
onAfterPrint();
}

this.handleRemoveIframe();
} else {
this.logMessages(["Printing failed because the `contentWindow` of the print iframe did not load. This is possibly an error with `react-to-print`. Please file an issue: https://github.com/gregnb/react-to-print/issues/"]);
Expand Down