title: Trigger browser PDF download Spring MVC
body: <p>I'm having trouble getting a download window to pop up in the browser using spring MVC. I am getting the file from Amazon S3 and trying to use a REST call made in the browser against the controller, which should prompt the user to download the PDF. The window isn't popping up when I hit the endpoint and I'm getting a load of random characters in the response, so I think maybe I'm not using my Input/Output Streams properly. What am I doing wrong? I should also mention we are using Backbone JS in the front end, I know it uses jQuerys ajax to make REST calls.. Would this have any effect?</p>
  
  <p>Controller: </p>
  
  <pre><code> @ResponseStatus(HttpStatus.OK)
      @ResponseBody
      @RequestMapping(value = "/email/download/{pdfId}", method = RequestMethod.GET)
      public void downloadStatusReport(@PathVariable("pdfId") String pdfId, HttpServletResponse response) {
          response.setContentType("application/pdf");
          response.setHeader("Content-Disposition", "attachment; filename=\"" + "RemediationStatusReport.pdf\"");
          response.setHeader("Content-Transfer-Encoding", "binary");
          response.setHeader("X-Frame-Options", "ALLOWALL");
          s3FileHandler.downloadPdfFileFromS3(pdfId, response);
      }
  </code></pre>
  
  <p>Method called to download file from S3 and send back a response:</p>
  
  <pre><code> public void downloadPdfFileFromS3(String id, HttpServletResponse response) {
          S3Object s3Object = client.getObject(new GetObjectRequest(S3_BUCKET_NAME, id));
          InputStream inputStream = s3Object.getObjectContent();
          LOGGER.info("PDF successfully downloaded from S3");
          try {
              IOUtils.copy(inputStream, response.getOutputStream());
              response.flushBuffer();
          } catch (IOException io) {
              LOGGER.error("IO Exception when processing PDF", io);
          }
      }
  </code></pre>
  
  <p>Some of the response:</p>
  
  <pre><code>%PDF-1.4
  %âãÏÓ
  2 0 obj
  &lt;&lt;/Type/XObject/ColorSpace/DeviceGray/Subtype/Image/BitsPerComponent 8/Width 840/Length 8784/Height 141/Filter/FlateDecode&gt;&gt;stream
  xíyTÅñÇgO.åX@ASE!à
  FC¼¢"r,"&amp;A7¢ á'7b4A5Ê­"7xSÈ" 7Ë²°L=æxÝõ­ê~o_òý{º^uÏôt¿útWù|:öÛÐé½&gt;5øæË´rCÕß}üò|«Oîj&gt;jÛÍ÷ÀMû¶~Ø§¨Û¿&amp;ñþÇe«ÆëqS©Ó3ò$O5Õ¥SuN:­RÜo­zûÙ¤nïË¾ºà¾mûÕ'±6hûìbäË;é§.§l^æS÷Y31½Q²Àë&amp;O5ÔÆ÷Õx°îâ©ÍãÛ±è¢NlÂý:aóÉ_·:hÙ+h ÿÌÐCÝ¼Ñ7Æþç®zý¥Wùñc´cRñG(qº+.æ¼ÑÚ:ÀqjÍG7þÊú m*tÓ97©§ï±âÿÁùVd¹]5ÆÉïÏÖÞÎ/y·KÞùTe+êÆrizgÃ×
  \´óÛH|©ìË&gt;o¨nÿµ7¼ûr¦Rí­¶,×U%c¡ó\ô0û³]?©ª¼½Õ¦&amp;}¿Köï»Ù¾´v·Ó.P·ÿÀf~ý°9¾[j½Ëæ£¥QßôU)ÞBÔI#gR7ûî·÷èrãÝ±3Z×ëÌY´ÕêæUWXîÁ^eQ×&lt;jùÙ2M«g6»ìàOù§UíÕ&lt;_òFY|ö&lt;wÆ+_ðy    °ÙMÝü-Ö{plx%Ð÷i§¬?\¤éF»»¤Unû·°%öáú²ÐÞÃ[ÿäÆ`êÀ¿Çd¡mk¡n?(Û.À3ùgèào¨¿&lt;|Ü?lçzò=ëî~ØïY÷åÊXPXZ?ôvw%!ÖÔ¦¦ÒÍ]\3/ü;9EèVÆ²Ùvµ§Û£°
  ýp+ìK§XRXV4BÜïwÈ­ç³5\Èzâ^Þ|övÌ C@Ç¶õpËÖI'ú¢Î&lt;
  XsÂ*5¹
  àÿòtFeñÄiù·©ºÄ=.ø§Ài¶îRNÐ¿¶Ü§°ÎÊlFû_åG`*««Ë&amp;1ÕÉúDºKâÅ
  [$Ö
  ¥ÑÐ©µhKÞPXG=uÙû]õ'ÉÞÎM
  ¡M(?ÈÐF[$Ö-
  ®}_Zn¤-yFa4ïB²ó]v¥©ìs8qw©N/áz§SéÇENØ"±.RØ0A©o   £°QÚÚêBmnûÀÑä¥.T®SØPÅôN§&amp;§E&gt;Ø"±®RØPí¾vf"`Ç[
  ©½]&gt;taÅHîSØPý3Ï;.xWè%ë.
  ÕâK)_gÒV&lt;§°º`Iß{à.æDJ&gt;æ©w»¬Ýðë$uÁuÂhEÙ¯.ÄÂFH%*ÔÀHÞPØbY{Eª%^G-X×)l±r»¾tÚA    
  ×ô¹7n âL¤xö5Cê;§Qéþb[Ù[z@aEíìJ0
  1zxBpÍÁÃßQØ"m+
  ;§QÃÃr¬X/(l±é(ìXQØ0é¯üõd½£°:ú¦QÅÑ,¶àG¶P7ÖùPØ/bGaC´ÿjí ¦°Éß="ä!-©VH¬7¶HÚ ¡°3bHaCYO;ª%mAò§aÉK
  [¨£B®éTc»î2vÀ3
  [¨Ï£q¡°è&gt;
  Ñ\íá°!@O&lt;Õvì`§¶P_B®ixÑómØZ^QØBéÂ3ý½º¹6DCtçðÒD³¡W$o)lö¶C\ÓèRÑÁºbÍ6|þµòÂèmõgÂ^«î'¶X­tã:ê¸§¾ºÙÙyLa:&gt;ðL£s1tÀílc 8ú¡ÒÂ.9-RfÍ¸¦÷½ÕQ`gç5-Ð÷´g:µ2½óeÄ¶2=¬ô¡°cbOa4X·³3zóuEÀÎ¡°§&gt;¡Õss2w;ëitþ;²ñ(9MÍ×+Çlðë+3¡ÍëçªA(lOu_öû1Q?'g-_´á¯j©ÙG0øÃÈgô¿I¦þêWÍÓèBa]EY9£Kû£PïL*eaKeLbó%ý¨;7ÛcÜýL±C(¬fîLèÿSU¸¢óÈµ'ñN5G¯¤&lt;ù[ybh¹Ú¬»;°\
  {LEwáxt@O¾fÐåºÀ§hdLbÛG*'#½Ú¡N
  ÕdPX-)¸àÑ¯rÁNèv5dc×ºÁÔú/á/ÔPØ_áîÔBAAä»ô£à3t2&amp;±½ÆLµDÙ£êÉÂÎÝèD«©,ð³\óÙØ]D¸ÃÕKú7îÃDF`ÂægøSv6[ä]n¬ÛÉ¢ÚÕUî@@åÆÑÏØ®ûÓ
  UW1§G*ÚRØetó5tr×+&gt;á|1±6v»íVó¦íÄPØãw²&lt;
  £&lt;ÐPüÑé÷Wéóª5~ÆÚ#q¡Jf¥_h0¤°ô9ÉP¤Ð+nA¢7v-gïFî­kkÛ'q¶¬º,*ÎEq7y[_¥¶yZHBa?Äø¼ÈRØí½#Wßô×,HôÆnåu«I¿õR@abútÉ`Å»T`æç|VMè@PHÈpÃÞµÀ+*`×ÄÂ"&lt;ÊÛFÃ§4!;ÅÞf­^¾.Í!ÝÕ¾!Å5¥û{êC¦SÐÎN:JA¼O'ÓÊrhPØ,VZ")æÑöºùÏðöáäK@üjk9Ì0Xê¤xàìÿÇ¸^]¢tÕöSkêWe9G(ì¿YÚÔdóM&lt;ä~{Oõ®¥ÏÁË©Ãi¤½CWÙl¶Rr!%¼E·(l* ýó¨Â(ÚôÑ'¡w¨·D¡°ÓCTí}Ú¢ ½B·tÂ:hXìT¥ êé»¶+DðîU/HÑD)l±Ê¾@[Î£6@?óù®¾ÊE"
  PXf}´EçytáÇtKW)l¤þ^®m£´Fº¼Ç.
  ×ü@=ªÒ­9¶@IwÓfeó¨ÚÛ´åwø|©à_ÃC¡°Cx&amp;¦-:Ï£k?C·)l*.,úu»dâ´ Uo(CßqíéÆ,
  {óÉX7ãÌçR¢²ÓÄ"vßx6Oßú¸Oa×òJlvÀn-Wo|õù·vÛ-·Ð&lt;Õ_µ Å?G7æQØ ® ÍæQ} ôæüU÷*ð @ýyòGÎñ:áRØÌî(:ÐJ¯jÚÙ9ìµÚB¯oTó(8
  Ç¤°¹µJÛÍy!ÿ£-éOXÂÎFkH:ìÌ¦öqæ;r@´ÎèJ
  ÔKôBGÔô~õÆ. Øp)¬GsAó[k{îßÒ5 ±Ç´ EtGM4z¦¯®Y ?AÁï5×kcv)ì$8ç¢¤+öJ?IÛÌ#$È°7=øÙ@ù{Ýe2½
  U-
  QmÓñ|(§.lAúZI¥êiß°R¹îèT
  èg=É¦°&gt;_ùé´]ÁyïÛÎ/ø,2å$¢°x6AsGPØ¹©¼¾øÀ)ï2e{Ý~Â.íÊ¸Ñë|`¡°ÇÙÖç«
  ûå'h¨¹¶º­(Bb_âw-(ûöÌN?ãÔÐ
  ;AaTJ®N¤Gv)ì\Æm
  Å¦°¾¸æ´Yþ}ØRÐVóg«$Ö
  ëë\(uºÇPØMÓq½Ùþ¼u&lt;TJÕ\°{ÄJ~ø¡ð(_o8@(ì
  ¾gÈU¾#ì=ÕeÀFa}ñ
  ¤ÀXÂ`Ú¢×¡°oRØë} bH#+nY¥°Ïs.Äíd¡°yøUv"Èi£    ùü$úóRëEîM8ë
  û¼zSQØËy}ÉiªLmù'õ   »lÍ
  @¾PxÓBasº°®MüÄ}Ulý$ôDn:ýy)E(ì
  ±qrmÂ1¡*Ba5sÚ
  ÔÀ1uÀN¬RØ0&lt;ÊWÍÖptæ¤:pÉvgØ¶§5àÄº@aÛ#é·ÂÝ¯¹Àá
  ØmUYÓÌ£QçHÜQ;¢Z¤ÝÎv¬&lt;RìcäI­tÀèÔ°&amp;wÁÄ$Ö&gt;®ç:½î#v¦+6_×
  ûÓkÂ&amp;3¿m¢°s¸aiN¸¯Gõ2iÂÍ5&gt;BPØ&lt;^¾VdWçüzPØqj
  [Ú
  /(ò­h@lªÈgÝÏÌ`m¢°3ýJ¸zmcRØrkÌHTøµËH¬}
  Ûú6'8m¯
  ûGõ]¢°ùBR
  æU
  »Ã-ÚDaY}ºA~å0ËM´bQo\HÁ&gt;mPØåcOØ4r®PXÍ¾¬»;6_AÌ#¶»^M(Mf¹U¬Iöo[wúÙu½£©[$¶]
  [æi,Åc=&gt;Âj*²þÅ%
  ryB0lRØ ì²¸Ñ'¬SØø&amp;hñøµ¼ÞÞ\Õª[$¶'àNa eJKBV¶2ß(l@½öÑ¶ó¨5Zø¿]F¤    Û6¾ÁÔf½òÔu´É-¿jV ±ä[åÆÐva
  [ý4îzØñ&gt;¥!­²n.¢°uþ6.Gc
  ±&lt;û ½nuáÁ£¢rÚ¥°¥º#çK
  Ä+W^
  H\dÈW:ÝNBbSgÓv1
  [ºÍ»x¸èóLRØö®QXkóÕcÊ³{EÃ/G·$Ò-
  xU»ûF°µt&gt;í§ÒåÀök§wÏÖ/{-p¹¤°U¯¹õñYÀ{V±JRØ®QX_¬çÑizË^UPÒ5j!ÖíÒ\ÎV
  õ cd!±Ëù7\+öåÑÂþ
  x²Âúb=&gt;T/,ÒãìògÑ    ¡°î¹!¢¹Db±z¶åãØÂ~D7RX_¬çQ;úÔÃzÖÕEØ(¬;â½Õ®·mwÞ¹ $övº+ec¸Rä6¥°kèæR
  ãy´&gt;ßØNéª#ñuEGY'f C~Æ:'!$vÇ±Þ-É9XgLa;n¥GêåúD'´(a$ÁBUxì¨N&amp;mqSMEcÄÎe¹ãÃ(¬u½á¼Ù/ÉG¢óÞè(C²,@a¥²bíam·ËI¯³±kÄþ«ÇFamkÝ5Î¾´0¤°ÈÑ|)Î3¸6fÓQ&lt;
  (ÜHÜõFnÃ©­+ëi $v«s)¥
  k]R8s   ¦°Ð&lt;Z¦Lc8ÑØGT
  7?ÌÈM±Àª
  ä¢ÚÝYÝ&gt;nýË#ÂÚbWçó!sÚÂ¾#O¥=¶®¾W®OLLieÀ£Â­Ä%,k·e8èvÀäLMûÖ@ûY, 
  kY_¨ZÉ@bL}øÏ ¥°Ð½   uBU³y42ãä7  Q¸°Ö]äe{¨Iü^·ï@HìRõÂVZcà)
  </code></pre>
  
  <p>Headers: </p>
  
  <pre><code>Remote Address:127.0.0.1:8070
  Request URL:http://localhost:8070/api/1/reports/email/download/RemediationStatusReport223937b2-e379-484e-8123-2918c5048a9f
  Request Method:GET
  Status Code:200 OK
  Response Headers
  view source
  Cache-Control:no-store
  Cache-Control:no-cache
  Content-Disposition:attachment; filename="RemediationStatusReport.pdf"
  Content-Transfer-Encoding:binary
  Content-Type:application/pdf
  Date:Thu, 28 May 2015 16:42:25 GMT
  Expires:Thu, 01 Jan 1970 00:00:00 GMT
  Pragma:no-cache
  Transfer-Encoding:chunked
  X-Content-Type-Options:nosniff
  X-Frame-Options:ALLOWALL
  X-XSS-Protection:1; mode=block
  Request Headers
  view source
  Accept:application/json, text/javascript, */*; q=0.01
  Accept-Encoding:gzip, deflate, sdch
  Accept-Language:en-US,en;q=0.8
  Connection:keep-alive
  Cookie:i18next=en; SESSIONID={3f9ba2d9-4fa0-4bb0-b4c9-13d6c207bc7f}1uiivf0cekzsfrbexwmwtgji1.{fa9e70b7-dde3-4f98-bc53-4c45040d8afc}
  CSRFToken:b8ab6f85-7248-40ac-929d-aca96ad18244
  Host:localhost:8070
  Referer:http://localhost:8070/
  User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
  </code></pre>
  
  <p>Any help appreciated. Thanks!</p>