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

it's possible to add some code before the content? #69

Closed
Richi2293 opened this issue Nov 9, 2018 · 7 comments
Closed

it's possible to add some code before the content? #69

Richi2293 opened this issue Nov 9, 2018 · 7 comments

Comments

@Richi2293
Copy link

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

@MatthewHerbst
Copy link
Owner

To be clear, you mean add an image to the printed output that isn't on the page normally?

@Richi2293
Copy link
Author

To be clear, you mean add an image to the printed output that isn't on the page normally?

exactly

@MatthewHerbst
Copy link
Owner

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

@Richi2293
Copy link
Author

Okok, like this:

<ReactToPrint
    trigger={() => <Button color="primary" style={styles.button}>Print</Button>}
     content={() => this.createContentPrint()}
/>

And this is the method 'createContentPrint':

createContentPrint() {
        var content = (<img src={'https://imageExample.png'}></img>);
        return(content+this.componentRef);
}

The componentRef is like this in a div in the page:
<div ref={el => (this.componentRef = el)}>

@MatthewHerbst
Copy link
Owner

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 content prop.

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.

@Richi2293
Copy link
Author

perfect thanks, I used a logic following your example and it worked

@MatthewHerbst
Copy link
Owner

Glad it worked out! If you wouldn't mind, could you please close this issue if all of your question has been addressed? Thanks!

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