1

I have a java class that generates a PDF file to a folder in my computer. I have managed to connect this class to a link on a web application and when i click this link it generates the pdf and writes it to the folder on my computer. I would want to change this and have the link send the pdf to the browser instead. How can i do this? The class does not use any HttpRequests or similar and the link isnt a hypertext link atm. Im looking for the most straight forward way to send a pdf to the browser.

1
  • send a pdf to the browser? Do you want to mean "open/view pdf in browser"? Commented Oct 26, 2010 at 7:44

4 Answers 4

2
/* Java Code */
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=path/to/file.pdf");
Sign up to request clarification or add additional context in comments.

Comments

1

i print it (any kind of file) to the response stream from a byte array, inside a servlet

 if(content != null)
                {
                    response.setContentType( "application/octet-stream" );
                                            response.setHeader("Content-Disposition", "attachment; filename=\"" + fname + "\"");
                response.setContentLength(content.length);
                out.write(content);
            }//where content  is byte[] 

Comments

0

You should be able to write the pdf to a stream, you can pass it the output stream from your response.

Comments

0

There are several ways to do that:

  1. Put PDF file on some place available from Web, and then redirect user to URL, which will lead him to PDF file (if your web server supports this). Redirection may be easily done with "Location" HTTP header.

  2. Send PDF file in HTTP response stream. Note, that you will have to set corresponding Mime-type in HTTP header. Implementation depends on web server / web framework you are using in your application.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.