Modify

Opened 5 months ago

Closed 4 months ago

Last modified 4 months ago

#14264 closed defect (fixed)

Syntax Error when deployed to trac 1.6

Reported by: Tony Albers Owned by: Cinc-th
Priority: normal Component: TracWikiPrintPlugin
Severity: normal Keywords:
Cc: Trac Release: 1.6

Description

Hi,

Running Trac 1.6 and TracWikiPrint 3.0.dev0, I get no extra wikiprint admin menu, and in trac.log:

2023-12-19 08:06:18,827 Trac[env] INFO: -------------------------------- environment startup [Trac 1.6] --------------------------------
2023-12-19 08:06:19,281 Trac[loader] ERROR: Skipping "wikiprint.formats = wikiprint.formats": ModuleNotFoundError: No module named 'api'
2023-12-19 08:06:19,283 Trac[loader] ERROR: Skipping "wikiprint.web_ui = wikiprint.web_ui": ModuleNotFoundError: No module named 'defaults'
2023-12-19 08:06:19,287 Trac[loader] ERROR: Skipping "wikiprint.wikiprint = wikiprint.wikiprint": 
Traceback (most recent call last):
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/loader.py", line 80, in _load_eggs
    entry.load(require=True)
  File "/var/www/tracinst/lib/python3.11/site-packages/pkg_resources/__init__.py", line 2468, in load
    return self.resolve()
           ^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/pkg_resources/__init__.py", line 2474, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/wikiprint/wikiprint.py", line 150
    except Exception, e:
           ^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized (wikiprint.py, line 150)

Attachments (0)

Change History (10)

comment:1 Changed 5 months ago by Jun Omae

Please install from repository in trac-hacks.org instead of pypi. The trunk has been made compatible with Python 3.

pip install svn+https://trac-hacks.org/svn/tracwikiprintplugin/trunk/

comment:2 Changed 4 months ago by Tony Albers

Thanks, however that gives me an error about wkhtmltopdf binary missing:

Trac detected an internal error:
OSError: No wkhtmltopdf executable found: "b''"

But as far as I understand it, xhtml2pdf should be used, not wkhtmltopdf... Or did i misunderstand that?

And in the log:

2023-12-19 12:08:52,166 Trac[main] ERROR: [10.23.245.44] Internal Server Error: <RequestWithSession "GE
T '/wiki/TracGuide?format=pdfpage'">, referrer 'https://trac01.example.com/wiki/TracGuide'
Traceback (most recent call last):
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/configuration.py", line 35, in __init__
    with open(self.wkhtmltopdf) as f:
         ^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 609, in dispatch_request
    dispatcher.dispatch(req)
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 301, in dispatch
    raise e
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 247, in dispatch
    resp = chosen_handler.process_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/wiki/web_ui.py", line 184, in process_request
    Mimeview(self.env).send_converted(req, 'text/x-trac-wiki',
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/mimeview/api.py", line 1039, in send_converted
    content, output_type, ext = self.convert_content(req, in_type, content,
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/mimeview/api.py", line 712, in convert_content
    output = conversion.converter.convert_content(req, mimetype,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/wikiprint.py", line 244, in convert_content
    pdf_page = from_url(page_lst, False, options=pdfoptions, cover=cover_url)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/api.py", line 24, in from_url
    r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/pdfkit.py", line 45, in __init__
    self.configuration = (Configuration() if configuration is None
                          ^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/configuration.py", line 38, in __init__
    raise IOError('No wkhtmltopdf executable found: "%s"\n'
OSError: No wkhtmltopdf executable found: "b''"
OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

And I'd rather not install wkhtmltopdf since it pulls in a whole bunch of X-related stuff that shouldn't be on this server.

comment:3 in reply to:  2 Changed 4 months ago by Jun Omae

But as far as I understand it, xhtml2pdf should be used, not wkhtmltopdf... Or did i misunderstand that?

TracWikiPrintPlugin#Installation says: Install ​wkhtmltopdf.

comment:4 Changed 4 months ago by Tony Albers

Well, it does. But I'm trying to use xhtml2pdf

And I missed the fact that WikiPrintXhtml2pdf doesn't actually have a "component" in the ticket system here. And I didn't even notice that when I created the ticket, sorry.

It doesn't look like WikiPrintXhtml2pdf is in svn..

So, is it possible to use it?

comment:5 in reply to:  1 ; Changed 4 months ago by Ryan J Ollos

Replying to Jun Omae:

Please install from repository in trac-hacks.org instead of pypi.

I added you as a maintainer on PyPI. Feel free to push the latest. I'll do the same if I can find time.

comment:6 Changed 4 months ago by Tony Albers

Okay, so I managed to fetch wkhtmltopdf from the author's gihub releases page: https://github.com/wkhtmltopdf/packaging/releases

I've installed the .deb, and I now have the wkhtmltopdf binary in PATH, and I've checked that it's working correctly by trying to convert a simple html to pdf. Worked fine.

But when I try to get a pdf from a trac page, I get:

Trac detected an internal error:

OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: UnknownContentError

And i the trac log file:

2023-12-20 14:27:24,541 Trac[main] ERROR: [100.230.240.22] Internal Server Error: <RequestWithSession "GET 
'/wikiprint/WikiStart'">, referrer None
Traceback (most recent call last):
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 609, in dispatch_request
    dispatcher.dispatch(req)
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 301, in dispatch
    raise e
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 247, in dispatch
    resp = chosen_handler.process_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/wikiprint.py", line 335, in process_request
    html_page = self.create_html_page(req, page.text)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/wikiprint.py", line 393, in create_html_page
    return self.page_tmpl.format(wiki=wiki_html, style=self._get_styles(stylepage))
                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/wikiprint.py", line 376, in _get_styles
    trac_css = get_trac_css(self.env, 'trac.css')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/util.py", line 53, in get_trac_css
    return to_unicode(f.read(-1))
                      ^^^^^^^^^^
  File "/usr/lib/python3.11/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 15517: ordinal not in range(128)
2023-12-20 14:27:24,984 Trac[main] ERROR: [100.230.245.48] Internal Server Error: <RequestWithSession "GET '/wiki/WikiStart?format=pdfpage'">, referrer 'https://trac01.example.com/wiki'
Traceback (most recent call last):
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 609, in dispatch_request
    dispatcher.dispatch(req)
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 301, in dispatch
    raise e
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/web/main.py", line 247, in dispatch
    resp = chosen_handler.process_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/wiki/web_ui.py", line 184, in process_request
    Mimeview(self.env).send_converted(req, 'text/x-trac-wiki',
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/mimeview/api.py", line 1039, in send_converted
    content, output_type, ext = self.convert_content(req, in_type, content,
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/trac/mimeview/api.py", line 712, in convert_content
    output = conversion.converter.convert_content(req, mimetype,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/wikiprint.py", line 244, in convert_content
    pdf_page = from_url(page_lst, False, options=pdfoptions, cover=cover_url)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/api.py", line 27, in from_url
    return r.to_pdf(output_path)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/pdfkit.py", line 201, in to_pdf
    self.handle_error(exit_code, stderr)
  File "/var/www/tracinst/lib/python3.11/site-packages/pdfkit/pdfkit.py", line 155, in handle_error
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: UnknownContentError
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: UnknownContentError

As far as I can tell, it goes wrong here:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 15517: ordinal not in range(128)

But I'm unable to figure out if this is an error in Trac or in wkhtmltopdf..

/tony

comment:7 in reply to:  6 Changed 4 months ago by Jun Omae

Replying to Tony Albers:

And i the trac log file:

2023-12-20 14:27:24,541 Trac[main] ERROR: [100.230.240.22] Internal Server Error: <RequestWithSession "GET 
'/wikiprint/WikiStart'">, referrer None
Traceback (most recent call last):
  ...
  File "/var/www/tracinst/lib/python3.11/site-packages/tracpdf/util.py", line 53, in get_trac_css
    return to_unicode(f.read(-1))
                      ^^^^^^^^^^
  File "/usr/lib/python3.11/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 15517: ordinal not in range(128)
...

Unable to reproduce it on my environment. However, could you please try the following patch?

  • tracpdf/util.py

     
    2323# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    2424# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    2525# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26import io
    2627import os
    2728from imp import find_module
    28 from trac.util.text import to_unicode
    2929
    3030try:
    3131    FileNotFoundError
     
    4848
    4949    # TODO: check for environment CSS files first to account for local
    5050    #       styles.
     51    path = os.path.join(mod[1], 'htdocs', 'css', css_file)
    5152    try:
    52         with open(os.path.join(mod[1], 'htdocs', 'css', css_file), 'r') as f:
    53             return to_unicode(f.read(-1))
     53        with io.open(path, encoding='utf-8', errors='replace') as f:
     54            return f.read()
    5455    except FileNotFoundError:
    55         return to_unicode(u'')
     56        return u''
    5657
    5758
    5859def writeResponse(req, data, httperror=200, content_type='text/plain; charset=utf-8'):

comment:8 Changed 4 months ago by Tony Albers

Thanks Jun,

That seems to have solved that issue.

However, I have run into another issue with it, the images on the pages don't show up in the PDFs or in the printable HTML. I've written to trac-users about that since it might no be related to this.

Thanks,

/tony

comment:9 Changed 4 months ago by Jun Omae

Resolution: fixed
Status: newclosed

In 18611:

TracWikiPrintPlugin 4.0.0dev: fix UnicodeDecodeError raised while reading css/trac.css with Python 3 (closes #14264)

comment:10 in reply to:  5 Changed 4 months ago by Jun Omae

Replying to Ryan J Ollos:

Replying to Jun Omae:

Please install from repository in trac-hacks.org instead of pypi.

I added you as a maintainer on PyPI. Feel free to push the latest. I'll do the same if I can find time.

Thanks, Ryan. I just pushed the latest at https://pypi.org/project/TracWikiPrint/4.0.0.dev0/.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Cinc-th.
The resolution will be deleted. Next status will be 'reopened'.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.