简体   繁体   English

SVG到PNG使用javascript保留CSS

[英]SVG to PNG retaining CSS using javascript

This is a bit of a long shot, but does anyone know of a tool that can export a png from an svg input AND retain the CSS styles applied to it. 这是一个很长的镜头,但是有人知道一个工具可以从svg输入中导出png并保留应用于它的CSS样式。 I have used canvg, but everything appears black in the output, as the styles are kept in css and not part of the svg document. 我使用了canvg,但输出中的所有内容都显示为黑色,因为样式保存在css中而不是svg文档的一部分。

The solution is web based, and i'd like to perform the conversion client side using a javascript solution if possible. 解决方案是基于Web的,如果可能的话,我想使用javascript解决方案执行转换客户端。

Update: 更新:

I understand that you are using an external stylesheet for your SVG markup. 我知道您使用外部样式表作为SVG标记。 So I think you need a three-step solution: 所以我认为你需要一个三步解决方案:

  1. Make the stylesheet declarations that apply to the SVG markup inline. 使内联适用于SVG标记的样式表声明。 This is done best client-side. 这是最好的客户端。 I do not have a solution in source code right now, but it should be possible to use W3C DOM Level 2 Style interface implementations to find out the selectors that apply to an element, and the declarations that have been used in the corresponding blocks ( document.defaultView.getComputedStyle() alone will probably result in an SVG fragment having too many inline declarations). 我现在没有源代码的解决方案,但应该可以使用W3C DOM Level 2 Style接口实现来找出适用于元素的选择器,以及已在相应块中使用的声明( document.defaultView.getComputedStyle()可能会导致SVG片段具有太多的内联声明)。
  2. Convert SVG markup with inline stylesheets to PNG. 将带有内联样式表的SVG标记转换为PNG。 This is best done server-side (eg, with ImageMagick), so you would need to send the SVG markup to the server. 这最好在服务器端完成(例如,使用ImageMagick),因此您需要将SVG标记发送到服务器。
  3. Serve the PNG resource to the user. 将PNG资源提供给用户。

Those two steps could be performed on form submission where in the onsubmit attribute you do step #1 and then call the form's submit() method. 这两个步骤可以在表单提交上执行,在onsubmit属性中执行步骤#1,然后调用表单的submit()方法。

@pluke, you've asked in the comments of the reply of @PointedEars for source code to turn external CSS styling into inline styles for your SVG. @pluke,您已经在@PointedEars的回复评论中询问了源代码,以便将外部CSS样式转换为SVG的内联样式。 I've spent hours of searching for such a tool myself, and found none. 我自己花了几个小时搜索这样一个工具,但没找到。 However, I've discovered a quite specific solution that applies SVG generated with Rickshaw / D3: @thirdcreed has posted the JavaScript it at: Rickshaw CSS/Axes in JSDOM - just adapt those D3 specific selectors to your custom CSS / SVG elements as needed. 但是,我发现了一个非常具体的解决方案,它应用了用Rickshaw / D3生成的SVG:@thirdcreed已将JavaScript发布在:JSDOM中的Rickshaw CSS / Axes - 只需根据需要将这些D3特定选择器调整为自定义CSS / SVG元素。

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

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