You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I’m currently working on a project where I need to render dynamic images using the react-pdf/renderer library. My initial approach was to use SVG images, but I found that I cannot render different images via url inside SVG components.
To work around this, I decided to convert SVG images to PNG format on-the-fly (cloudinary, images stored there) and then render them using the Image component from react-pdf/renderer.
Next, I attempted to create a buffer from the PNG image and then pass this buffer to the Image component. Again, I ran into the same issue.
Here’s an example of what’s happening:
Image looks like this:
But is shown like this:
here is my current component: (Image prop is simple url)
`import { Page, Text, StyleSheet, Image } from "@react-pdf/renderer"
import axios from "axios"
import { useEffect, useState } from "react"
I’m currently working on a project where I need to render dynamic images using the react-pdf/renderer library. My initial approach was to use SVG images, but I found that I cannot render different images via url inside SVG components.
To work around this, I decided to convert SVG images to PNG format on-the-fly (cloudinary, images stored there) and then render them using the Image component from react-pdf/renderer.
Next, I attempted to create a buffer from the PNG image and then pass this buffer to the Image component. Again, I ran into the same issue.
Here’s an example of what’s happening:
Image looks like this:
![real](https://private-user-images.githubusercontent.com/106894255/336820605-de09de19-8033-4280-a9f7-ccb67efd4552.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2ODE3NzAsIm5iZiI6MTcyMDY4MTQ3MCwicGF0aCI6Ii8xMDY4OTQyNTUvMzM2ODIwNjA1LWRlMDlkZTE5LTgwMzMtNDI4MC1hOWY3LWNjYjY3ZWZkNDU1Mi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcxMVQwNzA0MzBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT00Yzc3NTRmOGJiY2FiNGE2ODAxOWY4NTllNGFhYzcxMGFlY2RiNWEyZDVlYzY2NjVmNTY3NzhmMWZhOWQwMmFhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8vXl1Q2wJHQIVx7HLx_AZzTn19lAW2EylVVUKNGwZXI)
But is shown like this:
![issue](https://private-user-images.githubusercontent.com/106894255/336820269-bd05180c-69d3-4aab-81ee-114e93403deb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA2ODE3NzAsIm5iZiI6MTcyMDY4MTQ3MCwicGF0aCI6Ii8xMDY4OTQyNTUvMzM2ODIwMjY5LWJkMDUxODBjLTY5ZDMtNGFhYi04MWVlLTExNGU5MzQwM2RlYi5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcxMVQwNzA0MzBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05OWY1M2QwOTUzZTk0OTJmMjY2ZDcxYWZjODJiMzk5NjBlY2NlNTIyOTllNGU1ZWMwZWIwNzliM2QwNmM4NzZhJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.pj9iWj536VO1hXjxgbrXnfcW2EuqNtznline36_nV1M)
here is my current component: (Image prop is simple url)
`import { Page, Text, StyleSheet, Image } from "@react-pdf/renderer"
import axios from "axios"
import { useEffect, useState } from "react"
const styles = StyleSheet.create({
titlePage: {
flexDirection: "column",
backgroundColor: "#F3F4DA",
width: "210mm",
height: "285mm",
paddingLeft: "16mm",
paddingRight: "16mm",
paddingTop: "30mm",
paddingBottom: "30mm",
justifyContent: "space-between",
alignItems: "center",
},
bookTitle: {
fontSize: 48,
fontWeight: "bold",
fontFamily: "Amatic",
textAlign: "center",
},
chefName: {
fontSize: 24,
fontFamily: "Amatic",
},
image: {
width: "160mm",
height: "160mm",
objectFit: "fill",
},
})
const FirstCoverPage = ({
title = "Book Title",
chefName = "Name",
image,
coverColor,
textColor,
}) => {
const [convertedImage, setConvertedImage] = useState(null)
useEffect(() => {
async function getImageBuffer(url) {
const response = await axios.get(url, { responseType: "arraybuffer" })
const buffer = Buffer.from(response.data, "binary").toString("base64")
const bufferImage =
data:image/png;base64,${buffer}
setConvertedImage(bufferImage)
}
getImageBuffer(image)
}, [image])
return (
<Page style={{ ...styles.titlePage, backgroundColor:
#${coverColor}
}}><Text style={{ ...styles.bookTitle, color:
#${textColor}
}}>{title}{convertedImage && (
<Image alt="image" src={convertedImage || "/images/sundae-red.png"} style={styles.image} />
)}
)
}
export default FirstCoverPage
`
The text was updated successfully, but these errors were encountered: