Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream ends before emitting error #132

Open
AmwayEryk opened this issue Sep 24, 2020 · 1 comment
Open

Stream ends before emitting error #132

AmwayEryk opened this issue Sep 24, 2020 · 1 comment

Comments

@AmwayEryk
Copy link

We are working on developing a nice error handling in our app and after some investigation, we discovered a problem with the order of the events emitted by the returned stream from wkhtmltopdf library.

As we piped that stream into some Writeable and saved as a PDF file even though we got no error the PDF file was not valid. We investigated more and discovered that the error message from the wkhtmltopdf command line is being sent as the result in the stream. The reason for that is that the stream we receive from wkhtmltopdf function is basically the child.stdout which sends the end event before the child sends exit and then error event (if it exited with error code).

Below you can find a small test code that should show you the order of the events passed by the stream:

const wkhtml2pdf = require('wkhtmltopdf');

const stream = wkhtml2pdf('<html></html>', { a: 2 });

stream.on('data', (data) => {
  console.log('------------------------------ DATA EVENT --------------------------------');
  console.log(data.toString());
  console.log('------------------------------ DATA EVENT END --------------------------------');
});

stream.on('close', (data) => {
  console.log('------------------------------ CLOSE EVENT --------------------------------');
  console.log(data);
  console.log('------------------------------ CLOSE EVENT END --------------------------------');
});

stream.on('end', (data) => {
  console.log('------------------------------ END EVENT --------------------------------');
  console.log(data);
  console.log('------------------------------ END EVENT END --------------------------------');
});

stream.on('error', (data) => {
  console.log('------------------------------ ERROR EVENT --------------------------------');
  console.log(data);
  console.log('------------------------------ ERROR EVENT END --------------------------------');
});

After running it in the terminal you will get this (you can see that the end event fires before the error event):

------------------------------ DATA EVENT --------------------------------
















------------------------------ DATA EVENT END --------------------------------
------------------------------ END EVENT --------------------------------
undefined
------------------------------ END EVENT END --------------------------------
WKHTML2PDF exit -----------------------------------------
1
------------------------------ ERROR EVENT --------------------------------
Error: Unknown
switch -a

Name:
  wkhtmltopdf 0.12.6 (with patched qt)

Synopsis:
  wkhtmltopdf [GLOBAL OPTION]... [OBJECT]... <output file>

Document objects:
  wkhtmltopdf is able to put several objects into the output file, an object is
  either a single webpage, a cover webpage or a table of contents.  The objects
  are put into the output document in the order they are specified o
n the
  command line, options can be specified on a per object basis
or in the global
  options area. Options from the Global Options section can only be placed in
  the global options area.

  A page objects puts the content of
a single webpage into the output document.

  (page)? <input url/file name> [PAGE OPTION]...

Options for the page object can be placed in the global opt
ions and the page
  options areas. The applicable
options can be found in the Page Options and
  Headers And Footer Options sections.


  A cover objects puts the content of a single webpage
into the output document,
  the page does not appear in t
he table of contents, and does not have headers
  and footers.

  cover <input url/file name> [PAGE OPTION]...
  All options that can be specified for a page object can also be specified for
  a cover.

  A table of contents object inserts a table of contents into the output
  document.

  toc [TOC OPTION]...
  All options that can be specified for a page object can also be specified for
  a toc, further more the options from the TOC Options section can also be
  applied. The table of contents is generated via XSLT which means that it can

be styled to look however you want it to look. To get an idea o
f how to do
  this you can dump the default xslt docum
ent by supplying the
  --dump-default-toc-xsl, and the outline
it works on by supplying
  --dump-outline, see the Ou
tline Options section.

Description:
  Converts
one or more HTML pages into a PDF
document, using wkhtmltopdf patched
  qt.


Global Options:

--collate                       Collate when printing multiple cop
ies                                      (default)
      --no-collate                    Do not collate when printing multiple
                           copies
      --copies <number>
         Number of copies to print into the pdf
                            file (default 1)

  -H, --extended-help                 Display more extensive help,
 detailing                                      less
 common command switches
  -g, --grayscale
 PDF will be generated in grayscale
  -h, --hel
p                          Display help
      --l
icense                       Output license informati
on and exit
      --log-level <level>
 Set log level to: none, error, warn or
                              info (default none)

  -l, --lowquality                    Generates lower quality pdf/ps.
 Useful to                                      sh
rink the result document space
  -O, --orientation
 <orientation>     Set orientation to Landscape
 or Portrait                                      (default Portrait)
  -s
, --page-size <Size>              Set p
aper size to: A4, Letter, etc.
                        (default A4)
  -q, --quiet
                       Be less verbose, maint
ained for backwards
      compatibility; Same as using --log-level
                                      none
(default)
      --read-args-from-stdin          Read command line
 arguments from stdin
      --title <text>
             The title of the generated pdf file (The
                                  title of the
 first document is used if not
              specified)
  -V, --version
           Output version information and exit

Page Options:
      --print-media-type
  Use print media-type instead of screen
      --no-print-med
ia-type           Do not use print media-type instead
 of                                      screen
 (default)
Contact:
  If you
 experience bugs or want to request new
features please visit
  <https://wkhtmltopdf.org/support.html>



wkhtmltopdf exited with code 1
    at handleError (C:\Projects\docgen\app\node_modules\wkhtmltopdf\index.js:157:16)
    at ChildProcess.<anonymous> (C:\Projects\docgen\app\node_modules\wkhtmltopdf\index.js:127:7)
    at ChildProcess.emit (events.js:314:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
------------------------------ ERROR EVENT END --------------------------------
------------------------------ CLOSE EVENT --------------------------------
false
------------------------------ CLOSE EVENT END --------------------------------

We think it would be better to create a wrapper stream that would wait with emitting the end event until the child exits to get information about any errors from the command line.

@yurks
Copy link
Contributor

yurks commented Feb 27, 2021

take a look onto #133, more love to errors here)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants