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

Conditionally render an element #113

Closed
aaronzhongg opened this issue Jan 29, 2024 · 2 comments
Closed

Conditionally render an element #113

aaronzhongg opened this issue Jan 29, 2024 · 2 comments

Comments

@aaronzhongg
Copy link

aaronzhongg commented Jan 29, 2024

Thank you @ivmarcos for creating this library - it's been a pleasure using it without having to write a whole lot of separate code.

I'm trying to conditionally render an element when I'm printing which is otherwise not displayed.

Here's an example for a watermark that I don't want to render on the screen, but want to show in the pdf:
https://codesandbox.io/p/sandbox/fervent-brown-2j55ld

Maybe TMI but my use case isn't exactly a watermark, it's a little bit more complex but I hope it represents the problem well!

Thanks

@ivmarcos
Copy link
Owner

ivmarcos commented Jan 30, 2024

Hey @aaronzhongg I believe you can use the onclone callback to do that.

from the html2canvas docs: https://html2canvas.hertzen.com/configuration

Callback function which is called when the Document has been cloned for rendering, can be used to modify the contents that will be rendered without affecting the original source documen

See also: niklasvh/html2canvas#701 (comment)

import { Options } from 'react-to-pdf'
...
const options: Options = {
    overrides: {
      canvas: {
        onclone: (document) => {
          document.getElementById("elementId")!.classList.toggle('visible')
        },
      },
    },
  };

@aaronzhongg
Copy link
Author

Thanks @ivmarcos that works well!

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

No branches or pull requests

2 participants