简体   繁体   English

如何在页面渲染期间更改颜色 SVG?

[英]How to change the colour SVG during Page Render ?

First of all I know how to change the color of SVG dynamically.首先我知道如何动态改变SVG的颜色。 I have used the solution from this question:我已经使用了这个问题的解决方案:

jQuery SVG image replacement jQuery SVG 图像替换

But my scenario is different.但我的情况不同。

On my page I have several icons (SVGs) and there is a color picker.在我的页面上,我有几个图标 (SVG) 并且有一个颜色选择器。 The user can change the of icons using colorpicker.用户可以使用颜色选择器更改图标。 All works fine.一切正常。

Now I want to generate the PDF of the whole page with the color of icons selected by the user.现在我想用用户选择的图标颜色生成整个页面的 PDF。

I am using third party PDF generation library that takes my page URL and generates the PDF of that.我正在使用第三方 PDF 生成库,它获取我的页面 URL 并生成它的 PDF。 But the problem is that the color does not get applied to the icons and PDF is generated.但问题是颜色没有应用于图标并生成 PDF。 I know the cause of this problem because as on document ready I convert all the SVG img tags to actual SVG tags and then apply the color to them chosen by the user.我知道这个问题的原因,因为在准备好文档时,我将所有 SVG img 标签转换为实际的 SVG 标签,然后将颜色应用于用户选择的它们。 This all happens after the page has rendered.这一切都发生在页面呈现之后。

But the PDF generation library does not wait for this process to complete and generates the PDF before all the SVGs images are converted to SVGs and color is applied to them.但是 PDF 生成库不会等待此过程完成,而是在所有 SVG 图像转换为 SVG 并对其应用颜色之前生成 PDF。

I don't know to first wait for whole process to complete and then generate its PDF.我不知道要先等待整个过程完成,然后再生成它的 PDF。

I have tried caching of the page but that does not help either.我曾尝试缓存页面,但这也无济于事。

Any help would be highly appreciated.任何帮助将不胜感激。 Thanks谢谢

I did that.我就是这么做的。

The solution was to wrap the code in IIFE so that it executes immediately on page load rather than on ready event of the DOM.解决方案是将代码包装在 IIFE 中,以便它在页面加载时立即执行,而不是在 DOM 的就绪事件时执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM