Giter VIP home page Giter VIP logo

Comments (7)

MatthewHerbst avatar MatthewHerbst commented on July 19, 2024 1

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.

from react-to-print.

Richi2293 avatar Richi2293 commented on July 19, 2024 1

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

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 19, 2024

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

from react-to-print.

Richi2293 avatar Richi2293 commented on July 19, 2024

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

exactly

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 19, 2024

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

from react-to-print.

Richi2293 avatar Richi2293 commented on July 19, 2024

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)}>

from react-to-print.

MatthewHerbst avatar MatthewHerbst commented on July 19, 2024

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

from react-to-print.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.