Printing an svg from withing PyQt5
-
Hi,
I'm working on a system that scans barcodes on invitations at events and prints out the badges for the attendees.
For my first version, I generated pdf's and printed those (the badge printer shows up as a regular printer). That worked under Linux, but under Windows, libcairo wasn't avaiable. I worked my way around using poppler that but it wasn't pretty.Now I'd need more flexibility in generating content (I'd need boxes with/without ticks and such) and I'd like to avoid pdf altogether.
SVG seemed like a good format, using svgwrite. However, I can't seem to figure out how to send those to the printer.
printer = QPrinter(QPrinter.HighResolution) printer.setOrientation(QPrinter.Landscape) printer.setPageSizeMM(QSizeF(54.19, 85.2))
Sets up the printer correctly (it worked for the pdf).
Then I would do:painter = QPainter() painter.begin(printer)
To start "writing" to the printer and end with
painter.end()
In between that, I'm lost and I can't seem to find how to do it (so 2 svg files for front/back and being deployable on Linux and Windows).
Does anyone have any pointers?
-
In case anyone comes looking with the same question, I managed to get it working using two pre-created svg files (
test1.svg
andtest2.svg
):printer = QPrinter(QPrinter.HighResolution) printer.setOrientation(QPrinter.Landscape) printer.setPageSizeMM(QSizeF(54.19, 85.2)) painter = QPainter() painter.begin(printer) rend = QSvgRenderer('test1.svg') rend.render(painter) printer.newPage() rend = QSvgRenderer('test2.svg') rend.render(painter) painter.end()
Keep in mind that this does not show the print dialog box but directly prints to the default printer with the default settings.