有一个解决方案here,解释了如何将web worker与makepdf一起使用。但是,我想知道如何使用requirejs将此解决方案应用于角度。 我试图使用ngwebworker和requireJs作为RequireJS within webworker,但我没有成功。有人能帮助我吗?
修改: 为了更容易可视化问题,我将对此代码进行编辑:
$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
}
}
问题是,要将对象传输给Web worker,他们建议使用JSON.parse和JSON.stringify。但是,如果我在我的对象上使用它,我将松开方法“下载”。有没有办法将此对象发送到我的函数 downloadIt 而不会丢失pdfmake对象的下载方法?