-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
it's possible to add some code before the content? #69
Comments
To be clear, you mean add an image to the printed output that isn't on the page normally? |
exactly |
Do you mind showing how you previously tried? I think I have an idea for how to do it but I want to make sure it's not what you already tried first. Thanks |
Okok, like this:
And this is the method 'createContentPrint':
The componentRef is like this in a div in the page: |
I think your issue is that you're not returning a ref - you're returning actual content. You need to return a ref in the So, something along the lines of: createContentPrint() {
return this.componentRef;
}
handleAfterPrint() {
// Remove the image from the DOM manually
const elem = document.querySelector('#myImage');
elem.parentNode.removeChild(elem);
}
handleBeforePrint() {
// Add the image to the DOM manually
const image = <img id="myImage" src={'https://imageExample.png'} />;
this.componentRef.getElementsByTagName('body').appendChild(image);
}
setRef(ref) {
this.componentRef = ref;
}
renderButton() {
return <Button color="primary" style={styles.button}>Print</Button>;
}
render() {
return (
<div>
<ReactToPrint
afterPrint={this.handleAfterPrint}
beforePrint={this.handleBeforePrint}
content={this.createContentPrint}
trigger={this.renderButton}
/>
<div ref={this.setRef}>
</div>
);
} I haven't tested if that works exactly, but that's the jist of what you need to do. |
perfect thanks, I used a logic following your example and it worked |
Glad it worked out! If you wouldn't mind, could you please close this issue if all of your question has been addressed? Thanks! |
I need to add an image at the beginning of the page to be printed, but only for the print page.
It's possibile?
I tried but I did not succeed.
Thanks,
Riccardo
The text was updated successfully, but these errors were encountered: