Export React Page to PDF - reactjs

I'd like to be able to export React Page to PDF file(s). So far, I've tried jsPDF and html2canvas to capture the page as an image and save it as a pdf. Sample code:
const input = document.getElementById('exportToPDF')
window.scrollTo(0,0)
html2canvas(input)
.then((canvas) => {
document.body.appendChild(canvas)
const imgData = canvas.toDataURL('image/png')
const pdf = new jsPDF()
pdf.addImage(imgData, 'PNG', 0, 0)
pdf.save("test.pdf")
});
and 'exportToPDF' example:
<div id="exportToPDF">...</div>
I ran into problems with the canvas got cut off when the page content is too large/long. How can I get it to break into multiple pages when needed? It appears as it's limited to one page only.
What I have tried: set window width and height to html2canvas but it didn't help.
Update: I'm open to try other ways to export React page to PDF file(s) and not having to use html2canvas that are free.

Have you tried react-pdf or react-to-pdf these 2 might work for you if you aren't using next.js

I also faced same issue, now resolved by using #progress/kendo-react-pdf
visit https://www.telerik.com/kendo-react-ui/components/pdfprocessing/ for examples
sample code
import { PDFExport } from "#progress/kendo-react-pdf";
const ref: any = React.createRef();
...
<button onClick={() => {
if (ref.current) {
ref.current.save();
}
}}
>
Export PDF
</button>
<div id="container">
<PDFExport paperSize="A4" margin="0.5cm" ref={ref}>
<div id="htmldata" >sample data</div>
</PDFExport>
</div>
If you don't want to display contents of htmldata you can add below css
#container {
width: 0px;
height: 0px;
overflow: hidden;
}

Related

creating PDF black background instead of image via jsPDF in React js

Hi I am creating pdf with image background via jsPDF library. File creating successfully but making background black instead of image my code is this.
const exportBrocharPDF = () => {
let element = (
<div style={{ textAlign: "left", width:"600px"}}>
<div style={{ backgroundImage: `url("https://mentorlogix.com/pricebuilder/pdfimages/4.png")`}}>
<p style={{fontSize: "10px", color:"#000"}}>
Your investment in the maintenance package is just , billed directly to your credit card.
</p>
</div>
</div>
)
const doc = new jsPDF("p", "pt", "a4");
doc.html(ReactDOMServer.renderToString(element), {
callback: function (doc) {
doc.save('Brochar.pdf');
}
});
};
Result is like this
I am trying a lot but not success please anyone can help to resolve my issue

React Image - Local Image not showing

import styled from 'styled-components';
import bannerImage from '../img/bannerImage.jpg';
console.log(bannerImage);
const StyledBanner = styled.div`
margin-top: 55px;
width: 100%;
height: 20px;
background-color: red;
`;
function Banner() {
return (
<StyledBanner>
{/* <img src={`http://github.com/someprofile.png`} /> */}
<img src={require("../img/bannerImage.jpg")} />
<img src={bannerImage} />
</StyledBanner>
);
};
export default Banner;
I have this React Component that I am trying to return a local image, but this image never loads, it show the image is there but it is always broke.
I tried using require method and also the import but nothing works.
When it is an external image it works fine, only when it is local it doesnt work.
Could you please advise me how to correct this issue?
The final element that comes out of this is a DIV and those 2 images there appear as broken images, but if I change the src to a online picture it works fine.
<div class="sc-eDvSVe YFPxO">
<img src="[object Module]">
<img src="[object Object]">
</div>
I think the fix is going to be to use require("../img/bannerImage.jpg").default, because require returns an object with a default property that you have to access.
Example from an old project of mine:
If I set my state like this:
setProfileImg(require("../images/" + res.data.profileImg));
it has the value of this object:
{
"default": "/static/media/1640086164286.e03de477.png"
}
And so by using .default I access the actual image path I want.

Property 'initGradient' does not exist on type

I am building a website with react and typescript. I am having trouble implementing a gradient background that I found on -"https://kevinhufnagl.com/how-to-stripe-website-gradient-effect/".
When I initialise the gradient, I am getting an error "Property 'initGradient' does not exist on type 'Gradient'" The error is raised at 'initGradient' in the code below
This is the code I have written:
`
function App() {
<script src="./Gradient"></script>;
const gradient = new Gradient();
gradient.initGradient("#gradient-canvas");
return (
<div>
<canvas
id="gradient-canvas"
className="absolute w-50 h-50"
></canvas>
</div>
);
}
export default App;`
I have created a file 'Gradient.js' which has the code taken from the website linked above. I have also implemented the necessary styling in App.css
I have used the same code to create a website using standard HTML and CSS and was able to implement it correctly. However, I am now trying to build the site in React and Typescript. I have also used tailwind in this project.
Any help and advice would be greatly appreciated cause I've spent way too long trying to implement this! Thanks!
It looks like you are trying to import the Gradient.js file by using a script tag? If thats the case, you should use a module import.
import gradient from './Gradient'
function App() {
gradient.initGradient("#gradient-canvas");
return (
<div>
<canvas
id="gradient-canvas"
className="absolute w-50 h-50"
></canvas>
</div>
);
}
export default App;`
As for the gradient.initGradient("#gradient-canvas"); I would have to see the actual Gradient.js file to see if that would work.
I spent about 6 hours today using various mesh gradient implementations with React before I ended up with this
Gradient component:
useEffect(() => {
const canvasElement = document.getElementById("gradient-canvas");
const gradient: any = new Gradient();
if (canvasElement) {
gradient.initGradient("#gradient-canvas");
} else {
gradient.pause();
}
}, []);
CSS
canvas {
width: 100%;
height: 100%;
--gradient-color-1: #c3e4ff;
--gradient-color-2: #6ec3f4;
--gradient-color-3: #eae2ff;
--gradient-color-4: #b9beff;
}

How the javascript array display result with new line

Any hint or help would be greatly appreciated!!
In imageList.js, the following {images} is an array.
How can the return of an string "div" of an array return the below image:
return {images}
display this result like if there is a "\n" new line for each record?:
import React from 'react';
const ImageList = props => {
const images = props.images.map((image) => {
return <img src={image.urls.regular} />
});
console.log(props.images)
return <div>{images}</div>;
}
export default ImageList;
result:
for this you are creating img elements and depending on your css they are taking the entire container width. Solving this would require specifying image sizes for example
div{
display: grid;
grid-template-column: repeat(3, 1fr)
}
or
img{
width: 45%;
}
but in the end, it depends on your styling
Your images list is displayed correctly but you miss the css for it.
you can use the GridList or gird system of material ui
or you build your own design.
here is a sample
<GridList cellHeight={160} className={classes.gridList} cols={3}>
{imagess.map((image) => (
<GridListTile key={image.key} cols={image.cols || 1}>
<img src={image.img} alt={image.title} />
</GridListTile>
))} </GridList>
in the link below you can find more usefull information.

How to customize style of React Recaptcha?

I am using this package in my React application.
The problem is when running the application in a specific device Galaxy Fold, the container of selecting images is too big.
I need to change the size of the div with this id rc-imageselect.
Here is my code
<div className={classes.recapt} style={{ transform: 'scale(0.8, 0.8) translateX(10%)', display: "table" }}>
<Recaptcha
expiredCallback={() => setCaptchaCode(false)}
sitekey={localStorage.getItem("CAPTCHA_KEY")}
render="explicit"
onloadCallback={callback}
verifyCallback={verifyCallback}
/>
</div>
export default makeStyles((theme) => ({
recapt: {
"&div": {
"&#rc-imageselect": {
transform: 'scale(0.8, 0.8) translateX(10%)'
}
}
}
}));
But this doesn't work.
I changed it in te inspect panel and it works. So my guess I am selection the div in a wrong way.
Any help would be appreciated.
I think your style targeting has problem, try like this :
"& > div > image"
And i think u cannot using query (#elementID) on that .
Edit
After uploading photo : Take look at this

Resources