GWT attached pdf document in post response - java

I have an issue with http response.
I send some data and in response I get attached pdf document, and I need to open it document in a new browser tab.
I can't do it using Window.Open(String url, String title, String... parameters) because I dont have this url.
if I open it with right click on and tell firebug "Open in a new Tab" I got an url

In order to display pdf in your page you should have Content-Type: application/pdf in your response header. So, You should set in response header in your servlet which is responsible that given url.
After that you can put your pdf inside one of these html tags embed, object or iframe. I think gwt's Frame is a good way to do it. See this tutorial as well: 4 Ways To Stream Pdf and Some Tips

Related

PDF generation in orbeon

I have an orbeon form and when someone has fill out the form, I want it to create a pdf and sent it to my webservice.
Right now, when the form is fill out and you press sent, the document id, app name and form name are sent to the webservice.
Can anyone help me with a guide or an example ?
The webservice is in grails (JAVA).
Best regard
Martin
You have to customize a process for your send/save button.
Have a look a this:
http://doc.orbeon.com/form-runner/advanced/buttons-and-processes/index.html
Scroll to 'Customizing processes'. You can find here an example of the process for the send button:
<property
as="xs:string"
name="oxf.fr.detail.process.send.acme.hr"
value='require-valid
then pdf
then email
then send("http://example.org/")
then navigate("/success")
recover navigate("/failure")'/>
The 'pdf' action should generate a pdf document.
To get an url of the document you can customize the 'send' action: go to
'Core Form Runner actions' at http://doc.orbeon.com/form-runner/advanced/buttons-and-processes/index.html and find 'send'. In the 'content' property you can specify 'pdf-url' to get the pdf url. The 'uri' property should point to your webservice.
Once your web service receives a pdf url, you can issue a request using the url to download document.

Images are not showing in pdf created using velocity template

I create pdf using Velocity template, but i am not able to put images in pdf.
I am adding url into context object and accessing that object into eot.vm file, url is going proper but still images are not displaying in pdf.
Thanks & Regards,
Tushar
Make sure that URL you are passing is sending IMAGE as response not an full html page.
i was passing URL that was sending me a whole html page, so i was unable to see images.
But now its fixed.
Please visit below link:
https://answers.atlassian.com/questions/2907/how-to-access-image-generated-via-velocity-template-confluence

HTML page as AJAX response?

Can I send an entire HTML page with an AJAX response? If so, how to render that HTML page and what are the pros and cons doing that. The reason why I am asking this question is I found out that if we use response.sendRedirect("index.html") as a reply to an AJAX request in servlet we get the indx.html as AJAX response XML.
Your question doesn't make much sense and I can't quite tell what you're asking - the response to an ajax request will be whatever the server sends back. It could be plain text, XML, HTML, a fragment of an HTML/XML document etc. What you can do with depends on your script. If you're using a library like jQuery, what happens on the client side and what you can do with the response can also depend on how the library interprets the response (Is it a script? It it HTML/XML or JSON?).
if we use response.sendRedirect("index.html") as a reply to ajax request in servlet we get the indx.html as ajax response xml. Can some one pls explain this
An ajax request will behave much like a 'regular' HTTP request. So when you send back a redirect from your server (HTTPServletResponse#sendRedirect), the browser follows the redirect just like it would for any other request. If your ajax request was to a resource that required HTTP BASIC authentication, you'd see a prompt to login, just like you would if you visited the URL directly in a new browser window.
If you want to send HTML as a response, because you want to update divs, tables or other elements, but still want to use the same css or javascript files then it can make sense.
What you can do is to just send it as plain/text back to the javascript function, it can then take that and put it into the inner html element that you want to replace, but, don't do this if you want to replace the entire page, then doing what you want is pointless.
When you make the http request for your ajax call, it has its own response stream, and so when you redirect you are just telling the browser to have that http request go to index.html, as #no.good.at.coding mentioned.
If I get your question, you just want to know whether you could return whole entire HTML with AJAX and know the pro and cons.
The short answer to your question is yes, you could return the entire HTML page with your AJAX response as AJAX is just an http request to the server.
Why would you want to get the entire HTML? That's confusing to me and that is the part that I am not clear about. If you want to want to render the entire HTML (including tags like html, body, etc?), you might as well open it as a new page instead of calling it via Ajax.
If you are saying that you only want to get fragments of HTML to populate a placeholder in your page via AJAX then this is an acceptable practice. jQuery even provides load() function (http://api.jquery.com/load/) to make that task easy for you to use (check the section Loading Page Fragments).
Using this method, you could populate the placeholder using the HTML Fragments that is dictated by your server logic (i.e when the login fails or succeed) including the one via server redirect in your question.

Output PDF file contents stored as BLOB using AJAX call without creating the file

I need to open a pdf file for the user who clicks on a download button.
The download button makes a ajax call to a servlet which gets data from a blob field in the database containing PDF contents, and returns it as a response.
What can I do in order to make the response to be downloaded as a PDF file to the user.
The servlet code is as below:
response.setContentType("application/pdf");
oracle.sql.BLOB blob = (BLOB) rs.getBlob("MYPDF");
byte[] bytes = blob.getBytes(1, (int) blob.length());
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
I receive a long response containing characters like the ones below in AJAX when I checked the Fire bug for the response.
�a�J��㔎��ji�2K���y�2��q F�f�9�G��!%�kɂ��W��������mp){h̕�S���NJ_�A����'����2k��j���яR�>wB�e�|=w�p�%w��qǦ>�~�1o�㾙9j�B�;aNx3�`z��طc�O��ï��$�;�N|;�xۇ��;�-�c�f�M��c���(�f�M6K���
I don't want to submit the page or to popup a window for the servlet with the parameters sent for the query showing in the URL
I also don't want to create the file on the server.
Is there a way to take the response of servlet coming in ajax call and display it to a page or Iframe and the browser automatically downloads the file...
Its is NOT possible to download a file using Ajax. However, the same 'effect' can be achieved using a hidden IFRAME. Instead of using XMLHttpRequest, dynamically create a hidden iframe and submit it to the servlet with the proper parameters. When the response comes, the browser will automatically handle the content based on the content-type/extension. If you are using jQuery, then this plugin has this functionality in-built.
Sounds like Rahulmohan knows his stuff but it might be worth trying to add a filename header to your response stream. I do something similar in asp.net but not in an Ajax environment and the only difference between my code and yours is this line:
Response.AddHeader("Content-Disposition", "attachment; filename=" & Filename & ".pdf")
My page doesn't run in an Ajax environment though.

How to show images in div that stored in db

I decided to store uploaded to servlet pictures in DB. But how to show them in browser (in particular div with css image-background style) without page reload?
In details: I have an full-AJAX web client that works with java servlet.
Thanks.
Create a servlet that accepts a name / id uniquely identifying the picture as a GET parameter
Let the servlet load the picture from DB (as byte array or better - as stream)
Stream the picture using response.getOutputStream()
Don't forget to set the content-type to image/png or image/jpeg
Refer to the image in the css with url: imageServlet?id=13214

Categories