DOM node to image
Unanswered
Brown bear posted this in #help-forum
Brown bearOP
Does anyone happen to have a reliable way to do this? I've tried a custom solution as well as
Thanks.
html-to-image package, however, it seems that regardless of the given element's dimensions it completely cuts off the right side of the element and i'm not sure what could be causing this other than perhaps it's somehow out of view-port?Thanks.
9 Replies
@Brown bear Does anyone happen to have a reliable way to do this? I've tried a custom solution as well as `html-to-image` package, however, it seems that regardless of the given element's dimensions it completely cuts off the right side of the element and i'm not sure what could be causing this other than perhaps it's somehow out of view-port?
Thanks.
try using html2canvas. I haven't seen the package properly.
You can get canvas then convert to dataURL and/or save it
You can get canvas then convert to dataURL and/or save it
@Anay-208 try using html2canvas. I haven't seen the package properly.
You can get canvas then convert to dataURL and/or save it
Brown bearOP
I’ve unfortunately tried both, it cuts off half of my element and I’m not sure if it’s a DOM limitation? Visually on the browser the element is exactly how I want it
@Anay-208 Can you send your code related wit this?
Brown bearOP
const ref = useRef<HTMLDivElement>(null)
const onButtonClick = useCallback(() => {
if (ref.current === null) {
return
}
toPng(ref.current, { cacheBust: true, pixelRatio: 1, skipAutoScale: true,})
.then((dataUrl) => {
const link = document.createElement('a')
link.download = 'hero.png'
link.href = dataUrl
link.click()
console.log('data url', dataUrl)
})
.catch((err) => {
console.log(err)
console.log('ref', ref.current)
})
}, [ref])
<div ref={ref} className="container">
<ComponentToRender />
</div>
<button onClick={onButtonClick}>Click Me</button>@Brown bear ts
const ref = useRef<HTMLDivElement>(null)
const onButtonClick = useCallback(() => {
if (ref.current === null) {
return
}
toPng(ref.current, { cacheBust: true, pixelRatio: 1, skipAutoScale: true,})
.then((dataUrl) => {
const link = document.createElement('a')
link.download = 'hero.png'
link.href = dataUrl
link.click()
console.log('data url', dataUrl)
})
.catch((err) => {
console.log(err)
console.log('ref', ref.current)
})
}, [ref])
<div ref={ref} className="container">
<ComponentToRender />
</div>
<button onClick={onButtonClick}>Click Me</button>
set the parent div width and height to 100%, or even try to increase it further
And check in chrome Dev tools if parent div is covering all the children
@Anay-208 set the parent div width and height to 100%, or even try to increase it further
Brown bearOP
have tried this as well and it still cuts off for some reason, but it renders perfectly fine when I'm on the page 

@Anay-208 Try using the body Element for it
Brown bearOP
I ended up getting it working, I instead opted for an API route that sets up puppeteer and uses an element selector to take a screenshot, works great