Comments (7)
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.
perfect thanks, I used a logic following your example and it worked
from react-to-print.
To be clear, you mean add an image to the printed output that isn't on the page normally?
from react-to-print.
To be clear, you mean add an image to the printed output that isn't on the page normally?
exactly
from react-to-print.
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.
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.
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)
- codesandbox examples are not loading HOT 2
- onAfterPrint is called before print iframe appears HOT 1
- On mobile devices are not truncated accordingly ref HOT 1
- Add Border to every Page HOT 4
- Add dynamic colors to print function HOT 1
- How to check that the user click on Save in `onAfterPrint`? HOT 2
- getting Supplied Data is not a valid base64-String jsPDF.convertStringToImageData erorr while directly downloading image HOT 1
- Border top and bottom HOT 1
- Save as PDF hangs in print view HOT 1
- Blank PDF when trying to generate in production. HOT 2
- How should i handle large data to print? HOT 8
- Why does it render different content when in im browsing in mobile HOT 3
- Blank Page while printing after a certain period of time HOT 9
- How to add style using PageStyle prop, without it overriding component’s style HOT 5
- Warning: findDOMNode is deprecated outside StrictMode. HOT 10
- Unhandled error when trying to print not fully loaded canvas elements HOT 3
- HTML doctype declaration is displaying on top of the print dialog window HOT 10
- Print datatables HOT 4
- CSS "break-before" (replacing "page-break-before") not supported HOT 3
- '.brf' file : Change the save directory. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-to-print.