- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 Flying Saucer 将 XHTML 转换为 PDF 文档。我已经获得了仅使用基本 HTML 和内联 CSS 的代码,但是,现在我正尝试将图像作为一种标题添加到 PDF 中。我想知道的是,是否有任何方法可以通过将图像文件作为 Java Image 对象读取,然后以某种方式将其添加到 PDF(或 XHTML——就像它获得一个虚拟“url”一样)来添加图像表示可用于呈现 PDF 的图像对象)。有没有人做过这样的事情?
在此先感谢您提供的任何帮助!
最佳答案
上周我不得不这样做,所以希望我能马上给你答复。
最简单的方法是在使用 Flying Saucer 渲染之前,在 HTML 模板中添加您想要的图像作为标记。在 Flying Saucer 中,您必须实现 ReplacedElementFactory
这样您就可以在使用图像数据进行渲染之前替换任何标记。
/**
* Replaced element in order to replace elements like
* <tt><div class="media" data-src="image.png" /></tt> with the real
* media content.
*/
public class MediaReplacedElementFactory implements ReplacedElementFactory {
private final ReplacedElementFactory superFactory;
public MediaReplacedElementFactory(ReplacedElementFactory superFactory) {
this.superFactory = superFactory;
}
@Override
public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox, UserAgentCallback userAgentCallback, int cssWidth, int cssHeight) {
Element element = blockBox.getElement();
if (element == null) {
return null;
}
String nodeName = element.getNodeName();
String className = element.getAttribute("class");
// Replace any <div class="media" data-src="image.png" /> with the
// binary data of `image.png` into the PDF.
if ("div".equals(nodeName) && "media".equals(className)) {
if (!element.hasAttribute("data-src")) {
throw new RuntimeException("An element with class `media` is missing a `data-src` attribute indicating the media file.");
}
InputStream input = null;
try {
input = new FileInputStream("/base/folder/" + element.getAttribute("data-src"));
final byte[] bytes = IOUtils.toByteArray(input);
final Image image = Image.getInstance(bytes);
final FSImage fsImage = new ITextFSImage(image);
if (fsImage != null) {
if ((cssWidth != -1) || (cssHeight != -1)) {
fsImage.scale(cssWidth, cssHeight);
}
return new ITextImageElement(fsImage);
}
} catch (Exception e) {
throw new RuntimeException("There was a problem trying to read a template embedded graphic.", e);
} finally {
IOUtils.closeQuietly(input);
}
}
return this.superFactory.createReplacedElement(layoutContext, blockBox, userAgentCallback, cssWidth, cssHeight);
}
@Override
public void reset() {
this.superFactory.reset();
}
@Override
public void remove(Element e) {
this.superFactory.remove(e);
}
@Override
public void setFormSubmissionListener(FormSubmissionListener listener) {
this.superFactory.setFormSubmissionListener(listener);
}
}
你会注意到我在这里硬编码了/base/folder
这是 HTML 文件所在的文件夹,因为它将是 Flying Saucer 用于解析媒体的根 URL。您可以将其更改为正确的位置,来自您想要的任何位置(例如属性)。
在您的 HTML 标记中,您在某处指示了一个 <div class="media" data-src="somefile.png" />
像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My document</title>
<style type="text/css">
#logo { /* something if needed */ }
</style>
</head>
<body>
<!-- Header -->
<div id="logo" class="media" data-src="media/logo.png" style="width: 177px; height: 60px" />
...
</body>
</html>
最后您只需要指明您的 ReplacedElementFactory
渲染时飞碟:
String content = loadHtml();
ITextRenderer renderer = new ITextRenderer();
renderer.getSharedContext().setReplacedElementFactory(new MediaReplacedElementFactory(renderer.getSharedContext().getReplacedElementFactory()));
renderer.setDocumentFromString(content.toString());
renderer.layout();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);
// baos.toByteArray();
我一直在使用 Freemarker 从模板生成 HTML,然后将结果提供给 FlyingSaucer,并取得了巨大成功。这是一个非常简洁的库。
关于java - 使用 Flying Saucer 在内存中将图像渲染为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11477065/
我在 asp.net 应用程序(使用 IKVM)中使用 flysaucer 和 iText 将 HTML 转换为 PDF。如果我将样式直接放在 html 中,它工作正常(即使样式放在样式标签之间),但
我一直在尝试将带有内容的伪元素添加到正在使用飞碟库转换的页面的 CSS 中。在常规浏览器中以 HTML 格式查看页面时,代码运行良好,我可以看到伪元素 (:before)。然而,当使用 Flying
我使用 Flying Saucer 和 itext 将 html 转换为 pdf,为了在垂直方向写入文本,我尝试了简单的 css: style="writing-mode: tb-rl"; 浏览器的变
我有一个组合框,用户可以在其中选择可用的语言。该应用程序包含每种语言的属性文件。在页面的资源部分,资源包是根据用户配置文档中的语言标记(DE、EN ...)计算的。 有什么简单的方法可以根据组合框的值
我已经在页面上嵌入了Youtube播放器,并使用JavaScript生成了“即时”播放列表。观看期间是否可以在播放列表的末尾添加新视频?我可以从列表的开头删除视频吗? 最佳答案 您可以,但是在尝试在视
...特别是在 Groovy 中(因此是标签)? 在 Java 中你不能这样做......但在动态语言(例如 Python)中你通常可以。 在 Eclipse 中尝试做类似在 Spock 特性(即测试
在编写 shell 脚本(据我所知通常没有 .* 后缀)时,我通常会执行以下操作 1) emacs foo2) 输入 #!/bin/sh3) 关闭并重新打开 emacs 以启用 shell-scrip
嗨,我有一个对象:var myobject = new Object;我想在以这种方式循环 jquery 输入集合时动态填充它的属性: $('.test').each(function(){
我认为可以在 C 中即时连接字符串(字符数组)。 char* str1= "hello" " " "world"; 但是当我尝试以下操作时,我会收到一条错误消息(函数 fopen 的参数太少)。为什么
我有下表: store | dow | turnover ------+-----+----------- 1 | 1 | Eu59.426,00 1 | 2 | Eu33.
我需要使用标准 UNIX diff 命令和 python subprocess 模块创建一个差异文件。问题是我必须在不创建临时文件的情况下比较文件和流。我想过通过 os.mkfifo 方法使用命名管道
Fly-Pie 是一个用于 GNOME 的独特的菜单启动器,以操作鼠标为主的用户可以用它进行一些操作。 应用启动器可以方便地快速切换到一个活动窗口,启动新的应用,等等。 Ulau
strftime(),根据 cpan.org: print strftime($template, @lt); 我只是想不出这个正确的 Perl 代码配方。它在我调用 strftime() 的地方不断
这里有没有人得到 word-wrap: break-word 在 flying-saucer 中工作?我在 flying-saucer 中将 html 渲染为 png。我可以找到一个 3 年前的话题
Windows 7,Emacs 25.1 我需要对我的自定义文本进行“即时”拼写检查(例如,强调不正确的单词)。但我在 中写文字两种语言 : 英语和俄语。我想在两种语言的拼写检查之间轻松切换。 什么是
一直在玩飞碟R8,试过从首页隐藏页眉和页脚 我的PDF。 我关注了 hint试图遵循 的 W3C 规范内容:元素() (W3C running elements)在我的 print.css 中。据描述
我正在使用由按钮触发的 FlyTo 动画来放大 map 。平移缩放动画结束后,无法使用鼠标滚轮缩小。 如果我单击基本的缩小控件,它就可以工作,如果我在触摸屏上测试它,它就可以工作。如果我尝试使用鼠标滚
我无法将Wild-fly容器连接到在其他容器中运行的PostgreSQL服务器。但是,我能够从主机连接到数据库。 最佳答案 看看networking和links。我建议联网。网络内部的容器名称可以由d
我正在使用 Flying Saucer/iText 生成报告。现在报告有一个条件,如果特定条件发生,报告应该移动到 pdf 的下一页,并在 PDF 上添加数据等等。 问候帕万 最佳答案 您必须使用 c
您好,我有一个 tabpanel,其中有两个面板,但是当我尝试使用 tabpanel.show() 显示一个面板时。我收到这条消息 Ext.fly() null or not an object do
我是一名优秀的程序员,十分优秀!