deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
311 stars 80 forks source link

SVGLIB Produces Error - 'lxml.etree._Element' object has no attribute 'ancestors' #335

Closed louis-gallo-cigna closed 2 years ago

louis-gallo-cigna commented 2 years ago

Hello,

I have been using svglib for years. I am creating a new program whereas SVG files are inserted into a PDF file.

The SVG files are created in R package SVGLITE v2.0.0.

I am getting this error.

'lxml.etree._Element' object has no attribute 'ancestors'

Python simple code to include an SVG file in a PDF

`from lxml import etree from reportlab.graphics import renderPDF from reportlab.pdfgen import canvas from reportlab.lib.units import inch from reportlab.platypus import Image from reportlab.lib import utils import svglib import string from svglib.svglib import svg2rlg

def add_image(image_path, intInputRowNumber, intInputColumnNumber):
try: print("DEBUG0001") drawing = svg2rlg(image_path) # <======================= BOMBS OUT HERE print("DEBUG0002") drawing.transform = (0.25,0,0,0.25,0,0) #

Header Row

    #
    intHeaderIndex = 2850
    print("DEBUG0003")
    my_canvas.drawString(350, intHeaderIndex, 'System Performance Report')
    print("DEBUG0004")
    #
    #   Coordinates
    #
    if (intInputColumnNumber == 1):
        intCurrentColumnNumber = 0
    if (intInputColumnNumber == 2):
        intCurrentColumnNumber = 200
    if (intInputColumnNumber == 3):
        intCurrentColumnNumber = 400
    if (intInputColumnNumber == 4):
        intCurrentColumnNumber = 600
    #
    intCurrentRowNumber = ((intHeaderIndex - 150) - ((intInputRowNumber - 1) * 150))
    #
    #   Individual Rows
    #
    renderPDF.draw(drawing, my_canvas, intCurrentColumnNumber, intCurrentRowNumber)
except Exception as e :
    print("Render Error" + str(e))
    return True
print("Normal Render")
return False

my_canvas = canvas.Canvas("lou.pdf") my_canvas.setPageSize((11.5inch, 40inch)) add_image("foo1.svg", 1, 1) add_image("foo2.svg", 1, 2) add_image("foo3.svg", 1, 3) add_image("No-Image-Available.svg", 1, 4) my_canvas.save()`

When I run this I get the following:

DEBUG0001 Render Error'lxml.etree._Element' object has no attribute 'ancestors' DEBUG0001 Render Error'lxml.etree._Element' object has no attribute 'ancestors' DEBUG0001 Render Error'lxml.etree._Element' object has no attribute 'ancestors' DEBUG0001 DEBUG0002 DEBUG0003 DEBUG0004 Normal Render

foo1-3.svg are graphs. They bomb out. ""No-Image-Available.svg" is a simple file. It inserts fine.

I'm gonna guess and say that SVGLITE is creating SVG files that svglib does not like.

Any ideas.

Thanks,

Lou

louis-gallo-cigna commented 2 years ago

Moved from SVGLITE in R to CairoSVG in R. That fixed it. The two packages seem to create the SVG files differently.