Relative paths in Flying Saucer XHTML?

2019-03-28 00:26发布

I am using Flying Saucer to render some PDF documents from strings to XHTML. My code is something like:

iTextRenderer.setDocument(documentGenerator.generate(xhtmlDocumentAsString));
iTextRenderer.layout();
iTextRenderer.createPDF(outputStream);

What I'm trying to understand is, when using this method, where are relative paths in the XHTML resolved from? For example, for images or stylesheets. I am able to use this method to successfully generate a text-based document, but I need to understand how to reference my images and CSS.

7条回答
闹够了就滚
2楼-- · 2019-03-28 01:29

AtilaUy's answer is spot-on for the default way things work in Flying Saucer.

The more general answer is that it asks the UserAgentContext. It will call setBaseURL() on the UserAgentContext when the document is set in. Then it will call resolveURL() to resolve relative URLs and ultimately resolveAndOpenStream() when it wants to read the actual resource data.

Well, this answer is probably way too late for you to make use of it anyway, but I needed an answer like this when I set out, and setting a custom user agent context is the solution I ended up using.

查看更多
登录 后发表回答