jarrekk / imgkit

🌁 Wkhtmltoimage python wrapper to convert HTML to image
MIT License
803 stars 73 forks source link

UnicodeDecodeError on using "quiet" option #71

Open MLpranav opened 3 years ago

MLpranav commented 3 years ago

This works fine.

img = imgkit.from_string(data.to_html(), False, options = {'width': 772, 'disable-smart-width': '', 'zoom' : 1.5})

This is the output.

Loading page (1/2)
Rendering (2/2)
Done

But if I add the "quiet" option to suppress output, it does not work.

img = imgkit.from_string(data.to_html(), False, options = {'width': 772, 'disable-smart-width': '', 'zoom' : 1.5, 'quiet': ''})

This is the error:

Traceback (most recent call last):
  File "D:\Libraries\Desktop\oi.py", line 139, in <module>
    img = imgkit.from_string(data.to_html(), False, options = {'width': 772, 'disable-smart-width': '', 'zoom' : 1.5, 'quiet': ''})
  File "C:\Users\prana\AppData\Roaming\Python\Python37\site-packages\imgkit\api.py", line 108, in from_string
    return rtn.to_img(output_path)
  File "C:\Users\prana\AppData\Roaming\Python\Python37\site-packages\imgkit\imgkit.py", line 242, in to_img
    stderr = stderr.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

This is data.to_html(), if it helps.

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Time</th>
      <th>CE</th>
      <th>PE</th>
      <th>PE-CE</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>16:33</td>
      <td>955450</td>
      <td>-12425</td>
      <td>-958314</td>
      <td>34129</td>
    </tr>
  </tbody>
</table>

If I remove False argument and save the image as a png file instead, it works.

yosmelvin commented 3 years ago

Hello guys any update about this bug? , actually I am facing same issue its because we try not to define output path and error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte come out and it happen on ubuntu OS which using xvfb for headless server

MLpranav commented 3 years ago

No update or fix yet I belive.

I suggest setting a dummy file or /dev/null as the output path as a temporary solution to the problem.

arttuperala commented 3 years ago

I also encountered this today and quickly looking at it, it looks like the issue is caused by a fallback in the error handling logic.

https://github.com/jarrekk/imgkit/blob/1124d2ee120a2227f333e6225724a54be409d566/src/imgkit/imgkit.py#L241-L242

When the quiet option is used, bool(stderr) seems to evaluate to False, so it sets stderr to be equivalent to stdout, which is where the output file goes to. Then it tries to decode the file stream and fails.

https://github.com/jarrekk/imgkit/blob/424f46c657deac0d5fddfcacb2cff19e84baf193/src/imgkit/imgkit.py#L245-L249

I'm guessing previously it used to work since there was a try ... except to catch UnicodeDecodeError, but it was removed in commit eaa8cc304e78e84c7a07fdde0c38b10f776818d6.

ltfavas commented 2 years ago

Also suffering from this issue