tcr / scissors

PDF manipulation in Node.js! Split, join, crop, read, extract, boil, mash, stick them in a stew.
Apache License 2.0
285 stars 45 forks source link

Error Handling if Pdf path is not Found #25

Open janschmutz opened 7 years ago

janschmutz commented 7 years ago

Hi, how can I handle Errors, if the Pdf Input Path was not found?. I'd like to use the 'error' event, to handle the error. But instead it throws me this error:

'throw er; // Unhandled stream error in pipe.'

janschmutz commented 7 years ago
function createPdfCollection (sticker) {
var out = 'out.pdf';
var in = 'in.pdf';
var pdf = scissors(in)
    .pages(sticker)
    .pdfStream()
    .pipe(fs.createWriteStream(out))
    .on('finish', function(){
        console.log("Created new Pdf");
    })
    .on('error',function(err){
        console.log('Handle this: ' + err)
    });
}
cboulanger commented 7 years ago

@janschmutz Thanks for the report.

@tcr : in https://github.com/tcr/scissors/blob/master/scissors.js#L118 seqq., I see that errors thrown by non-existing files will be caught and ignored (it returns this.input). Why is that?

janschmutz commented 7 years ago

Thank you, also as you can see I'm passing a array of pages as arguments, this works. However calling pdf.pages.apply(pdf, args); doesn't. Es6 Spread pdf.pages(...args) works. might be worth a notice in the documentation

janschmutz commented 6 years ago

Any updates on this issue?

cboulanger commented 6 years ago

@tcr, in case you didn't see the question before - can you have a quick look, would make things easier to solve: in https://github.com/tcr/scissors/blob/master/scissors.js#L118 seqq., I see that errors thrown by non-existing files will be caught and ignored (it returns this.input). Why is that?

tcr commented 6 years ago

My best guess at what this code was doing (and this might be just a guess) was the following:

  1. Expand a relative filename argument to an absolute path, because some PDF programs (which?) required absolute paths. It may have been that they wrote output files relative to the first argument, rather than the current directory.
  2. Some programs need to create a temporary intermediate file, which doesn't exist when the script is first executed. This temporary intermediate file will always be an absolute path; thus we don't need to do the "fixing" step in step 1 for any reason, and can swallow its error.
  3. If the filename doesn't exist, we assume it will be created in the middle of program execution at some point. So just return it.

If I'm correct about the above, an easy fix would be to change the function signature to _input(suppressNonexisting: boolean). suppressNonexisting would be true for a Command which has had another stream piped to it. It would be false otherwise, which means the first Command checks whether the filename exists; the rest just assume it does.