<div dir="ltr" style="text-align: justify;" trbidi="on">In this tutorial we will discuss about download <i><b>pdf </b></i>files from <i><b>JAX-RS</b></i>, for user to download a file, annotate the method with <i><b>@Produces(&#8220;application/pdf&#8221;)</b></i> :</p>
<ol>
<li>Put<i><b> @Produces(&#8220;application/pdf&#8221;)</b></i> on service method, with a Response return type. It means the output is a pdf file.</li>
<li>Set <i><b>&#8220;Content-Disposition&#8221; </b></i>in Response header to tell browser pop up a download box for user to download.</li>
</ol>
<p>
 <b>1. Download PDF file in <i>JAX-RS</i></b></p>
<p> Full example to download an <i><b>pdf </b></i>file from <i><b>JAX-RS</b></i>.</p>
<pre class="highlight" name="code">package com.dineshonjava.ws.rest; 
 
import java.io.File; 
 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.Response; 
import javax.ws.rs.core.Response.ResponseBuilder; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Path("/pdf") 
public class PdfFileService { 
 private static final String PDF_PATH = "d:mongodb.pdf"; 
 
 @GET 
 @Path("/get") 
 @Produces("application/pdf") 
 public Response getSalarySlipFile() { 
 
 File file = new File(PDF_PATH); 
 
 ResponseBuilder response = Response.ok((Object) file); 
 response.header("Content-Disposition", 
 "attachment; filename="mongodb_pdf_file.pdf""); 
 return response.build(); 
 
 } 
} 
 
</pre>
<div align='center' id="ads-id"></div>
<p><b>2. Test</b></p>
<p> Deploy above <i><b>JAX-RS</b></i> service, access this URI pattern : &#8220;<i><b>http://localhost:8181/sdnext/doj/pdf/get</b></i>&#8220;.</p>
<p> Figure : PDF file &#8220;<b>d:mongodb.pdf</b>&#8221; from server is prompt for user to download, with a new <b>pdf </b>file name <b>&#8220;mongodb_pdf_file.pdf&#8221;</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/06/downloadpdf.png"/></div>
<p>
<b><br />
</b> <b>Download SourceCode</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Download%20pdf%20file%20from%20JAX-RS.zip?attredirects=0&;d=1" target="_blank">Download pdf file from JAX-RS.zip</a></b></p>
<p>
<i><b>References</b></i><br />
1.<b> <i><a href="http://docs.oracle.com/javaee/6/tutorial/doc/giepu.html" target="_blank">JAVA REST Web Services</a></i></b><br />
2. <i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank">Wikipedia for REST Web Service</a></b></i></p>
<p> ; </p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/download-image-file-from-jax-rs/">Previous </a><;<; ; ; || <a href="https://dineshonjava.com/jax-rs-web-service-tutorial/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/download-excel-file-from-jax-rs/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/download-image-file-from-jax-rs/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/download-excel-file-from-jax-rs/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '416'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…