90% of my usage of PaperJS is running as a nodejs process that generates bitmaps, and the method below is the technique I use.
(note - presumes that the file is always saved to a folder called ‘img’ which sits next to the script being run, and that the filename being passed in doesn’t have it’s suffix.
I’ve also included a JPEG method as you might want that too - the different outputs have slightly different renderings to them, so worth trying both to see which you prefer.
There could well be a better way of doing it, but this method has worked for me for a few projects.
function saveCanvasToPNGFile(filename, callback)
{
filename += ".png";
var fullpath = __dirname + '/img/'+filename;
// Saving the canvas to a file.
out = fs.createWriteStream(fullpath);
stream = paper.view.element.pngStream();
stream.on('data', function(chunk) {
out.write(chunk);
});
stream.on('end', function() {
console.log('saved png '+filename);
callback( fullpath, filename );
});
}
function saveCanvasToJPGFile(filename, callback)
{
filename += ".jpg";
var fullpath = __dirname + '/img/'+filename;
// Saving the canvas to a file.
out = fs.createWriteStream(fullpath);
stream = paper.view.element.jpegStream({quality: 100});
stream.on('data', function(chunk) {
out.write(chunk);
});
stream.on('end', function() {
console.log('saved jpeg '+filename);
callback( fullpath, filename );
});
}