stumpylog / gotenberg-client

A Python client for interfacing with the Gotenberg API
Mozilla Public License 2.0
14 stars 3 forks source link

Pass HTML variable to convert? #29

Closed peterfarrell closed 1 month ago

peterfarrell commented 1 month ago

We generate most of HTML document on the fly using a Django/Jinja template and have the HTML in a variable. Simple example using the Requests library.


pdf_html = """
<!DOCTYPE html>
<html>
<body>
  <p>This is a test string.</p>
</body>
</html>
"""

req = requests.post(
    f"{GOTTENBERG_URL}/forms/chromium/convert/html",
    files={"file": ("index.html", pdf_html)},
)

I looked but it appears there is not a way to via the client here to pass HTML blob as a variable. Is there a way to do that?

stumpylog commented 1 month ago

So the pdf_html is just a string in this case?

peterfarrell commented 1 month ago

@stumpylog correct. I updated the original body of the issue with a more fleshed out example.

peterfarrell commented 1 month ago

For that stumble upon this later, when using the Python Requests library and post() -- the files argument allows you to "send strings to be received as files." This avoids having to write a temp file to disk if you already have something as a variable in memory.

Ref: https://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

stumpylog commented 1 month ago

Ok, there's nothing implemented currently, though it shouldn't be difficult to add. I'm a little short on time myself until maybe next week, if you'd like to take a crack at it.

I could see accepting some additional items, such as StringIO or BinaryIO, and just wrapping the string up with that and using much of the existing code

stumpylog commented 1 month ago

Eh, I'm interested enough. This shouldn't take long

stumpylog commented 1 month ago

If you could give it a try and some feedback, #30

peterfarrell commented 1 month ago

@stumpylog I can give it a whirl next Tuesday.