2015-04-03 8 views
6

Sono nuovo su node.js e jsreport, ma quello che sto tentando di fare è creare un pdf in memoria usando node.js e poi salvandolo sul disco. Ho bisogno che questo sia in stand-by perché funzionerà come una funzione AWS Lambda.Come si salva un rendering jsreport su file con nodeJs?

var fs = require('fs'); 
require("jsreport").render("<h1>Hi there!</h1>").then(function(out) { 
    //pipe pdf with "Hi there!" 
    fs.writeFile('C:\\helloworld.pdf', out, function (err) { 
     if (err) return console.log(err); 
     console.log('Hello World > helloworld.txt'); 
    }); 
fs.close(); 
    console.log("The End"); 
}); 

Anche se questo corre il pdf uscita non viene aperto in Adobe Reader in modo da assumere l'output file non è un PDF valido.

questo richiede npm installare jsreport

+0

FYI '.writeFile' è asincrona. –

risposta

5

Da quanto ho capito dal sito web jsreport (anche se non sono stato in grado di verificare, in quanto nessuno degli esempi sul loro sito web di lavoro per me), sembra che out isn dati resi (PDF), ma un oggetto che contiene, tra le altre cose, un flusso.

Il che mi porta a credere che questo potrebbe funzionare:

require("jsreport").render("<h1>Hi there!</h1>").then(function(out) { 
    out.result.pipe(fs.createWriteStream('c:\\helloworld.pdf')); 
});