Skip to content

SVGObject.from_file: use the encoding the file declares - #2157

Open
RavSinghChandan wants to merge 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-from-file-encoding
Open

RavSinghChandan wants to merge 1 commit into
py-pdf:masterfrom
RavSinghChandan:svg-from-file-encoding

Conversation

@RavSinghChandan

Copy link
Copy Markdown

SVGObject.from_file() always decodes the file as UTF-8, so a file that declares another encoding fails:

SVGObject.from_file("car.svg")  # <?xml version="1.0" encoding="iso-8859-1"?>
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc ...

pdf.image("car.svg") reads the same file fine, because it passes the bytes to the XML parser, which uses the declaration. car.svg and python221imap.svg from svglib's test samples both declare iso-8859-1.

encoding now defaults to None, which reads the bytes and lets the parser use the declared encoding (UTF-8 if there is none, as before). An encoding given explicitly is used as before.

Added test_svg_from_file_encoding (declared Latin-1, UTF-8 without declaration, explicit encoding); the first case fails on master. The full test suite passes except test_image_with_explicit_dimensions, which also fails on master in my environment; black, pylint, mypy and pyright pass

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant