0

I'm trying to create a PDF and push to user in a standalone node-webkit app. Generating PDF is ok but i'm not able to send it to user. I'have followed indication find here : http://pdfkit.org/demo/browser.html .

Here my controller :

paiApp.controller('pdfCtrl', function($scope) {
  var PDFDocument = require('pdfkit');
  var blobStream  = require('blob-stream');

  var doc = new PDFDocument;
  stream = doc.pipe(blobStream());
  doc.fontSize(15).text('Hello World');
  doc.end();

  stream.on('finish', function() {
    iframe = document.getElementById('myIframe');
    iframe.src = this.toBlobUrl(); //Error here
    // blob = stream.toBlob('application/pdf') //Error ...
  });


  // Writting PDF is working
  // ###################################
  // var PDFDocument = require('pdfkit');
  // var fs = require('fs');

  // doc = new PDFDocument;
  // doc.pipe(fs.createWriteStream('output.pdf'));
  // doc.fontSize(15);
  // doc.text('Generate PDF coool!');
  // doc.end();
  // #####################################

});

Here my error :

Object [object Object] has no method 'toBlobUrl'

Any idea ?

1 Answer 1

1

I struggled with this as well but recently got it to work for my app. I'm just intending to display a dynamically generated PDF in the browser in a window - so I used an <object> -- my client controller code is such:

$http({method: 'GET', url: 'SERVERCONTROLLER', responseType: 'arraybuffer'})
.success(function(response){
    var file = new Blob([response], {type: 'application/pdf'}));
    var fileURL = URL.createObjectURL(file);
    $scope.mycontent = $sce.trustAsResourceUrl(fileURL);
});

Make sure you remember to inject $sce to your controller and you should be good to go.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer helped as well as this blog post: notjoshmiller.com/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.