Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to print to a printer PySide6
Forum Updated to NodeBB v4.3 + New Features

How to print to a printer PySide6

Scheduled Pinned Locked Moved Solved Qt for Python
7 Posts 4 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kumar80
    wrote on last edited by
    #1

    I am starting off with Pyside6 and trying to build a billing application which runs on ubuntu. How do format and print the bill any sample or suggestions will be helpful

    eyllanescE 1 Reply Last reply
    0
    • K kumar80

      I am starting off with Pyside6 and trying to build a billing application which runs on ubuntu. How do format and print the bill any sample or suggestions will be helpful

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      @kumar80 Demo:

      from PySide6.QtGui import (
          QPageSize,
          QPageLayout,
          QTextBlockFormat,
          QTextCursor,
          QTextDocument,
          QTextFormat,
      )
      from PySide6.QtWidgets import QApplication
      from PySide6.QtPrintSupport import QPrinter, QPrintPreviewDialog
      
      
      app = QApplication()
      
      dialog = QPrintPreviewDialog()
      
      
      def handle_paint_requested(printer):
          document = QTextDocument()
          cursor = QTextCursor(document)
          blockFormat = QTextBlockFormat()
          for i in range(1, 11):
              cursor.insertBlock(blockFormat)
              cursor.insertHtml(f"<h1>This is the {i} page</h1>")
              blockFormat.setPageBreakPolicy(QTextFormat.PageBreak_AlwaysBefore)
          document.print_(printer)
      
      
      dialog.paintRequested.connect(handle_paint_requested)
      dialog.exec()
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      1 Reply Last reply
      2
      • K Offline
        K Offline
        kumar80
        wrote on last edited by
        #3

        @eyllanesc said in How to print to a printer PySide6:

        filename = "test.pdf"

        Do we need to create a pdf file before printing it? For my use case will be filling out a form with text boxes and then click on print. In such case is there a way to print without have having an intermediate pdf?

        eyllanescE 1 Reply Last reply
        0
        • K kumar80

          @eyllanesc said in How to print to a printer PySide6:

          filename = "test.pdf"

          Do we need to create a pdf file before printing it? For my use case will be filling out a form with text boxes and then click on print. In such case is there a way to print without have having an intermediate pdf?

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on last edited by eyllanesc
          #4

          @kumar80 No, it is not necessary, I put 2 codes together but do not eliminate the unnecessary elements. I recommend you test my code by removing that line and you will see that it works. See my updated code.

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            karlowic
            wrote on last edited by
            #5

            Sorry to revive this topic but I can't deal with it. For me, the text on the first page is not displayed in the dialog box, but it prints correctly. Why? How to improve it?

            jsulmJ 1 Reply Last reply
            0
            • K karlowic

              Sorry to revive this topic but I can't deal with it. For me, the text on the first page is not displayed in the dialog box, but it prints correctly. Why? How to improve it?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @karlowic Your problem is not related to this thread - would be better if you would open your own thread and provide your code.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • K Offline
                K Offline
                karlowic
                wrote on last edited by
                #7

                My post is about the code written by eylanesc in this thread.
                This is what Print Preview looks like:
                image.png

                This is what the PDF document looks like after printing:

                acbac909-1a1c-405f-9147-720657e90756-image.png

                Print Preview is missing text on the first page but prints correctly. I don't know why this happens, can it be improved?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved