title: How to create pdf file from server response?
body: <p>I am working in an application in which I need to create pdf file from response which I am getting from server. Is there any way to create pdf using this response? The response looks like this:</p>
  
  <pre><code> %PDF-1.4
      %����
      2 0 obj
      &lt;&lt;/Type/XObject/Subtype/Image/Width 422/Height 119/Length 72/ColorSpace/DeviceGray/BitsPerComponent 8/Filter/FlateDecode&gt;&gt;stream
      x���
       ��o7����[qD
      endstream
      endobj
      3 0 obj....
  .......
  </code></pre>
  
  <p>Below is the method which I am using to convert it to pdf but I am getting error of BAD-BASE64</p>
  
  <pre><code>private void convertBase64StringToPdfAndStoreIt(String base64PDf) throws IOException {
      final int notificationId = 1;
      String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
      final File dwldsPath = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_DOWNLOADS) + "/FileName_" + currentDateTime + "_.pdf");
      byte[] pdfAsBytes = Base64.decode(base64PDf.replaceFirst("application/pdf", ""), 0);
      FileOutputStream os;
      os = new FileOutputStream(dwldsPath, false);
      os.write(pdfAsBytes);
      os.flush();
  
      if(dwldsPath.exists()) {
          NotificationCompat.Builder b = new NotificationCompat.Builder(InspectionformWebView.this, "MY_DL")
                  .setDefaults(NotificationCompat.DEFAULT_ALL)
                  .setWhen(System.currentTimeMillis())
                  .setContentTitle("MY TITLE")
                  .setContentText("MY TEXT CONTENT");
          nm = (NotificationManager) InspectionformWebView.this.getSystemService(Context.NOTIFICATION_SERVICE);
          if(nm != null) {
              nm.notify(notificationId, b.build());
              Handler h = new Handler();
              long delayInMilliseconds = 5000;
              h.postDelayed(new Runnable() {
                  public void run() {
                      nm.cancel(notificationId);
                  }
              }, delayInMilliseconds);
          }
      }
  }
  </code></pre>