#!env python
import sys
from PyQt5.
QtPrintSupport import
QPrinterfrom PyQt5.
QtGui import
QFont
font
= QFont('Times',
24)widget
= QLabel('Lorem ipsum dolor sit amet, consectetur adipiscing elit')widget.setFont(font)
widget.resize(256, 256)
widget.setWordWrap(True)
# Print options
printer.
setOrientation(QPrinter.
Portrait)printer.
setPageMargins(15,
15,
15,
15,
QPrinter.
Millimeter)# Setting the margins and then calling setFullPage() will discard the margins.
#printer.setFullPage(True)
printer.
setOutputFormat(QPrinter.
PdfFormat)printer.setOutputFileName("testfile.pdf")
# Render/Paint it
painter.begin(printer)
# Establish scaling transform
scaleX = printer.pageRect().width() / widget.rect().width()
scaleY = printer.pageRect().height() / widget.rect().height()
useScale = min(scaleX, scaleY)
painter.scale(useScale, useScale)
widget.render(painter)
painter.end()
#!env python
import sys
from PyQt5.QtGui import QPainter
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import (QLabel, QApplication)
app = QApplication(sys.argv)
font = QFont('Times', 24)
widget = QLabel('Lorem ipsum dolor sit amet, consectetur adipiscing elit')
widget.setFont(font)
widget.resize(256, 256)
widget.setWordWrap(True)
# Print options
printer = QPrinter(QPrinter.HighResolution)
printer.setOrientation(QPrinter.Portrait)
printer.setPaperSize(QPrinter.A4)
printer.setPageSize(QPrinter.A4)
printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter)
# Setting the margins and then calling setFullPage() will discard the margins.
#printer.setFullPage(True)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("testfile.pdf")
# Render/Paint it
painter = QPainter()
painter.begin(printer)
# Establish scaling transform
scaleX = printer.pageRect().width() / widget.rect().width()
scaleY = printer.pageRect().height() / widget.rect().height()
useScale = min(scaleX, scaleY)
painter.scale(useScale, useScale)
widget.render(painter)
painter.end()
To copy to clipboard, switch view to plain text mode
Bookmarks