title: How to get AngularJS BLOB to Download PDF?
body: <p>Hello everyone I am really new to developing with AngularJS and I am trying to figure out how to use BLOB to download a PDF locally to a machine. I already got it to work with a JSON and now I need a PDF. I have written some code but it doesn't seem to be working. </p>
  
  <p><strong>html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;style&gt;
          .center {
              position: absolute;
              left: 50%;
              bottom: 50%;
          }
  
          .btn-purple {
              background-color: rgb(97, 34, 115);
              width: 100px;
          }
  
      &lt;/style&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;!-- Latest compiled and minified CSS --&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
            integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"&gt;
  &lt;/head&gt;
  
  
  &lt;body&gt;
  &lt;div class="center" ng-controller="jsonController" ng-app="app"&gt;
      &lt;a style="color: white;" ng-href="{{ fileUrl }}" download="{{fileName}}"&gt;
          &lt;button type="button" class="btn btn-purple"&gt;{{fileName}}&lt;/button&gt;
      &lt;/a&gt;
  &lt;/div&gt;
  
  &lt;div class="center" ng-controller="pdfController" ng-app="app"&gt;
      &lt;a style="color: white;" ng-href="{{ fileUrl }}" download="{{fileName}}"&gt;
          &lt;button type="button" class="btn btn-purple"&gt;{{fileName}}&lt;/button&gt;
      &lt;/a&gt;
  &lt;/div&gt;
  
  
  &lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/javascript-canvas-to-blob/3.1.0/js/canvas-to-blob.js"&gt;&lt;/script&gt;
  &lt;script src="app.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>controller.js</strong></p>
  
  <pre><code>var app = angular.module('app', []);
  
  app.config(['$compileProvider', function ($compileProvider) {
      $compileProvider.aHrefSanitizationWhitelist(/^\s*(|blob|):/);
  }]);
  
  app.controller('jsonController', function ($scope, $window, $http, $log) {
      $http.get('data.json')
          .success(function (info) {
              var data = angular.toJson(info, true);
              data = data.replace(/\n/g, "\r\n")
              console.log(data)
              var blob = new Blob([data], {type: "octet/stream"}),
                  url = $window.URL || $window.webkitURL;
              $scope.fileUrl = url.createObjectURL(blob);
              $scope.schemaName = "test"
              $scope.fileName = $scope.schemaName + ".json"
          })
  });
  app.controller("pdfController", function ($scope, $http, $log, $sce) {
      $http.get('data.json' + $stateParams.id,
          {responseType: 'arraybuffer'})
          .success(function (response) {
              var file = new Blob([(response)], {type: 'application/pdf'});
              var fileURL = URL.createObjectURL(file);
              $scope.content = $sce.trustAsResourceUrl(fileURL);
          });
  });
  </code></pre>