Pdfmake: open function won't work in a callback

Created on 23 May 2016  路  9Comments  路  Source: bpampuch/pdfmake

I can't seem to use the open function after any callback. I tried to use the function after an ajax call and after using SVG.toDataURL, in both cases i get "undefined is not an object (evaluating 'e.location')"

bug

Most helpful comment

Implemented by commit https://github.com/bpampuch/pdfmake/commit/5f4076ff0e4738134664f75108e34b980993452f.

open() example:

$scope.generatePdf = function() {
  // create the window before the callback
  var win = window.open('', '_blank');
  $http.post('/someUrl', data).then(function(response) {
    // pass the "win" argument
    pdfMake.createPdf(docDefinition).open({}, win);
  });
};

print() example:

$scope.generatePdf = function() {
  // create the window before the callback
  var win = window.open('', '_blank');
  $http.post('/someUrl', data).then(function(response) {
    // pass the "win" argument
    pdfMake.createPdf(docDefinition).print({}, win);
  });
};

All 9 comments

please post some line of code

Same issue here.
This code does not work. Gives;

pdfmake.min.js:110 Uncaught TypeError: Cannot read property 'location' of undefined

           $.post(site_url, form_data, function(json) {

                var pdfContent = {
                    content:[
                        "First paragraph",
                        "Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines"
                    ]
                };
                pdfMake.createPdf(pdfContent).open();

            }, "json");

But it is working outside of the ajax callback function.

Works only download() method.

This is a temporary solution:
I solved this issue modified the build code ("open" method).

// add the "win" argument
Document.prototype.open = function(win) {
  // if don't exist a window, create one
  if (!win) {
    win = this._openWindow();
  }
  try {
    [...]
  } catch (e) {
    win.close();
    throw e;
  }
};

Usage

$scope.generatePdf = function() {
  // create the window before the callback
  var win = window.open('', '_blank');
  $http.post('/someUrl', data).then(function(response) {
    // pass the "win" argument
    pdfMake.createPdf(docDefinition).open(win);
  });
};

@raky291 Did you submit a pull request? I would love to have this in the official release.

Implemented by commit https://github.com/bpampuch/pdfmake/commit/5f4076ff0e4738134664f75108e34b980993452f.

open() example:

$scope.generatePdf = function() {
  // create the window before the callback
  var win = window.open('', '_blank');
  $http.post('/someUrl', data).then(function(response) {
    // pass the "win" argument
    pdfMake.createPdf(docDefinition).open({}, win);
  });
};

print() example:

$scope.generatePdf = function() {
  // create the window before the callback
  var win = window.open('', '_blank');
  $http.post('/someUrl', data).then(function(response) {
    // pass the "win" argument
    pdfMake.createPdf(docDefinition).print({}, win);
  });
};

Is it true that this solution can not be used unless application is powered by AngularJS?
If not, how could I implement it in pure JS?

This is the only solution to this issue i've found yet.

No., Used framework/technology for async process is not related to pdfmake and open().

It is important at begin of action create/open new window and then pass this parameter to pdfmake open(). See readme.

not work

I used new version. I read node_modules/pdfmake/build/pdfmake.js and same to https://github.com/bpampuch/pdfmake/commit/5f4076ff0e4738134664f75108e34b980993452f

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Christian24 picture Christian24  路  3Comments

einfallstoll picture einfallstoll  路  3Comments

imoum007 picture imoum007  路  3Comments

Masber picture Masber  路  3Comments

dgrice picture dgrice  路  3Comments