我正在尝试将资源打包到一个 jar 中,但是我无法让 Flying Saucer 在类路径上找到 css -我无法轻松构建 URL 以无缝解决这个问题。
飞碟有没有办法在类路径上指定资源包来解析项目和图像?
注意:我在一个没有文件系统写入权限的 webstart 应用程序中运行它,所以 jar 扩展不是一个真正的选择。
我已经按照这篇文章使用FlyingSaucer将XHTML转换为PDF,它很棒,但有一个主要的垮台......它的速度非常慢!
我发现从XHTML渲染PDF需要1到2分钟,无论该页面有多简单.
基本代码:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
public class FirstDoc {
public static void main(String[] args) throws IOException, DocumentException {
String inputFile = "firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
Run Code Online (Sandbox Code Playgroud)
示例XHTML:
<?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 First Document</title>
<style type="text/css"> b …Run Code Online (Sandbox Code Playgroud)