1

There is a solution here that explains how to use web worker with makepdf. However, I want to know how to apply this solution to angular using requirejs. I have tried to use ngwebworker along with requireJs, as the RequireJS within webworker, but I had no success. Can someone help me?

EDIT: To make it easier to visualize the problem, I will put on this edit the code that it is not working:

                                           $scope.exportToPDF = function(){

                    var list = $rootScope.orderedData;
                        var body = [];

                            var headers = new Array();
                            headers.push({ text: 'A', fillColor: '#0075E5', color: '#ffffff'});
                            headers.push({ text: 'B', fillColor:#0075E5', color: '#ffffff'});
                            headers.push({ text: 'C', fillColor:#0075E5', color: '#ffffff'});
                            headers.push({ text: 'D', fillColor:#0075E5', color: '#ffffff'});
                            headers.push({ text: 'E', fillColor:#0075E5', color: '#ffffff'});
                            headers.push({ text: 'F', fillColor:#0075E5', color: '#ffffff'});
                            body.push(headers);

                            for (var key in list) 
                            {
                                if (list.hasOwnProperty(key)){
                                    var position = list[key];
                                    var fila = new Array();
                                    fila.push( { text: position.a.toString(), fillColor: '#ffffff' } );
                                    fila.push( { text: position.b.toString(), fillColor: '#ffffff'}  );
                                    fila.push( { text: position.c.toString(), fillColor: '#ffffff'} );
                                    fila.push( { text: position.d.toString().substring(0,5) + "...", fillColor: '#ffffff'}  );
                                    fila.push( { text: position.e.toString(), fillColor: '#ffffff'} );
                                    body.push(fila);
                                }
                            }
                            var dd = {
                                    background: [{
                                        width: 30,
                                        alignment: 'left'
                                    }],
                                      pageOrientation: 'landscape',
                                      footer: function(currentPage, pageCount) { return currentPage.toString() + '/' + pageCount;},
                                      info: {
                                            title: 'Title',
                                            author: 'Me'
                                          },
                                content: [
                                          { text: 'Text', style: 'header' },
                                    {
                                        style: 'tableHeader',
                                        table: 
                                        {
                                            dontBreakRows: true,
                                            headerRows: 1,
                                            widths: ['5%', '10%', '10%', 'auto', '15%', 'auto', 'auto', '10%', '10%', 'auto', '4%', '10%'],
                                            body: body
                                        }
                                    }
                                ],
                                styles: {
                                    header: {
                                        fontSize: 18,
                                        bold: true,
                                        margin: [0, 0, 0, 10],
                                        alignment: 'center'
                                    },
                                    subheader: {
                                        fontSize: 16,
                                        bold: true,
                                        margin: [0, 10, 0, 5]
                                    },
                                    tableExample: {
                                        margin: [0, 5, 0, 15]
                                    },
                                    tableHeader: {
                                        bold: true,
                                        fontSize: 9,
                                        color: 'black',
                                        margin: [0, 5, 0, 15]
                                    }
                                },
                                defaultStyle: {
                                },
                            };
                              var obj =  pdfMake.createPdf(dd) // FAST PROCESS!

                              var myWorker = Webworker.create(downloadIt);

                              myWorker.run(obj).then(function(body) {
                                });


                   function downloadIt(obj) {
                       obj.download("Title.pdf"); // REALLY SLOW PROCESS THAT SHOULD BE IN BACKGROUND
                   }


                   }

The problem is that to transfer an object to a web worker, they suggest to use JSON.parse and JSON.stringify. However, if I use that on my object, I will loose the method "download". Is there a way to send this object to my function downloadIt without loosing the method download of pdfmake object?

Community
  • 1
  • 1
Gabriel
  • 952
  • 10
  • 31

0 Answers0