File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,7 @@ PDF.prototype.toFile = function PdfToFile (filename, callback) {
84
84
}
85
85
86
86
PDF . prototype . exec = function PdfExec ( callback ) {
87
+ var callbacked = false
87
88
var child = childprocess . spawn ( this . options . phantomPath , [ ] . concat ( this . options . phantomArgs , [ this . script ] ) )
88
89
var stdout = [ ]
89
90
var stderr = [ ]
@@ -105,20 +106,29 @@ PDF.prototype.exec = function PdfExec (callback) {
105
106
return child . kill ( )
106
107
} )
107
108
108
- child . on ( 'exit' , function ( code ) {
109
+ function exit ( err , data ) {
110
+ if ( callbacked ) return
111
+ callbacked = true
109
112
clearTimeout ( timeout )
113
+ if ( err ) return callback ( err )
114
+ return callback ( null , data )
115
+ }
116
+
117
+ child . on ( 'error' , exit )
118
+
119
+ child . on ( 'exit' , function ( code ) {
110
120
if ( code || stderr . length ) {
111
121
var err = new Error ( Buffer . concat ( stderr ) . toString ( ) || 'html-pdf: Unknown Error' )
112
- return callback ( err )
122
+ return exit ( err )
113
123
} else {
114
124
try {
115
125
var buff = Buffer . concat ( stdout ) . toString ( )
116
126
var data = ( buff ) != null ? buff . trim ( ) : undefined
117
127
data = JSON . parse ( data )
118
128
} catch ( err ) {
119
- return callback ( err )
129
+ return exit ( err )
120
130
}
121
- return callback ( null , data )
131
+ return exit ( null , data )
122
132
}
123
133
} )
124
134
Original file line number Diff line number Diff line change @@ -114,6 +114,24 @@ test('allows custom html and css', function (t) {
114
114
} )
115
115
} )
116
116
117
+ test ( 'allows invalid phantomPath' , function ( t ) {
118
+ t . plan ( 3 )
119
+
120
+ var filename = path . join ( __dirname , 'invalid-phantomPath.pdf' )
121
+
122
+ var options = {
123
+ phantomPath : '/bad/path/to/phantom'
124
+ }
125
+
126
+ pdf
127
+ . create ( html , options )
128
+ . toFile ( filename , function ( error , pdf ) {
129
+ t . assert ( error instanceof Error , 'Returns an error' )
130
+ t . equal ( error . code , 'ENOENT' , 'Error code is ENOENT' )
131
+ t . error ( pdf , 'PDF does not exist' )
132
+ } )
133
+ } )
134
+
117
135
test ( 'allows custom page and footer options' , function ( t ) {
118
136
t . plan ( 3 )
119
137
You can’t perform that action at this time.
0 commit comments