NickNaso / ghostscript4js

Ghostscript4JS binds the Ghostscript C API to the Node.JS world.
http://www.nacios.it
Apache License 2.0
68 stars 19 forks source link

Suppress console output #32

Closed lukassteiner closed 4 years ago

lukassteiner commented 6 years ago

Is there a way to suppress the console output of ghostscript? When I convert a pdf to jpg like this:

const gs = require('ghostscript4js');
const command = `-r300 -sPAPERSIZE=a4 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dUseTrimBox -sDEVICE=jpeg -dDownScaleFactor=3 -o out-%d.jpg -dJPEGQ=100 -f in.pdf`;
await gs.execute(command);

I get this output in the console:

GPL Ghostscript 9.21 (2017-03-16) Copyright (C) 2017 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. Processing pages 1 through 2. Page 1 Page 2

When I use the gs command directly, I usually just add the "-q" flag.

NickNaso commented 6 years ago

Hi @lukassteiner, try to execute this command

const gs = require('ghostscript4js');
const command = `-psconv -q -r300 -sPAPERSIZE=a4 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dUseTrimBox -sDEVICE=jpeg -dDownScaleFactor=3 -o out-%d.jpg -dJPEGQ=100 -f in.pdf`;
await gs.execute(command);

It should solve your problem. Send me a feedback if it works for you.

lukassteiner commented 6 years ago

Hi @NickNaso Thanks a lot for the command! This works very well.

I just have a case with a specific pdf where I get this output even with your command:

Error: stream operator isn't terminated by valid EOL. Output may be incorrect. Error: stream operator isn't terminated by valid EOL. Output may be incorrect. Error: stream operator isn't terminated by valid EOL. Output may be incorrect. Error: stream operator isn't terminated by valid EOL. Output may be incorrect.

I am not able to publish the pdf publicly but I will send it to you per email. Maybe you know a way to suppress that output as well.

NickNaso commented 6 years ago

Hi @lukassteiner, I just received your pdf and in next couple of days I will investigate the reasons behind your error and I will answer here.

freewil commented 6 years ago

I'm having a similar issue where I have GS repair corrupted PDFs. It'd be great to find a way to suppress this:

**** Error: Encountered 'obj' while expecting 'endobj'.
               Treating this as a missing 'endobj', output may be incorrect.
NickNaso commented 6 years ago

Hi @freewil, do you obtain this error only the addon? Could you try to execute the command directly with the ghostscript command line over the same file and report if you obtain the same error? Thank you.

freewil commented 6 years ago

@NickNaso I receive the error when running it with the addon via node and via the command-line directly from my shell. From my shell, I can confirm GS is outputting the error/warning to stdout. Since GS is processing a corrupted PDF, it's to be expected that it outputs the "error" or "warning" while processing. GS still successfully creates the output PDF, it's really just a warning, but it would be nice to somehow suppress its output via output redirection to another stream (like /dev/null) or file descriptor.

I tried to create a corrupted PDF without sensitive information that I'm working with for an example, but was unable to immediately do so.

This is the command I am running:

(async () => {
  const cmd = ['-psconv', '-q', '-o', repairedFile, '-sDEVICE=pdfwrite', '-dPDFSETTINGS=/prepress', corruptedFile]
  await gs.execute(cmd)
})()
$ gs -q -o repaired.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress corrupted.pdf
   **** Error: Encountered 'obj' while expecting 'endobj'.
               Treating this as a missing 'endobj', output may be incorrect.
NickNaso commented 6 years ago

Hi @freewil, thanks for your feedback now I'm sure that the problem is in the redirection of the stream. In the next days I need to investigate how to do that.

tealwren commented 5 years ago

I have this issue with one PDF as well. Is there a solution?

lukassteiner commented 4 years ago

Is this fixed in the newest release @NickNaso ?

choudhurym commented 4 years ago

I happen to have the same issue as @freewil. Did anyone able to find the root cause and how to resolve this issue?

choudhurym commented 4 years ago

I was able to work around to fix this issue. Hopefully, it will solve yours. Identify the pdf which throws the below error **** Error: Encountered 'obj' while expecting 'endobj'. Treating this as a missing 'endobj', output may be incorrect.

Then fix the pdf separately using the below command: gs -o gs-repaired---35.pdf -dPDFSETTINGS=/prepress -sDEVICE=pdfwrite 35.pdf