Convert a HTML file into PDF in ReactJS - reactjs

I need to download a HTML file in PDF format. I tried some plugins but that doesn't work. I need help.
import React from 'react';
class App extends React.Component {
htmlToPdf()
{
//code need to be written
}
render() {
return (
<div>
<button onClick={this.htmlToPdf}>download</button>
</div>
);
}
}
export default App;

Related

ReactJS display return value from component class that extends React.Component

I have a component that I want to show in another site.
components/Hello.js
import React from 'react';
export default class Hello extends React.Component {
render() {
return (
<div><p>Hello</p></div>
)
};
};
Now I want to import it into my site named "Profile.js".
pages/Profile.js
import React from 'react';
import Hello from '../components/Hello';
export default function Profile() {
return(
<div>
{/* Say hello*/}
{Hello}
</div>
);
}
What am I doing wrong? Why wont it say hello on my "Profile.js" page?
React syntax for rendering components is as follow :
export default function Profile() {
return(
<div>
{/* Say hello*/}
<Hello/>
</div>
);
}

Not importing class

I'm new in React Js don't judge me bad for it. I was writing some class to learn but unfortunately I can't export and import one of my class.
import React from "react";
class Yozuvla extends React.Component{
render(){
return(
<div>
<p>Ismingiz</p>
<p>Familyagiz</p>
</div>
);
}
}
export default Yozuvla;
Here is the second class which can't be imported
import React from "react";
import yozuv from "./components/Form";
class App extends React.Component{
render(){
return (
<div>
<yozuv/>
</div>
);
}
}
export default App;
yozuv class don't show up why.browser is white like snow
Classname should be in PascalCase,
import yozuv from "./components/Form";
this should be
import Yozuv from "./components/Form";
Usage
<Yozuv/>
When you add any element like <yozuv/> (starts with small letter), react takes it as a normal HTML tag and NOT a React Component.
import React, { Component } from 'react'
// make sure the location of file is the same for Yozuvla
import Yozuvla from'./components/Form'
class App extends Component {
render() {
return (
<div>
<Yozuvla/>
</div>
)
}
}
export default App;
tell me in commit it work or not

Include header html file in Reactjs

I have header.htm and footer.htm files and I would like to add these two files into my reactjs app. I tried to create header.htm as a component and render it in my app.js but it display as a string on the top not a page. ie. http://mydomain/include/header.htm. How do I solve this problem?
import React, { Component } from 'react';
class Header extends Component {
createMarkup()
{
return { __html: "https://mydomain/include/header.htm"};
}
render(){
return (
<div dangerouslySetInnerHTML={this.createMarkup()} ></div>
);
}
}
export default Header;
app.js
class App extends Component {
render(){
return (
<Header />
)
}
}

React - Inline CSS and Image won't load

I'm still new to React and after I finished the tutorial I wanted to create something myself.
I've passed the url to make a backgroundImage style and add borders to try it out. Unfortunately I can see the img being set in the css but it won't actually display as expected.
As a test I also tried a simple img with src attribute set to my image and nothing would display.
I made a folder called img inside the src
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<Home/>
</div>
);
}
}
function Portfolio(props) {
const stylee = {
backgroundImage:'url('+props.imgURL+')',
borderWidth: 1,
};
const ide = 'portfolio'+props.counting;
return (<div id={ide} style={stylee} className='portfolio'/>)
}
class Home extends Component {
renderPortfolio(count,img){
return <Portfolio counting={count} imgURL={img}/>
}
render(){
return(
<div>
{this.renderPortfolio(1,'./img/1.jpeg')}
{this.renderPortfolio(2,'./img/2.jpeg')}
{this.renderPortfolio(3,'./img/1.jpeg')}
<img src='./img/1.jpeg'/>
</div>
)
}
}
export default App;
What am I doing wrong that is preventing the images from displaying ?
edit-- I've changed my code to the working solution
Instead of passing the local img url, I used require() first then pass the image to props.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<Home/>
</div>
);
}
}
function Portfolio(props) {
const counter = 'portfolio'+props.counting;
const loadGambar = props.gmbr;
// const loadGambar2 = require(props.gmbr);
const stylee = {
borderColor:props.color,
border:1,
borderWidth: 10,
backgroundImage: "url("+loadGambar+")",
}
return(
<div id={counter} className='portfolio' style={stylee}>
</div>
)
}
class Home extends Component {
renderPortfolio(count,gambare){
return <Portfolio counting={count} gmbr={gambare}/>
}
render(){
return(
<div>
{this.renderPortfolio(1,require('./img/1.jpg))}
{this.renderPortfolio(2,require('./img/1.jpg'))}
{this.renderPortfolio(3,require('./img/2.jpg'))}
</div>
)
}
}
export default App;
Here's a link to a working example based on your code: sandbox link.
The problem with setting a background image is that it adapts to the dimensions of the div. In your case your div is empty in Portfolio so its dimensions are 0x0 and the image won't show. You will see from the link that I update the style and everything shows. Now the only issue you might have is that you don't have an image loader in your webpack configuration, if you are starting with react I would recommend create-react-app.

React Component Image Issue

My image Component won't seem to actually put the image in, the URL is correct as I can add it by physically adding the HTML into the page.
The component seems to be failing. I tried what I found at http://facebook.github.io/react/docs/jsx-in-depth.html#attribute-expressions
but it did not help.
export default class mainLogo extends React.Component {
render() {
return (
<div>
<img src = {'/src/app/assets/images/home/mainLogo.png'}/>
</div>
);
}
}
The class name should start with a capital letter (MainLogo) and you shouldn't use {} on the src, because you are sending the img url directly to the src. It's not a variable or state.
This should work:
export default class MainLogo extends React.Component {
render() {
return (
<div>
<img src='/src/app/assets/images/home/mainLogo.png' />
</div>
);
}
}

Resources