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

PDF Export with Embedded SVG #124

Closed
jdgwf opened this issue Apr 3, 2018 · 13 comments
Closed

PDF Export with Embedded SVG #124

jdgwf opened this issue Apr 3, 2018 · 13 comments

Comments

@jdgwf
Copy link
Contributor

jdgwf commented Apr 3, 2018

I've created the following test SVG (I've simplified this ugly example immensely after discovering this issue):

SVG Preview Screenshot:
screenshot from 2018-04-03 10-09-15

PDF Render:
screenshot from 2018-04-03 10-09-37

Will paste my (again, ugly) test SVG code below.

What appears to be happening:
svglib is trying to use one of the embedded SVG dimensions as the parent document dimension.

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Here's the svg as a txt file:
drawing.svg.txt

@claudep
Copy link
Collaborator

claudep commented Apr 3, 2018

Yes, it's known that embedding SVGs is currently buggy (this was revealed by the Slovenia.svg flag output in the current test suite).

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

I apologize for the duplicate ticket then. Any suggestions where to look to help squash this bug? I'll be mulling over the code to help patch this :)

@claudep
Copy link
Collaborator

claudep commented Apr 3, 2018

No need to apologize, there was no properly-named ticket about this until now.
The problem is probably around the renderSVG method, which assumes it is always the top-most node of the file.
https://github.com/deeplook/svglib/blob/master/svglib/svglib.py#L526

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Ok I have a working Alpha quality patch - testing now
screenshot from 2018-04-03 10-46-45

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Looks like I may have to open a pull request once I'm more sure ;)

@claudep
Copy link
Collaborator

claudep commented Apr 3, 2018

Show us the code, dude :-)

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Scaling needs to be fixed...

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Here are the two functions I've applied code to class SvgRenderer:

def __init__(self, path=None,color_converter=None):
    self.attrConverter = Svg2RlgAttributeConverter(color_converter=color_converter)
    self.shape_converter = Svg2RlgShapeConverter(path,self.attrConverter)
    self.handled_shapes = self.shape_converter.get_handled_shapes()
    self.definitions = {}
    self.waiting_use_nodes = defaultdict(list)
    self.box = Box(x=0, y=0, width=0, height=0)
    self.parent_node = True

def renderSvg(self, node):
    getAttr = node.getAttribute
    width, height = map(getAttr, ("width", "height",))
    width, height = map(self.attrConverter.convertLength, (width, height))

    x, y = map(getAttr, ("x", "y",))
    x, y = map(self.attrConverter.convertLength, (x, y))

    viewBox = getAttr("viewBox")
    print( width, height, x, y, self.parent_node, viewBox )

    if viewBox:
        viewBox = self.attrConverter.convertLengthList(viewBox)
        if self.parent_node:
            self.box = Box(*viewBox)
    else:
        if self.parent_node:
            self.box = Box(0, 0, width, height)
    self.parent_node = False

    group = Group()
    for child in node.getchildren():
        self.renderNode(child, group)
    transform = ""
    if x or y:
        transform += " translate(%s, %s)" % (x or '0', y or '0')
    if transform:
        self.shape_converter.applyTransformOnGroup(transform, group)

    return group

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

translate works, but I have a scale issue to work through. Still learning the class/object.

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Still hacky. Here's the code that's working on my fairly complex SVG:

def renderSvg(self, node):
    getAttr = node.getAttribute
    width, height = map(getAttr, ("width", "height",))
    width, height = map(self.attrConverter.convertLength, (width, height))

    x, y = map(getAttr, ("x", "y",))
    x, y = map(self.attrConverter.convertLength, (x, y))

    viewBox = getAttr("viewBox")

    childBox = None



    if viewBox:
        viewBox = self.attrConverter.convertLengthList(viewBox)
        if self.parent_node:
            self.box = Box(*viewBox)
        else:
            childBox = Box(*viewBox)
    else:
        if self.parent_node:
            self.box = Box(0, 0, width, height)
    self.parent_node = False

    group = Group()

    for child in node.getchildren():
        self.renderNode(child, group)
    transform = ""



    x_scale = 1
    y_scale = 1
    if childBox:
        print( "childBox", childBox )
        if childBox.height != height:
            y_scale = height /  childBox.height
        if childBox.width != width:
            x_scale = width /  childBox.width

    print( width, height, x, y, self.parent_node, viewBox, x_scale, y_scale )
    if x or y:
        transform += " translate(%s, %s)" % (x or '0', y or '0')

    if transform:
        self.shape_converter.applyTransformOnGroup(transform, group)

    group.scale(x_scale, y_scale )
    return group

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

Ignore the debug print(), of course ;)

@jdgwf
Copy link
Contributor Author

jdgwf commented Apr 3, 2018

screenshot from 2018-04-03 11-22-50

claudep added a commit to claudep/svglib that referenced this issue Apr 4, 2018
Thanks Jeffrey D. Gordon for the initial patch.
claudep added a commit to claudep/svglib that referenced this issue Apr 4, 2018
…/width

Thanks Jeffrey D. Gordon for the initial patch.
@claudep claudep closed this as completed Apr 4, 2018
claudep added a commit that referenced this issue Apr 9, 2018
Thanks Jeffrey D. Gordon for the initial patch.
claudep added a commit that referenced this issue Apr 9, 2018
Thanks Jeffrey D. Gordon for the initial patch.
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

No branches or pull requests

2 participants