@@ -8,27 +8,27 @@ path = require('path');
88
99phantomjs = require ( 'phantomjs' ) ;
1010
11- script = path . join ( __dirname , 'scripts/ pdf_a4_portrait.coffee' ) ;
11+ script = path . join ( __dirname , 'scripts' , ' pdf_a4_portrait.coffee') ;
1212
1313exports . create = function ( string , options , callback ) {
14- var child , stderr , stdout , timeout ;
14+ var child , content , stderr , stdout , timeout ;
1515 if ( arguments . length === 2 ) {
1616 callback = options ;
1717 options = { } ;
1818 }
1919 if ( ! ( string != null ? string . length : void 0 ) ) {
20- return callback ( new Error ( "Can't create pdf without content" ) ) ;
20+ return callback ( new Error ( "html-pdf: Can't create a pdf without content" ) ) ;
2121 }
22- child = spawn ( phantomjs . path , [ options . script || script , string . length , JSON . stringify ( options ) ] ) ;
22+ child = spawn ( phantomjs . path , [ options . script || script ] ) ;
2323 stdout = [ ] ;
2424 stderr = [ ] ;
2525 timeout = setTimeout ( function ( ) {
2626 child . stdin . end ( ) ;
2727 child . kill ( ) ;
2828 if ( ! stderr . length ) {
29- return stderr = [ new Buffer ( 'PDF creation timeout. PDF generation script did not end .' ) ] ;
29+ return stderr = [ new Buffer ( 'html-pdf: PDF generation timeout. Phantom.js script did not exit .' ) ] ;
3030 }
31- } , parseInt ( options . timeout ) || 10000 ) ;
31+ } , parseInt ( options . timeout ) || 30000 ) ;
3232 child . stdout . on ( 'data' , function ( buffer ) {
3333 return stdout . push ( buffer ) ;
3434 } ) ;
@@ -38,30 +38,33 @@ exports.create = function(string, options, callback) {
3838 return child . kill ( ) ;
3939 } ) ;
4040 child . on ( 'exit' , function ( code ) {
41- var error , file , filename , isFile ;
41+ var error , file , filename , isFileBuffer ;
4242 clearTimeout ( timeout ) ;
4343 if ( ( stderr . length || code ) > 0 ) {
44- error = new Error ( Buffer . concat ( stderr ) . toString ( ) ) ;
44+ error = new Error ( Buffer . concat ( stderr ) . toString ( ) || 'html-pdf: Unknown Error' ) ;
4545 return callback ( error ) ;
4646 }
4747 file = Buffer . concat ( stdout ) ;
48- if ( isFile = / ^ \% P D F / . test ( file . slice ( 0 , 4 ) . toString ( ) ) ) {
49- return callback ( null , file ) ;
50- } else {
48+ isFileBuffer = / ^ \% P D F / . test ( file . slice ( 0 , 4 ) . toString ( ) ) ;
49+ if ( options . filename ) {
50+ return callback ( null , file . toString ( ) ) ;
51+ } else if ( ! isFileBuffer ) {
5152 filename = file . toString ( ) ;
5253 return fs . readFile ( filename , function ( err , buffer ) {
5354 if ( err ) {
5455 return callback ( err ) ;
5556 }
56- if ( ! options . filename ) {
57- return fs . unlink ( filename , function ( err ) {
58- return callback ( err , buffer ) ;
59- } ) ;
60- } else {
57+ return fs . unlink ( filename , function ( err ) {
6158 return callback ( err , buffer ) ;
62- }
59+ } ) ;
6360 } ) ;
61+ } else {
62+ return callback ( null , file ) ;
6463 }
6564 } ) ;
66- return child . stdin . write ( string , 'utf8' ) ;
65+ content = {
66+ html : string ,
67+ options : options
68+ } ;
69+ return child . stdin . write ( JSON . stringify ( content ) + '\n' , 'utf8' ) ;
6770} ;
0 commit comments