Flying Saucerを使用して、いくつかのPDFドキュメントを文字列からHTMLにレンダリングしています。
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputStream is = new ByteArrayInputStream(html.getBytes("UTF-8"));
Document doc = builder.parse(is);
response.setContentType("application/pdf; charset=UTF-8");
response.setHeader("Content-disposition", "inline; filename=\"" + outFileName + "\"");
OutputStream os = response.getOutputStream();
ITextRenderer iTextRenderer = new ITextRenderer();
iTextRenderer.setDocument(doc,null);
iTextRenderer.layout();
iTextRenderer.createPDF(os);
os.flush();
os.close();
プレーンテキストの場合、これは問題なく機能します。HTMLコンテンツで外部CSSを参照しました。ただし、PDFが生成されると、CSSは適用されません。
setDocument()
メソッドはdocumentとurlの2つのパラメーターを取ることを読みました。urlパラメーターは、外部CSSなどのxhtmlに表示される相対パスの前に追加するために使用されるベースURLを示します
だから、私は供給しようとしました
コンテキストパス/css
baseURLでディレクトリを作成し、で使用しましたsetDocument()
。それでも結果はありません
だから、私の質問baseURLとして渡す正しいURLは何ですか?
String baseURL = ""; // What goes here as root URL for resources
iTextRenderer.setDocument(doc,baseURL);