Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Display PDF in a RAP app
Display PDF in a RAP app [message #112633] Tue, 18 November 2008 12:02 Go to next message
Eclipse UserFriend
Originally posted by: spanprevention-nursubscriptions.gmail.com

Hi,

Perhaps a very basic query. The requirement is to display a dynamically
generated PDF in a RAP application.

The following threads provide leads:
http://dev.eclipse.org/newslists/news.eclipse.technology.rap /msg00946.html
http://dev.eclipse.org/newslists/news.eclipse.technology.rap /msg02087.html

However, my foundations being shaky, i am unable to relate the concepts
involved. I have created the PDF content using iText. My problem is how to
display it. Basically, i am unable to understand the relation between the
PDF content, servlet and browser widget mentioned in the threads.

As far as i understand, the way to display the dynamically generated PDF
in a browser having a PDF plugin is as follows:
Invoke a servlet and set its content to the PDF bytestream. Set the header
as "application\pdf".

Questions:
1. Is the above understanding correct? If not, how does one go about this?
2. How does one invoke a servlet from RAP code?
3. What is the need for the browser widget?

Clarifications and pointers to sample code are much appreciated.

TIA,
Best Regards,
-abhi
Re: Display PDF in a RAP app [message #112646 is a reply to message #112633] Tue, 18 November 2008 12:56 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: benjamin.wolff.web.de

Hi ahbi,

i also create several PDFs using iText and provide them in an external Browser to
the user. The Use-Case is as follows:

- i create the PDF in a Temp File, with File.createTempFile...
- then i open an externalbrowser with an url that links to a ServiceHandler, as
explained in the other threads, for example: http://<url to rap
app>?custom_service_handler=myServiceHander&file=<filename of the pdf>
- the servicehandler accesses the file stored in the Temp Directory using the
delivered filename (get the path with System.getProperty("java.io.tmpdir")) and
puts the file as a byte stream with the proper header (mimeType etc.) in the
servlet response
- after that, the servicehandler deletes the temp file because only the handler
knows when the file is no longer needed

This works quite well for me, it may be better not to use a temp file but put the
file in a resource directory within the rap application. i couldn't make this
work (registering a resource directory to store and read files) so i'll go with
the temp variant. moreover you should consider some security issues so that not
everyone can just call this servicehandler (provide a hashed session id and
compare it inside the servicehandler with the current sid or what so ever...).

how to create and register the servicehandler is described in the threads you
posted earlier. however to register the servicehandler you should use the
RWT.getServiceManager()... methods instead of directly accessing the register
methods.

HTH,
-Ben




abhi schrieb:
> Hi,
>
> Perhaps a very basic query. The requirement is to display a dynamically
> generated PDF in a RAP application.
> The following threads provide leads:
> http://dev.eclipse.org/newslists/news.eclipse.technology.rap /msg00946.html
> http://dev.eclipse.org/newslists/news.eclipse.technology.rap /msg02087.html
>
> However, my foundations being shaky, i am unable to relate the concepts
> involved. I have created the PDF content using iText. My problem is how
> to display it. Basically, i am unable to understand the relation between
> the PDF content, servlet and browser widget mentioned in the threads.
> As far as i understand, the way to display the dynamically generated PDF
> in a browser having a PDF plugin is as follows:
> Invoke a servlet and set its content to the PDF bytestream. Set the
> header as "application\pdf".
>
> Questions:
> 1. Is the above understanding correct? If not, how does one go about this?
> 2. How does one invoke a servlet from RAP code?
> 3. What is the need for the browser widget?
>
> Clarifications and pointers to sample code are much appreciated.
>
> TIA,
> Best Regards,
> -abhi
>
>
Re: Display PDF in a RAP app [message #112805 is a reply to message #112646] Wed, 19 November 2008 18:34 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: spanprevention-nursubscriptions.gmail.com

Hi Ben,

Thanks a lot for your detailed reply. It really helped me, and i have
progressed further!

A couple of queries/observations though: I read in one of the posts that
if one is using a servlet, it is sufficient to register it using the
org.eclipse.equinox.http.registry.servlets extension point, rather than
using a custom ServieHandler. This is what i tried, and it worked (i may
have problems knowing when to delete the file though, right now i do it if
the output is flushed successfully). However, the place where i thought i
would not have a problem at all, had me stumped. i needed to pass the PDF
filename to the servlet, so i set a data object in the request using
RWT.getRequest().setAttribute("report", report); In the servlet, when i do
a request.getAttribute("report");, i get null. Unfortunately, this part of
the code cannot be debugged either. The same thing happened when i tried
to pass this through the session using
RWT.getSessionStore().getHttpSession().setAttribute("report ", report);. i
can't believe this is not working, must be a silly mistake on my part
somewhere, but can't figure out for the time being.

Incidentally, i too was unable to use resources to save the PDF file,
rather than a temp directory. i kept getting null, no matter what i tried.
Any ideas why?

Thanks again for your help!

Best Regards,
- abhi
Re: Display PDF in a RAP app [message #112842 is a reply to message #112805] Wed, 19 November 2008 20:22 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: m.n.com

In my RAP app i am creating a pdf on the fly (with iText) and pulling
existing ones (they are actually uploaded in the app too);

The pdfs I create with iText i put info in the session in RAP and pull
it out in the servlet.

Here is a snippet from my plugin.xml:

<extension
point="org.eclipse.equinox.http.registry.servlets">
<servlet
alias="/results"
class="com.xxx.xxxx.ui.SearchResultsServlet">
</servlet>
<servlet
alias="/documents"
class="com.xxx.xxxx.ui.DocumentServlet">
</servlet>
</extension>


From my code for what I do via iText:

....
RWT.getSessionStore().getHttpSession().setAttribute("resultSet ",
resultsTableViewer.getInput());
....
Browser browser = new Browser(container, SWT.None);
browser.setUrl("/results");
....

And in the servlet:
....
resp.setContentType("application/pdf");
req.getParameter("");
....
PdfWriter.getInstance(document, resp.getOutputStream());
....
List list = req.getSession().getAttribute("resultSet");
....
document.close();
....



For the files on the file system:
....
Browser browser = new Browser(container, SWT.None);
browser.setUrl("/documents?documentId" + "=" + document.getId());
....

and in the Servlet:
resp.setContentType("application/pdf");
Long id = Long.parseLong(req.getParameter("documentId"));
NotificationDocument document =
Services.get(NotificationService.class).getNotificationDocum ent(id);

FileInputStream fileInputStream = new FileInputStream(new File(rootPath
+ document.getId() + "/" + document.getFilename()));
OutputStream fileOutputStream = resp.getOutputStream();
byte[] buf = new byte[bufferSize];
int len;
while ((len = fileInputStream.read(buf)) > 0){
fileOutputStream.write(buf, 0, len);
}
fileInputStream.close();
fileOutputStream.close();



abhi wrote:
> Hi Ben,
>
> Thanks a lot for your detailed reply. It really helped me, and i have
> progressed further!
>
> A couple of queries/observations though: I read in one of the posts that
> if one is using a servlet, it is sufficient to register it using the
> org.eclipse.equinox.http.registry.servlets extension point, rather than
> using a custom ServieHandler. This is what i tried, and it worked (i may
> have problems knowing when to delete the file though, right now i do it
> if the output is flushed successfully). However, the place where i
> thought i would not have a problem at all, had me stumped. i needed to
> pass the PDF filename to the servlet, so i set a data object in the
> request using RWT.getRequest().setAttribute("report", report); In the
> servlet, when i do a request.getAttribute("report");, i get null.
> Unfortunately, this part of the code cannot be debugged either. The same
> thing happened when i tried to pass this through the session using
> RWT.getSessionStore().getHttpSession().setAttribute("report ", report);.
> i can't believe this is not working, must be a silly mistake on my part
> somewhere, but can't figure out for the time being.
>
> Incidentally, i too was unable to use resources to save the PDF file,
> rather than a temp directory. i kept getting null, no matter what i
> tried. Any ideas why?
>
> Thanks again for your help!
>
> Best Regards,
> - abhi
>
>
Re: Display PDF in a RAP app [message #112870 is a reply to message #112842] Thu, 20 November 2008 06:05 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: spanprevention-nursubscriptions.gmail.com

Hi Mark,

Thanks a lot for the code snippet. Were you able to access the session
attribute in the servlet using only the piece of code you have attached? i
was unable to do so unless i encoded the url using
response.encodeUrl(String url). i am using ExternalBrowser for displaying
the PDF, so that the application is not blocked.

Thanks again!

Best Regards,
-abhi
Re: Display PDF in a RAP app [message #113858 is a reply to message #112870] Thu, 27 November 2008 15:29 Go to previous message
Eclipse UserFriend
Originally posted by: m.n.com

Abhi,
Sorry i didn't get back sooner. I have been off for a few days.

Yes, i was able to access the session attribute. That code was cut
and pasted directly from working code.

I am not using an external browser though. But i am not sure how that
could make a difference.

abhi wrote:
> Hi Mark,
>
> Thanks a lot for the code snippet. Were you able to access the session
> attribute in the servlet using only the piece of code you have attached?
> i was unable to do so unless i encoded the url using
> response.encodeUrl(String url). i am using ExternalBrowser for
> displaying the PDF, so that the application is not blocked.
> Thanks again!
>
> Best Regards,
> -abhi
>
>
Previous Topic:SWT.EMBEDDED alternative?
Next Topic:[ANN] RAP 1.2 M3 available
Goto Forum:
  


Current Time: Thu Feb 09 00:43:37 GMT 2023

Powered by FUDForum. Page generated in 0.02247 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top