svg

博主分享处理粘贴图片生成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!



您可能感兴趣的与本文相关的镜像

Qwen-Image

Qwen-Image

图片生成
Qwen

Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像

05-22
### SVG 的基本概念及其在网页开发中的应用 SVG 是 Scalable Vector Graphics 的缩写,是一种基于 XML 的矢量图像格式。它允许开发者通过代码来描述复杂的二维图形,并且可以在不损失质量的情况下任意缩放[^1]。 #### 什么是 SVGSVG 是一种矢量图形文件格式,由 W3C 定义并标准化。与位图不同的是,SVG 基于数学公式绘制形状、路径和其他视觉元素,因此无论放大还是缩小都不会失真。这种特性使得 SVG 成为设计图标、图表以及响应式布局的理想选择[^2]。 #### 如何嵌入 SVG 到 HTML 中? 可以通过多种方式将 SVG 集成到网页中: - **Inline 方法**:直接将 SVG 标记嵌入 HTML 文档中。 ```html <svg width="100" height="100"> <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /> </svg> ``` - **`<img>` 标签**:类似于加载 PNG 或 JPEG 文件一样引入外部 SVG 文件。 ```html <img src="image.svg" alt="SVG Example"> ``` - **CSS 背景图片**:利用 CSS `background-image` 属性指定 SVG 文件作为背景。 ```css div { background-image: url('image.svg'); } ``` 每种方法都有各自的优缺点,在实际项目中可以根据需求灵活选用[^1]。 #### SVG 在网页开发中的优势 相比传统的栅格图像(如 JPG 和 PNG),使用 SVG 开发网站具有以下几个显著优点: - 更高的分辨率独立性——无论屏幕尺寸如何变化都能保持清晰度; - 较小的文件体积通常意味着更快的页面加载速度; - 支持脚本编程和动画效果制作,增强用户体验互动感; 例如,借助 D3.js 这样的库,可以轻松实现复杂的数据可视化方案[^2]。 #### 推荐的学习资源与工具 对于初学者来说,掌握基础语法之后还需要不断实践才能熟练运用这项技术。这里列举几个有助于学习和工作的辅助软件及文档链接: - Boxy SVG Editor 提供直观界面帮助快速构建简单的矢量作品[^1]; - 查阅官方规范说明获取权威指导 https://www.w3.org/TR/SVG/; - 尝试在线平台 CodePen 创建实时预览案例研究. --- ###
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值