svg

利用XSLT和Java处理图片生成PDF
博主分享处理粘贴图片生成PDF的框架代码。先从含图片的文档生成XML,再创建XSL样式表处理XML,因标准XSLT无Base64解码器,需用Apache Xalan扩展其功能,将图片数据转换为PNG格式。输出使用XSL - FO,最后用Apache FOP将XML转换为PDF。
I got something working so I just thought I'd contribute the framework code of what I did to get there for anyone interested.

First off, I doubt this will ever work as a domino agent etc. The solution needs access to a Base64 encoder and decoder. I'm using the classes in the sun.misc.* package on sun's j2se 1.4.2_01, but these aren't distributed as a part of the supported java API. I don't think they are available as part of the domino JVM, but I haven't looked.


Step 1)
Generate the xml from a document(s) containing a pasted image in a richtext field. I'm using a notes 6.5 client to generate the xml. The code to do this looks something like:

DxlExporter exporter = session.createDxlExporter();
exporter.setConvertNotesBitmapsToGIF(true);
String dxl = exporter.exportDxl(collection);


Step 2)
Create a XSL stylesheet to process the xml. It traverses the domino items in the exported dxl to get to the binary gif file image dump. This data is base64 encoded. Now, I doubt that there is a base64 decoder available in standard XSLT so that leads us to step 3.


Step 3)
Extend the standard XSLT functionality. I'm using Apache Xalan for XSL transforming. You have to create a java class that takes the xml image data from the domino dxl and send it to your java class. My java class does the following:
- get input from caller
- base64 decode the input
- transform the result to png
- base64 encode the result
- return the encoded result to the XSLT caller
This actually isn't as hard as it may seem. It's looks something like this (comments welcome):

------------

String s;

BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();

ByteArrayInputStream input = new ByteArrayInputStream(s.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();

ByteArrayOutputStream decoded = new ByteArrayOutputStream();
decoder.decodeBuffer(input, decoded);
input.close();

BufferedImage im = ImageIO.read(new ByteArrayInputStream(decoded.toByteArray()));
ByteArrayOutputStream png = new ByteArrayOutputStream();
ImageIO.write(im, "png", png);

encoder.encode(new ByteArrayInputStream(png.toByteArray()), output);

return output.toString();

------------

Extending Apache Xalan is easier than I initially thought, but you may want to try it with something simpler before attempting this. You also have to set a few things in your XSL so that the transformer can find your java code. You also have to make sure the class is available for your JVM. I did this by packaging the class in a jar file a putting it in my jvm/lib/ext directory so it is available to Xalan-J.


Step 4)
Your output from of the transform must be using XSL-FO if you want to use FOP to create a pdf.
I used the fo:instream-foreign-object when adding the resulting image dump back to the xml. It looks something like this:

<fo:instream-foreign-object>
<svg:svg width="{$width}" height="{$height}">
<svg:image width="{$width}" height="{$height}">
<xsl:attribute name="xlink:href">
<xsl:value-of select="concat('data:image/png;base64,', mystuff:png(.))"/>
</xsl:attribute>
</svg:image>
</svg:svg>
</fo:instream-foreign-object>

Is there a better way of doing this than using the xlink:href attribute? Comments welcome!
Anyway, here my call to the XSLT extension is mystuff:png(). You set this up at the top of you stylesheet (see extension-element-prefixes, Apache Xalan docs).


Step 5)
Transform the resulting xml (with included image data) using Apache FOP to create a pdf file.


In summary, to do this you need to have some knowledge of:

java
xml namespaces
xslt
xsl-fo
some svg

Good luck!



09-03
<think> 我们正在讨论CSS中如何使用SVG图标。结合之前的对话和引用内容,用户特别关注自定义颜色和动画处理。我们将从以下几个方面展开: 1. 自定义SVG颜色:通过CSS控制SVG属性(如fill, stroke等) 2. 动画处理:使用CSS动画或变换对SVG进行动态效果处理 3. 引用中的示例展示了具体应用(如圆形加载动画) 我们将结合引用[1][3][4]中的知识点,提供具体示例。 注意:用户之前的提问中还包含几个相关问题,我们将逐一解答,但主要回答集中在“如何在CSS中使用SVG图标”这个核心问题上。 </think> ### CSS中使用SVG图标详解 结合SVG的特性和CSS的控制能力,可实现灵活的图标定制与动画效果。以下是核心方法及代码示例: --- #### 1. **自定义SVG颜色** 通过CSS控制SVG的`fill`(填充色)、`stroke`(描边色)等属性: ```css /* 类选择器控制SVG样式 */ .svg-icon { fill: #ff5733; /* 修改填充色 */ stroke: #3498db; /* 修改描边色 */ stroke-width: 2px; /* 描边宽度 */ } ``` ```html <svg width="24" height="24"> <circle class="svg-icon" cx="12" cy="12" r="10" /> </svg> ``` **效果**:圆形图标填充为橙色,蓝色描边[^1]。 > **关键点**: > - 内联SVG(直接嵌入HTML)可直接用CSS选择器控制 > - 修改`fill`属性可快速切换图标颜色(如悬停效果) > - 复杂路径图标同样适用此方法[^1][^4] --- #### 2. **SVG作为CSS背景** 将SVG编码为Data URI嵌入背景: ```css .button { background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2L2 7l10 5 10-5z" fill="currentColor"/></svg>'); width: 24px; height: 24px; } ``` **优势**: - 减少HTTP请求,提升加载速度 - 通过`currentColor`继承父元素文字颜色(如`.button { color: red }`使图标变红)[^1][^5] --- #### 3. **SVG动画实现** 结合CSS动画属性实现动态效果: ```css /* 旋转动画 */ @keyframes spin { to { transform: rotate(360deg); } } /* 路径缩放动画 */ .loader circle { stroke-dasharray: 100; animation: dash 2s linear infinite; } @keyframes dash { to { stroke-dashoffset: 200; } } ``` ```html <!-- 旋转的齿轮图标 --> <svg class="icon-spin" width="40" height="40"> <path d="M20 5l-5 10h10z"/> </svg> <!-- 进度条动画 --> <div class="loader"> <svg> <circle cx="40" cy="40" r="40" /> </svg> </div> ``` **常见动画效果**: - 旋转(`transform: rotate()`) - 路径描边动画(`stroke-dasharray` + `stroke-dashoffset`) - 缩放/位移(`transform: scale()`/`translate()`)[^3][^4] --- #### 4. **复杂路径处理** 多路径图标可通过分组控制: ```css /* 分别控制不同路径 */ .svg-multipath path:nth-child(1) { fill: #e74c3c; } .svg-multipath path:nth-child(2) { stroke: #2c3e50; } ``` --- ### 总结最佳实践 1. **内联SVG**:需精细控制时直接嵌入HTML,用CSS选择器操作 2. **背景图SVG**:需复用或简化代码时用Data URI,搭配`currentColor`继承颜色 3. **动画优化**:优先使用CSS动画而非JavaScript,性能更佳[^4] > SVG在缩放不失真、灵活控制方面的优势,使其成为图标方案的理想选择[^2][^5]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值