-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Image is often missing on first render on Safari iOS #343
Comments
same problem! |
My fix was to stop using |
It was working fine with me on chrome, but on iphone usually it miss some pictures. Try it from your side and let me know how it goes with you 👍 domtoimage.toPng(element)
const doc = new jsPDF("p", "mm", "a4");
doc.addImage(page, 'JPEG', 0, 0, 210, 297); |
Update: |
Heey I think I workaround it -not 100% sure yet- 😂 I tried to run the converter function couple of times then take the last page. function generateImage() {
return new Promise(resolve => {
const doc = new jsPDF("p", "mm", "a4");
const node = this.$refs.content;
domtoimage.toPng(node)
.then((page) => {
doc.addImage(page, 'JPEG', 0, 0, 210, 297);
return resolve(doc);
})
})
} And use it like this: generateImage().then(() => {
console.log("first time")
generateImage().then(() => {
console.log("one more time to make sure ")
generateImage().then((doc) => {
console.log("okay now it should be fine")
doc.save(`my_great_file.pdf`)
})
})
}) I just pushed this to our production, I hope it works fine with me. |
I really recommend just using |
I've just stopped using this because it's very unreliable on how it processes the image, text is weird and it's not supported on safari it seems. Shame.
I've just leveraged this package in a similar way by calling it once, then a second time before returning the image to the user. Seems to work this way... however I don't expect this package to be updated any time soon so it'll do for now... 😬 |
Did not work for me... 😞. maybe some one find another solution? |
domtoimage(node).then(dataUrl => { |
Running domtoimage multiple times does not work with ios 13 |
Still no solutions? When I build my ionic app it doesn't work the first time, second time works flawlessly. It makes no sense. There are no errors, I tried with try catch, no error neither... Idk |
This is a serious issue and needs to be addressed :( Stuck there.... |
html2canvas |
For us it didn't work the same way, like the image was rescaled or something strange. We finally ended up launching 2 times the app at the very first time, hahaha I know... |
@eddsaura did you manage to solve the issue? Please share the approach with us as well :) |
Same problem! Seems to happen only on Safari on MacOS (not iOS) |
The problem occurs on iOS as well. |
@hyl1374961656 |
any solution for this issue? html2canvas not support ssr so i have to search other way using domtoimage |
Html2canvas works for me. I have done extensive testing.
…On Wed, Jul 21, 2021, 9:47 PM ihoment-chentao ***@***.***> wrote:
any solution for this issue? html2canvas not support ssr so i have to
search other way using domtoimage
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#343 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABOBID2LPZ4QJUV2U4Q4CULTY52B5ANCNFSM4OIMPODA>
.
|
I ended up making a "disposable" first render with dom-to-image that I don't use basically. The second render then works. |
Hi, i try your answer,it`s working! But, do you know why it working? |
Hey Everyone! Just posting that, yes, this is still an issue with Safari (15.1) and, yes, the workaround is to run domtoimage twice. Here's how I'm doing it: domtoimage.toJpeg(imageCard, {
width: 1080,
height: 1080,
quality: 0.5,
}).then(dataUrl => {
domtoimage
.toJpeg(imageCard, {
width: 1080,
height: 1080,
quality: 0.5,
})
.then(dataUrl2 => {
//Do your all stuff with dataUrl2...
});
}); Interestingly, I only had the missing images problem on the dataUrl that I upload to my API. Bafflingly, all images generated for local download are fine (domtoimage only needs to run once.) This seems to be only a Safari issue. Based on the suggestions in this thread, II did try html2canvas when I first encountered the missing images issue, but I don't recommend it: it's super-buggy, doesn't seem to like linear gradients, and I could not get a single image rendering with my setup. Maybe it will work for you? I can live with the above workaround for now 😄. |
@kittykatattack i had the same problem with iOS but your answer is correct i try this pice of code in my service and it works! Thanks |
As described in bug at tsayen/dom-to-image#343
Okay so after much testing I was finally able to solve this. Apparently, the images that were coming from the server were not rendering in safari when converting dom to the image. So after many tries, I came up with the solution of converting the image URL to base64 then using that string to render the dom element i want the image of. After that everything was local to that dom element which then solved my problem
|
To convert images to base64 doesn't solve,. Still safari doesn't create images on first render. I need to create canvas there are how many images in element, times. If images from external domain, it works fine. When images from local server, it needs to create many times. Issue occurs on ipad and ios and macos. It's serious problem. There must be a solution... |
even double rendering doesnt work now ,someone create a proper fix without double rendering because the time consumption is too long . |
Double rendering stopped working at some stage. Tried html2canvas but it doesn't work will with transform, rotate, overflow: 'hidden' and SVG. So all renders are really messy. Still looking for a solution. Only occurring on Safari and iOS. |
Calling |
I recently started using the library and came up with this same error. The solution can be:
The process is a little longer than using the built in functions of the library but results are a lot more consistent. Code example: domtoimage.toSvg($element).then(async (svgData) => { // Call the .toSvg function
let $svgImg = new Image(); // Svg container img
$svgImg.src = svgData; // Set the svg data as src
const $canvas = document.createElement('canvas'); // Create a rendering canvas
$canvas.width = $element.offsetWidth; // Set canvas width same as svg image
$canvas.height = $element.offsetHeight; // Set canvas height same as svg image
var ctx = $canvas.getContext("2d");
document.appendChild($svgImg); // Append svg to the DOM for rendering
$svgImg.onclick = async function () { // Wait for svg to render then click it
ctx.drawImage($svgImg, 0, 0); // Draw the fully rendered svg to the canvas
// Use canvas functionality to export its data (in this case to a png file)
const dataUrl = $canvas.toDataURL();
// Do what you need with the data
let $pngImg = new Image();
$pngImg.src = dataUrl;
document.appendChild($pngImg);
};
}); I know this doesn't satisfy every use case, but if you can wait for a PREVIEW svg and then perform the conversion this can help. |
this worked well for me...Thank you so much!! |
I use this way to solve the problem; only when all the images are loaded, and then use the API of toJpeg to create the canvas; I also try to html2canvas, even though it solved the problem of the image blank, because the library directly renders the image to canvas, not like the dom-to-image first converts the HTML element to SVG, and then converts canvas; // The total num of needed load images
const totalLoadImgCount = useRef(0)
// The num of loaded images
const loadSuccessImgCount = useRef(0)
const createImgUrl = async () => {
await domtoimage.toJpeg(document.getElementById('share-dom') as HTMLElement)
const imgUrl = await domtoimage.toJpeg(
document.getElementById('share-dom') as HTMLElement
)
xxxxx
}
// When all images are loaded, then to convert the canvas
const onLoadImage = () => {
loadSuccessImgCount.current++
if (loadSuccessImgCount.current === totalLoadImgCount.current) {
createImgUrl()
}
}
//Count the totalLoadImgCount
useEffect(() => {
options?.extends?.imgUrl && totalLoadImgCount.current++
options?.extends?.initImageUrl && totalLoadImgCount.current++
}, [options])
return
(<div id="share-dom">
<img
className="w-full"
src={toXSmallImage(options?.extends?.imgUrl as string)}
onLoad={() => {
onLoadImage()
}}
/>
<img
className="relative w-full"
id="initImageUrl"
src={toXSmallImage(options?.extends?.initImageUrl as string)}
onLoad={() => {
onLoadImage()
}}
/>
</div>) |
I have similar issue in safari when the url is like:
it renders normally in the browser, but when downloading the image disappears, but in chrome it works perfectly, btw do not replace it with html2canvas, this library is far far superior. html2canvas is full of bugs and barely renders CSS properties. this library has much better compatibility with CSS styles than html2canvas. @sulmanazhar2 Thanks! It works gracefully! |
My solutions is adding the target dom width/height:
|
草你他妈简直是天才 |
let dataUrl = await domtoimage.toPng(node, params)
for (let i = 0; i < 10; i++) {
// When a blank background appears, the image size will be much smaller than before. Find a value that can determine whether the generation was successful
if (dataUrl.length / 1024 > 200) {
break
} else {
dataUrl = await domtoimage.toPng(node, params)
}
} |
兄弟,真牛逼,我也是连续生成然后双数index的就可以,没想到居然可以这么解决 |
if not works on ios, please try this
this code solved my problem |
你好,我已收到你的来信。辛苦了!!
|
This was a GREAT approach |
I'm using
domtoimage.toPng(element)
whereelement
contains a few different elements, including a .png img that has partial transparency.That img is missing from the resulting data URL on the first call on iPhones. Subsequent calls do include the img. There are two other img inside
element
that always show up in everytoPng
result, including the first one. Those other .pngs have transparency as well, but it isn't partial - they are either fully opaque text on a fully transparent background.The text was updated successfully, but these errors were encountered: