GithubHelp home page GithubHelp logo

Document the Python API. about cairosvg HOT 7 CLOSED

kozea avatar kozea commented on May 19, 2024
Document the Python API.

from cairosvg.

Comments (7)

mbr avatar mbr commented on May 19, 2024

+1

from cairosvg.

SimonSapin avatar SimonSapin commented on May 19, 2024

Until we write proper docs:

The cairosvg module has svg2pdf, svg2ps, svg2svg (!) and svg2png functions. Each function expects one of these named arguments for the input: bytestring, url (which I think also accepts filenames) or file_obj. If you provide a write_to argument (which can be either a filename or a file object), the output is written there. Otherwise, the function returns a byte string.

This is basically it for the public API. Everything else is implementation details and could change in future versions.

from cairosvg.

waldyrious avatar waldyrious commented on May 19, 2024

Maybe I'm doing something wrong, but passing a filename to svg2ps didn't work. Instead, I had to do:

cairosvg.svg2ps(open("/path/to/input.svg", "rb").read(), write_to="/tmp/output.ps")

If this is correct, I'd be happy to submit a PR fixing the documentation, so please confirm. Otherwise, let me know why

cairosvg.svg2ps("/path/to/input.svg", write_to="/tmp/output.ps")

didn't work -- I got the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/cairosvg/__init__.py", line 43, in <lambda>
    surface_type.convert(*args, **kwargs))(_surface_type)
  File "/usr/local/lib/python3.5/site-packages/cairosvg/surface/__init__.py", line 103, in convert
    tree = Tree(**kwargs)
  File "/usr/local/lib/python3.5/site-packages/cairosvg/parser.py", line 270, in __init__
    tree = ElementTree.fromstring(bytestring)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/etree/ElementTree.py", line 1320, in XML
    parser.feed(text)
  File "<string>", line None
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
not well-formed (invalid token): line 1, column 0

At the very least, the error could be more informative -- again, happy to submit a PR if you let me know where / what to change.

from cairosvg.

waldyrious avatar waldyrious commented on May 19, 2024

By the way, I also tried navigating to the folder where the input file is and running the script there passing only the filename, without the path, and also adding "file://" to the path (using both the full path and the filename only). None of this worked, so it seems to me a filename isn't actually accepted as input.

from cairosvg.

liZe avatar liZe commented on May 19, 2024

As described in the documentation:

The cairosvg module offers 4 functions:

svg2pdf,
svg2png,
svg2ps, and
svg2svg.

These functions expect one of these parameters:

bytestring, a byte string containing SVG, or
url, an URL or a filename, or
file_obj, a file-like object.

You can use:

  • cairosvg.svg2ps(file_obj=open("/path/to/input.svg", "rb"), write_to="/tmp/output.ps"), or
  • cairosvg.svg2ps(url="/path/to/input.svg", write_to="/tmp/output.ps")

from cairosvg.

SimonSapin avatar SimonSapin commented on May 19, 2024

file_obj without .read(), right?

from cairosvg.

liZe avatar liZe commented on May 19, 2024

Of course, I've changed the comment.

from cairosvg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.