title: Writing PDF content in Response
body: <p>I am writing PDF byte array in response but when I download the file, its showing blank. Means no content. Here what I have tried. How to resolve that. I have written following servlet to read pdf file and then send that pdf file as response but I am getting blank PDF. </p>
  
  <pre><code>    @WebServlet("/downloadTest")
  public class TestServlet extends javax.servlet.http.HttpServlet {
  
      private static final long serialVersionUID = 1L;
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          performTask(request, response);
      }
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
              IOException {
          performTask(request, response);
      }
  
      private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
              IOException {
  
          File pdfFile = new File("D://Tempo.pdf");
          response.setContentType("application/pdf");
          response.addHeader("Content-Disposition", "attachment; filename=" + "Tempo");
          response.setContentLength((int) pdfFile.length());
  
          FileInputStream fileInputStream = new FileInputStream(pdfFile);
          OutputStream responseOutputStream = response.getOutputStream();
          int bytes;
          while ((bytes = fileInputStream.read()) != -1) {
              responseOutputStream.write(bytes);
          }
          fileInputStream.close();
      }
  }
  </code></pre>
  
  <p>I am getting something like below in response.</p>
  
  <pre><code>%PDF-1.4
  %âãÏÓ
  3 0 obj
  &lt;&lt;/Length 708/Filter/FlateDecode&gt;&gt;stream
  xTMSÛ0½ûWèHQµú´nup¡ÍôI;Ð3þ}%%4v¼pÈLF»zoßê=?'ã2¥I¹Lò¾FÊ_ÉAFNªü¨%¹ÌÈ²¾}y¬Zú¥üí®°ÍMú?2¹JÃSw~0ªÈ_Ï¢­!J!IS%ÅæÜ2ü¤ZàãÁ"ÜBGÈ%DÈ¥+#wÜFùs×±£;ºÑ×n¬t£!ºÑÝ8ÑJàº±ó -xÝX!èÆ
  A7ZÐò -èyÐÝ/\ù0ñ"uçÆáÝ8ÝÖ/Omó:¿ðáykV@ æl6ñE¤å4Þb¹lªÕj~&gt;íSÔ-á
  ñb2!$Hý)Le)S]L&gt;ÄäSc1Ý÷­ÍºÕÛ)Õ\ÉÈ/í]Ý8ÀaÔn'ÄÖ(ÀÍ½ËpÒÅ`©°[¼lz jIqG÷(´ 2t\'ó&lt;+{© Ú¸o-ErkøY×vV"ÿh%Òµà.oÆ=&lt;#ÖV]Ow&gt;-¼^Gú·ùKQqCD¬ÚE[­v$(ùá«*6ù\4"'6N/òiIR®\¿îwgy~=)y?I&lt;ôº'^e)ÝRéd$J(äÉlg½WáÒ-`cAÒ¶¢³E{÷Jûªi»¥þû»;ÄYñí¬ ß{Îåöÿ3,(è¹Ï{:&gt;â±¦Ê%v«8&lt;½Ä¬6sÆÔ¥P&lt;¬nxéøæ4»^G5û~ÊlZîo6¥ö2Û^³u!ß1Û2j¶
   j6ÍÙ+WMKÆ&gt;äq«íµÚfÕì`-ûÈjÃ[ïXmØ¼ÕúVû(@qQ
  endstream
  endobj
  5 0 obj
  &lt;&lt;/Type/Page/MediaBox[0 0 612 792]/Resources&lt;&lt;/Font&lt;&lt;/F1 1 0 R/F2 2 0 R&gt;&gt;&gt;&gt;/Contents 3 0 R/Parent 4 0 R&gt;&gt;
  endobj
  1 0 obj
  &lt;&lt;/Type/Font/Subtype/Type1/BaseFont/Times-Bold/Encoding/WinAnsiEncoding&gt;&gt;
  endobj
  2 0 obj
  &lt;&lt;/Type/Font/Subtype/Type1/BaseFont/Times-Roman/Encoding/WinAnsiEncoding&gt;&gt;
  endobj
  4 0 obj
  &lt;&lt;/Type/Pages/Count 1/Kids[5 0 R]&gt;&gt;
  endobj
  6 0 obj
  &lt;&lt;/Type/Catalog/Pages 4 0 R&gt;&gt;
  endobj
  7 0 obj
  &lt;&lt;/Producer(iText® 5.5.10 ©2000-2015 iText Group NV \(AGPL-version\))/CreationDate(D:20170615141342+05'30')/ModDate(D:20170615141342+05'30')/Author(KSHITIJ)/Creator(Rishabh Software)/Title(Title)/Subject(Subject)&gt;&gt;
  endobj
  xref
  0 8
  0000000000 65535 f 
  0000000911 00000 n 
  0000001000 00000 n 
  0000000015 00000 n 
  0000001090 00000 n 
  0000000790 00000 n 
  0000001141 00000 n 
  0000001186 00000 n 
  trailer
  &lt;&lt;/Size 8/Root 6 0 R/Info 7 0 R/ID [&lt;8fa4882fe3f73b7d1a30457c18d686a4&gt;&lt;8fa4882fe3f73b7d1a30457c18d686a4&gt;]&gt;&gt;
  %iText-5.5.10
  startxref
  1416
  %%EOF
  </code></pre>