2015-04-08 161 views
1

所以我試圖用名爲html-pdf的NPM包打印PDF到客戶端,本地一切都很好用。我使用了一種方法來做到這一點,我使用Meteor.call來調用服務器方法,然後從pdf中獲取緩衝輸出並將其粘貼到一個集合中。我使用autosubscribe來聽取當前用戶ID的新PDF,然後使用一個包將PDF保存到瀏覽器。重要的代碼片段如下:部署時使用npm包的流星錯誤

客戶端/ helpers.js

Meteor.autosubscribe(function() { 
PdfsCollection.find().observe({ 
    added: function(item){ 
     var blob = new Blob([item.pdf], {type: 'application/pdf'}); 
     console.log('new pdf found'); 
     saveAs(blob, 'test.pdf'); 
     PdfsCollection.remove({_id: item._id}); 
    } 
}); 
}); 

客戶端/模板/報價/ quotes.js:

Template.QuotesEdit.events({ 
"click #pdf" : function(event, template) { 

    var html = Blaze.toHTML(Blaze.With(template.data, function() { return Template.QuotePrint; })); 
    Meteor.call('createPDF', html, Meteor.userId()); 

} 
}); 

服務器/ pdf.js:

Meteor.methods({ 
createPDF: function(html, userId) { 
    var pdf = Meteor.npmRequire('html-pdf'); 

    pdf.create(html).toBuffer(Meteor.bindEnvironment(function(err,buffer) { 
     PdfsCollection.insert({userId: userId, pdf: buffer}); 
    })); 
    } 
}); 

導致錯誤的部分是當我實際調用pdf.create時。有兩個錯誤,在服務器日誌中我得到這個:

[Wed Apr 08 2015 03:23:16 GMT+0000 (UTC)] WARNING events.js:72 
throw er; // Unhandled 'error' event 
^ 
[Wed Apr 08 2015 03:23:16 GMT+0000 (UTC)] WARNING Error: write EPIPE 
at errnoException (net.js:905:11) 
at Object.afterWrite (net.js:721:19) 
[Wed Apr 08 2015 03:23:16 GMT+0000 (UTC)] ERROR Application crashed with code: 8 
[Wed Apr 08 2015 03:23:16 GMT+0000 (UTC)] NOTICE Starting application on port 9000 
[Wed Apr 08 2015 03:23:16 GMT+0000 (UTC)] INFO STATUS running -> starting 
[Wed Apr 08 2015 03:23:16 GMT+0000 (UTC)] INFO STATUS starting -> running 

,並在瀏覽器控制檯有這樣的:

GET https://ddp--3484-metcrm.meteor.com/sockjs/info?cb=qtrpq640d2 6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 w._start6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 (anonymous function)setTimeout (async)6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 n.delay6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 n.XHRLocalObject6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 X.doXhr6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 (anonymous function)setTimeout (async)6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 n.delay6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 X6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 q6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 T6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:34 a.extend._launchConnection6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:34 a.extend._retryNow6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:3 t.extend.withValue6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:3 n6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:3 e.bindEnvironmentsetTimeout (async)6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:3 t.extend.setTimeout6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:19 t.extend.retryLater6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:34 a.extend._retryLater6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:34 a.extend._lostConnection6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:34 a.extend._launchConnection.t.socket.onclose6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 r.dispatchEvent6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 (anonymous function)setTimeout (async)6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 n.delay6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 T._didClose6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 o._ir.onfinish6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 a.emit6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 X.doXhr.a.onfinish6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 a.emit6f6418a2bd63d77a57cb8a21c5b677d0f83c8d36.js:32 w._start.a.xhr.onreadystatechange 
oLxjjRxydCC5AuDig:1 XMLHttpRequest cannot load https://ddp--3484-metcrm.meteor.com/sockjs/info?cb=qtrpq640d2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://metcrm.meteor.com' is therefore not allowed access. The response had HTTP status code 503. 

我現在已經絞盡腦汁3小時,試圖找到源這是在我的本地服務器上工作,但不在Meteor上。這裏的兩個重要的包: meteorhacks:NPM(在流星使用NPM包) pfafman:filesaver(爲另存爲功能,我使用)

請任何幫助表示讚賞!

回答

0

我發現這實際上是由meteor.com主機上創建的臨時文件的文件權限造成的。我部署到Modulus.io並且所有工作都按預期工作。