Reputation: 93
I am using JSF1.2 framework and converting XML into PDF using FOP engine and XSL stylesheet. as far as string data is concerned, its working perfectly but now i want to embed images in my output PDF. the directory structure is as follows
ProjectDirectory
-src
--resources
---P2O.xsl
-WebContent
--images
---image1.jpg
the following works fine if i place image1.jpg file in d: drive
fo:external-graphic src='url("file:///d:/image1.jpg")'>
/fo:external-graphic>
Since i have to deploy this application on production server so i can not set this path there. My question is that what should i specify in "src" to pick that image file from WebContent/images path
Any example/help in this case would be greatly appreciated. With Regards
Upvotes: 2
Views: 9414
Reputation: 93
I figured out the problem after a brief search. The main point of consideration is the FOUserAgent
which acts the mediator between the application and FOP processor. The FOP processor does not know the root or base path of your application unless it's defined explicitly. To use a relative path for your images, you need to create an FOUserAgent
object and specify the base URL for that FOUserAgent
instance. In my case, I added the following line (noted below):
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// This is the important line
foUserAgent.setBaseURL("file:///" + FacesUtils.getHttpServletRequest().getRealPath("/"));
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outputStream);
where FacesUtils
is just a custom made utils file I keep in my project. If you do not have any utils file then simply use FacesContext
to get the realPath()
.
Now basePath will refer to your WebContent folder and any image can be accessed relatively from there. I now access my images like this in the .xsl:
src='url("images/image1.jpg")'
where the images folder lies in WebContent folder.
Upvotes: 1