Skip to content

Commit

Permalink
添加pdf功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ksky521 committed Sep 17, 2013
1 parent 9ba57ca commit 737bf44
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 25 deletions.
5 changes: 3 additions & 2 deletions bin/nodeppt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ var opts = {
'-v': '--version',
'-p': '--port',
'-d': '--dir',
'-f': '--file'
'-f': '--file',
'-o': '--output'
}

var argv = parseArgs(process.argv);
Expand All @@ -33,4 +34,4 @@ function parseArgs(args) {
}
return v;
});
}
}
42 changes: 40 additions & 2 deletions lib/nodePPT.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var gFile = grunt.file;
var gTpl = grunt.template;
var color = require('colors');
var read = require('read');
var exec = require('child_process').exec;

var libDir = __dirname;
var rootDir = join(libDir, '../');
Expand All @@ -29,6 +30,42 @@ var templateQ = [{
'default': '演讲者'
}];
var ppt = module.exports = {
pdf: function(args) {
var url = args.splice(0, 1).toString();

var self = this;
var output = '';

for (var i = 0, len = args.length; i < len; i++) {
switch (args[i]) {
case '--output':
output = args[i + 1] ? args[i + 1] : 8080;
i++;
break;
}
}
if (url === '') {
console.log('pdf need a URL'.red);
console.log('nodeppt pdf 用法:');
console.log('nodeppt pdf url -o a.pdf: 输出一个pdf');
return;
}
if (output === '') {
output = 'nodeppt.pdf';
}
if (output.slice(-4) !== '.pdf') {
output += '.pdf';
}
var child = exec('phantomjs ' + libDir + '/pdf.js ' + url + ' ' + output);
// child.stderr.setEncoding('utf8');
child.stderr.on('data', function(data) {
console.log('请安装phantomjs:npm install -g phantomjs'.red);
console.log('nodeppt pdf 功能需要 phantomjs 支持'.red);
});
child.stdout.on('data', function(data) {
console.log(data);
});
},
help: function() {

console.log(' ________________|'.bold.green + 'nodePPT帮助'.bold.red + '|_______________'.bold.green);
Expand All @@ -37,6 +74,7 @@ var ppt = module.exports = {
console.log(' | nodeppt start -p 8000: 启动8000端口 |'.bold.green);
console.log(' | nodeppt start -p 8000 -d ./ |'.bold.green);
console.log(' | nodeppt create fileName: 创建一个ppt |'.bold.green);
console.log(' | nodeppt pdf url -o a.pdf: 输出一个pdf |'.bold.green);
console.log(' |____________________________________________|'.bold.green);
},
start: function(args) {
Expand Down Expand Up @@ -70,7 +108,7 @@ var ppt = module.exports = {
// } else {

// }
// } else
// } else
if (!fs.existsSync(dir)) {
dir = join(self.cwd, dir);
if (!fs.existsSync(dir)) {
Expand Down Expand Up @@ -157,4 +195,4 @@ function writeFile(path, content) {
gFile.write(path, content, {
encoding: 'utf-8'
});
}
}
40 changes: 20 additions & 20 deletions lib/pdf.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
var page = new WebPage();
var system = require( 'system' );
var system = require('system');

page.paperSize = {
format: 'A4',
orientation: 'landscape',
margin: {
left: '0',
right: '0',
top: '0',
bottom: '0'
}
format: 'A4',
orientation: 'landscape',
margin: {
left: '0',
right: '0',
top: '0',
bottom: '0'
}
};
page.zoomFactor = 1.5;

var revealFile = system.args[1] || 'index.html?print-pdf';
var slideFile = system.args[2] || 'slides.pdf';

if( slideFile.match( /\.pdf$/gi ) === null ) {
slideFile += '.pdf';
var revealFile = system.args[1];
var slideFile = system.args[2];
// console.log(revealFile);
if (slideFile.match(/\.pdf$/gi) === null) {
slideFile += '.pdf';
}

console.log( 'Printing PDF...' );
console.log('Printing PDF...');

page.open( revealFile, function( status ) {
console.log( 'Printed succesfully' );
page.render( slideFile );
phantom.exit();
} );
page.open(revealFile, function(status) {
console.log('Printed succesfully');
page.render(slideFile);
phantom.exit();
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nodeppt",
"jsname": "nodeppt",
"description": "Create a PowerPoint presentation with Markdown",
"version": "0.4.5",
"version": "0.4.6",
"site": "https://github.com/ksky521/nodePPT",
"author": {
"name": "Theo Wang",
Expand Down

0 comments on commit 737bf44

Please sign in to comment.