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

weasyprint/tools removed #1422

Closed
titanofold opened this issue Aug 12, 2021 · 5 comments
Closed

weasyprint/tools removed #1422

titanofold opened this issue Aug 12, 2021 · 5 comments
Labels
documentation Problems or improvements needed on the documentation or on the website
Milestone

Comments

@titanofold
Copy link

With commit 431c11e, weasyprint/tools was removed from the repo. However, there wasn't any associated explanation, and it wasn't noted in the release notes. Additionally, the subject line appears to be entirely unrelated to dropping all tools.

Was weasyprint/tools removed in error?

@liZe liZe added the documentation Problems or improvements needed on the documentation or on the website label Aug 12, 2021
@liZe
Copy link
Member

liZe commented Aug 12, 2021

Hello!

You’re right, there’s nothing in the release notes about this, that’s an error, sorry! But removing the tools wasn’t an error: as WeasyPrint doesn’t render PNG files anymore, the two tools are now obsolete.

Did you use them?

@liZe liZe added this to the 53.1 milestone Aug 12, 2021
@glorg
Copy link

glorg commented Aug 13, 2021

Hello.
We used to connect to weasyprint over TCP socket with running python3 -m weasyprint.tools.navigator.
This was convenient as php application didn't require any permission to run (external) python tool while still could use weasyprint.
But with removal of 'tools' this is no longer possible as mentioned module is no longer available, see also https://bugs.gentoo.org/807769

@liZe
Copy link
Member

liZe commented Aug 13, 2021

Wow, that’s always interesting to see how tools written to "play" are used seriously with interesting use cases 😄. That was actually a good way to get PNG files out of WeasyPrint without writing a single line of Python.

Unfortunately, as WeasyPrint can’t generate PNG files anymore, that’s not possible now without writing extra code.

Would you be interested in a code sample showing how to do this?

@glorg
Copy link

glorg commented Aug 13, 2021

We were not rendering PNG files, we rendered PDF files, just connecting to weasyprint as a local network service.
Is there a way to restore that functionality (I mean, run Weasyprint with listening TCP port in order to render PDF with it)?
If there's another way to achieve this, could you please advise?
If you can share code sample to restore the functionality of being a network-bound tool, that would be appreciated!

@liZe
Copy link
Member

liZe commented Aug 13, 2021

We were not rendering PNG files, we rendered PDF files,

Sorry, I didn’t remember that "navigator" was also able to generate PDF files.

This code was really included to play, and even if it was useful we won’t maintain it seriously. But you can use this script instead:

from wsgiref.simple_server import make_server

from weasyprint import HTML
from weasyprint.urls import url_is_absolute


def app(environ, start_response):
    url, query_string = environ['PATH_INFO'], environ.get('QUERY_STRING')
    url = url.lstrip('/')
    if query_string:
        url += f'?{query_string}'
    if not url_is_absolute(url):
        url = f'http://{url}'
    body = HTML(url=url).write_pdf()
    filename = url.rstrip('/').rsplit('/', 1)[-1] or 'out'
    start_response('200 OK', [
        ('Content-Type', 'application/pdf'),
        ('Content-Length', str(len(body))),
        ('Content-Disposition', f'attachment; filename={filename}.pdf')
    ])
    return [body]


make_server('localhost', 5000, app).serve_forever()

Of course, this code is just a snippet that can be adapted to your needs, even if it is quite close to what "navigator" was. Here are the main changes: the URL doesn’t have the leading /pdf, and you can launch the script using python3 script.py.

Even if this code may work well for you, it’s not exactly ready for production. Just as "navigator", it uses http.server that is explicitly "not recommended for production" and "only implements basic security checks". And it’s also an easy target for a DoS attack. You’ve been warned 😉.

liZe added a commit that referenced this issue Aug 13, 2021
@liZe liZe closed this as completed Aug 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Problems or improvements needed on the documentation or on the website
Projects
None yet
Development

No branches or pull requests

3 participants