[1.2.4][PDF 0.9] Render image from blob

219 views
Skip to first unread message

Neoh59

unread,
Jun 22, 2012, 11:47:49 AM6/22/12
to play-fr...@googlegroups.com
Hi all,

I want to use the PDF module to save a user profile to a file (a PDF file of course).

class models.User -> firstname, lastname, avatar
avatar is a blob (a JPEG file)
I render avatar with URL like : http://localhost:9000/user/{userid}/avatar.jpg
and Play! renderBinary method.


I use the HTML template for the PDF rendering, with a CSS dedicated to "print mode".
But the avatar is never displayed with PDF. (like if it doesn't exist)

I tried with relative URL, or with absolute one. It's the same.
If I put the avatar file to /public/images directory, and change URL for this staticDir -> it's OK

I found some topics about that, and tried a lot of lines of codes, but without success.


In debug mode, I have see :
ITextUserAgent.getImageResource(String) line: 66
-> generate exception :
java.io.IOException: The byte array is not a recognized imageformat.
-> and go to :
ITextUserAgent.getImageResource(String) line: 70

the stack trace :
Thread [play-thread-1] (Suspended)   
    ITextUserAgent.getImageResource(String) line: 66   
    ExtendedITextReplacedElementFactory(ITextReplacedElementFactory).createReplacedElement(LayoutContext, BlockBox, UserAgentCallback, int, int) line: 57   
    ExtendedITextReplacedElementFactory.createReplacedElement(LayoutContext, BlockBox, UserAgentCallback, int, int) line: 72   
    BlockBox.calcDimensions(LayoutContext, int) line: 677   
    BlockBox.calcDimensions(LayoutContext) line: 631   
    BlockBox.layout(LayoutContext, int) line: 768   
    BlockBox.layout(LayoutContext) line: 735   




Someone can help me ?
Thanks

Neoh59

unread,
Jun 28, 2012, 3:53:25 AM6/28/12
to play-fr...@googlegroups.com

Hi all,

I found a solution to solve that. It's surely ugly but it works.


The problem was :
/1/ the pdf-module try to read the avatar with the HTTP URL
NaiveUserAgent.getImageResource(theURL)
/2/ try to get it like a VirtualFile :
NaiveUserAgent.resolveURI(theURL)
then call VirtualFile file = Play.getVirtualFile(uri);
it can't => return theURL
/3/ try to get the stream with a URLConnection
NaiveUserAgent.resolveAndOpenStream(uri);
URLConnection connection = (URLConnection) new URL(uri).openConnection();
return connection.getInputStream();
=> in my case, the connection is not the current user session, so is redirected to the login page (my app need authentication) and don't get the avatar

Solution : read the file directly with the filesystem path
in the template, check if it's a PDF rendering, and if it is, set the relative path to the file
#{if request.format == 'pdf'}<img src="${play.vfs.VirtualFile.open(_user.avatar.getFile()).relativePath()}" />#{/if}
#{else}<img src="@@{Users.avatar(_user.id)}" />#{/else}
=> it could be : <img src="/data/attachments/50a95262-b998-4833-928d-2d7a5e07fb9e" />

It now works like a charm.

Hope this help someone else in the future.
Reply all
Reply to author
Forward
0 new messages