svg2rlg converting svg to png only part of the image with percentage size

Question:

My svg image:
https://pastebin.com/raw/EeptY1C8

My code:

from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM

drawing = svg2rlg("qr_code.svg")
renderPM.drawToFile(drawing, "temp.png", fmt="PNG")
    
from tkinter import *

tk = Tk() 
from PIL import Image, ImageTk

img = Image.open('temp.png')
pimg = ImageTk.PhotoImage(img)
size = img.size

frame = Canvas(tk, width=size[0], height=size[1])
frame.pack()
frame.create_image(0,0,anchor='nw',image=pimg)
tk.mainloop()

And I get test.png image:
enter image description here

And there is an error code:

Message: 'Unable to resolve percentage unit without knowing the node name'
Arguments: ()

How to resolve percentage units?

Asked By: XuMuK

||

Answers:

So, the svg2rlg, cannot handle the svg files with percentage units.

I have managed to render an svg file using PyQt5, without creating any png files:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWebEngineWidgets import QWebEngineView


class DisplaySVG(QtWidgets.QWidget):
    "A simple SVG display."
    def __init__(self, url=None, parent=None):
        super().__init__(parent)
        #square size for QR code
        self.resize(500,500)
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.webview = QWebEngineView(self)
        self.verticalLayout.addWidget(self.webview)

        self.setWindowTitle("Display SVG")
        act = QtWidgets.QAction("Close", self)
        act.setShortcuts([QtGui.QKeySequence(QtCore.Qt.Key_Escape)])
        act.triggered.connect(self.close)
        self.addAction(act)
        #opening and reading the svg file
        with open('qr_code.svg', 'r') as f:
            svg = f.read()

        self.webview.setHtml(svg)

qt_app = QtWidgets.QApplication(sys.argv)
disp = DisplaySVG()
disp.show()
qt_app.exec_()

And to convert svg to png I found solution:

import aspose.words as aw

# SVG file's path
fileName = "qr_code.svg"

# create a document
doc = aw.Document()

# create a document builder and initialize it with document object
builder = aw.DocumentBuilder(doc)

# insert SVG image to document
shape = builder.insert_image(fileName)

# OPTIONAL
# Calculate the maximum width and height and update page settings 
# to crop the document to fit the size of the pictures.
pageSetup = builder.page_setup
pageSetup.page_width = shape.width
pageSetup.page_height = shape.height
pageSetup.top_margin = 0
pageSetup.left_margin = 0
pageSetup.bottom_margin = 0
pageSetup.right_margin = 0

# save as PNG
doc.save("qr_code.png") 
Answered By: XuMuK
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.