我试图在双工模式(DuplexLongSide)中打印2页,但是即使在实例化对话框之前设置打印机设置之后,在QPrintDialog.Accepted之后确认双工设置是正确的,打印仍然是单纯的。我已经确认,我的Windows 10主机上的驱动器支持双工打印,我使用的网络打印机也支持双面打印。根据音乐人8月18日的评论,我更新了下面的例行公事:
@logger.catch
def printDialog(self, bDummy):
if (True == td.__Debug__):
logger.info(f"TestResultsMainWindow: printDialog...bDummy:{bDummy}")
#end td.__Debug__
# Give the user a QPrintDialog to allow them to print the currently-displayed
# results from the active tab.
#printer = QPrinter(QPrinter.HighResolution)
printer = QPrinter()
printer.setDoubleSidedPrinting(True)
#printer.setDuplex(QPrinter.DuplexAuto)
printer.setDuplex(QPrinter.DuplexLongSide)
printer.setResolution(self.DefaultPrintDotsPerInch)
if (printer.doubleSidedPrinting()):
if (True == td.__Debug__):
logger.debug("QPrinter: doubleSidedPrinting printing!")
#end td.__Debug__
else:
if (True == td.__Debug__):
logger.debug("QPrinter: singleSidedPrinting printing!")
#end td.__Debug__
#end if (printer.doubleSidedPrinting())
if (True == td.__Debug__):
logger.debug(f"QPrinter mode: {printer.duplex()}")
#end td.__Debug__
# Instantiate the dialog with the current printer settings
dialog = QPrintDialog(printer, self.mainWindow)
if (dialog.exec_() == QPrintDialog.Accepted):
if (printer.doubleSidedPrinting()):
if (True == td.__Debug__):
logger.debug("Check again...QPrinter: doubleSidedPrinting printing!")
#end td.__Debug__
else:
if (True == td.__Debug__):
logger.debug("Check again...QPrinter: singleSidedPrinting printing!")
#end td.__Debug__
#end if (printer.doubleSidedPrinting())
if (True == td.__Debug__):
logger.debug(f"Check again...QPrinter mode: {printer.duplex()}")
#end td.__Debug__
if (True == td.__Debug__):
logger.debug(f"{self.name}: create pixmap of the currently-displayed results and send it to the printer")
#end td.__Debug__
# Open the PDF...
tmpPdfFileHandle = fitz.open(self.pdfFilename)
mat = fitz.Matrix(self.ImagePrintZoomX, self.ImagePrintZoomY) # zoom factor 1.25x in each direction
if (True == td.__Debug__):
logger.debug(f"printDialog: NumPages of {self.pdfFilename} is {len(tmpPdfFileHandle)}")
#end td.__Debug__
painter = QtGui.QPainter()
# We're beginning to print a new page on the printer...
#printer.newPage()
for pdfPage in tmpPdfFileHandle:
# Get the page itself
pagePixmap = pdfPage.get_pixmap(matrix=mat, alpha=False) # render the page to an image that can be printed
#pagePixmap = pdfPage.getPixmap(alpha=False) # render the page to an image that can be printed
pagePixmap.save("page-%i.png" % pdfPage.number)
# Now convert to PyQt5 QImage
image=QImage(pagePixmap.samples,
pagePixmap.width, pagePixmap.height,
pagePixmap.stride, # length of one image line in bytes
QImage.Format_RGB888)
# Convert the image to a PyQt5 QPixmap
qt5Pixmap = QPixmap.fromImage(image)
# Now print the page
#painter = QtGui.QPainter()
painter.begin(printer)
# Send the page to the printer...
painter.drawImage(10, 30, qt5Pixmap.toImage())
# We're done sending this page to the printer...
painter.end()
#end for pdfPage in tmpPdfFileHandle
else:
if (True == td.__Debug__):
logger.info(f"{self.name}: printDialog...canceled the print")
#end td.__Debug__
#end if (dialog.exec_() == QPrintDialog.Accepted)
#end printDialog()下面是我在“打印”对话框中选择“打印”按钮后看到的情况:
2022-08-24 09:36:57.940 DEBUG \ lruTestDriver:printDialog:633 - QPrinter: doubleSidedPrinting printing!2022-08-24 09:36:59.297 DEBUG \ lruTestDriver:printDialog:641 - QPrinter模式:2 2022-08-24 09:37:05.361调试lruTestDriver:printDialog:651 - Check again...QPrinter: doubleSidedPrinting打印!2022-08-24 09:37:07.087调试lruTestDriver:printDialog again...QPrinter模式:2 2022-08- 0824 09:37:08.429 \x{e76f}\x{e76f} TestResultsMainWindow:创建当前显示的结果的像素映射,并将其发送到打印机2022-08-24 :37:10.537 DEBUG lruTestDriver: printDialog: 669 -printDialog: NumPages of is 549_TestResults_2022-08-24_09-36-31.pdf为2。
发布于 2022-09-07 15:14:49
解决这个问题的方法是使用QPrinter.newPage()方法。这种方法本质上要么翻转打印机上的页面(如果打印是双工的话),要么将另一张纸输入打印机(如果打印是单纯的)。还请注意,QPainter.begin()和QPainter.end()方法在for循环之外,因为for循环中的所有内容都要打印出来。
@logger.catch
def printDialog(self, bDummy):
if (True == td.__Debug__):
logger.info(f"TestResultsMainWindow: printDialog...bDummy:{bDummy}")
#end td.__Debug__
# Give the user a QPrintDialog to allow them to print the currently-displayed
# results from the active tab.
printer = QPrinter()
printer.setDoubleSidedPrinting(True)
printer.setDuplex(QPrinter.DuplexLongSide)
printer.setResolution(self.DefaultPrintDotsPerInch)
if (printer.doubleSidedPrinting()):
if (True == td.__Debug__):
logger.debug("QPrinter: doubleSidedPrinting printing!")
#end td.__Debug__
else:
if (True == td.__Debug__):
logger.debug("QPrinter: singleSidedPrinting printing!")
#end td.__Debug__
#end if (printer.doubleSidedPrinting())
if (True == td.__Debug__):
logger.debug(f"QPrinter mode: {printer.duplex()}")
#end td.__Debug__
# Instantiate the dialog
dialog = QPrintDialog(printer, self.mainWindow)
if (dialog.exec_() == QPrintDialog.Accepted):
if (printer.doubleSidedPrinting()):
if (True == td.__Debug__):
logger.debug("Check again...QPrinter: doubleSidedPrinting printing!")
#end td.__Debug__
else:
if (True == td.__Debug__):
logger.debug("Check again...QPrinter: singleSidedPrinting printing!")
#end td.__Debug__
#end if (printer.doubleSidedPrinting())
if (True == td.__Debug__):
logger.debug(f"Check again...QPrinter mode: {printer.duplex()}")
#end td.__Debug__
if (True == td.__Debug__):
logger.debug(f"{self.name}: create pixmap of the currently-displayed results and send it to the printer")
#end td.__Debug__
# Open the PDF...
tmpPdfFileHandle = fitz.open(self.pdfFilename)
mat = fitz.Matrix(self.ImagePrintZoomX, self.ImagePrintZoomY) # zoom factor 1.25x in each direction
if (True == td.__Debug__):
logger.debug(f"printDialog: NumPages of {self.pdfFilename} is {len(tmpPdfFileHandle)}")
#end td.__Debug__
painter = QtGui.QPainter()
# We're beginning to print a new page on the printer...
painter.begin(printer)
page = 1
for pdfPage in tmpPdfFileHandle:
# Get the page itself
pagePixmap = pdfPage.get_pixmap(matrix=mat, alpha=False) # render the page to an image that can be printed
#pagePixmap = pdfPage.getPixmap(alpha=False) # render the page to an image that can be printed
pagePixmap.save("page-%i.png" % pdfPage.number)
# Now convert to PyQt5 QImage
image=QImage(pagePixmap.samples,
pagePixmap.width, pagePixmap.height,
pagePixmap.stride, # length of one image line in bytes
QImage.Format_RGB888)
# Convert the image to a PyQt5 QPixmap
qt5Pixmap = QPixmap.fromImage(image)
# Send the image to the printer...
painter.drawImage(10, 30, qt5Pixmap.toImage())
# We're done sending this page to the printer...
if (len(tmpPdfFileHandle) > page):
# Assume 1 side of a page per image. If duplex printing is set
# then newPage() will cause printing to the other side
printer.newPage()
page = page + 1
#end for pdfPage in tmpPdfFileHandle
painter.end()
else:
if (True == td.__Debug__):
logger.info(f"{self.name}: printDialog...canceled the print")
#end td.__Debug__
#end if (dialog.exec_() == QPrintDialog.Accepted)
#end printDialog()https://stackoverflow.com/questions/73408411
复制相似问题