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 can I release a pdf document for use again after it has been used with QPdfView in PyQt?
Forum Updated to NodeBB v4.3 + New Features

How can I release a pdf document for use again after it has been used with QPdfView in PyQt?

Scheduled Pinned Locked Moved Solved Qt for Python
qt for pythonpyside
6 Posts 2 Posters 761 Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Blumi
    wrote on last edited by
    #1

    Hello everyone,

    I just can't figure out how to release a PDF file that has been opened with QPdfView for use by other processors or programs. I just want to give a preview of a PDF in my PyQt app. The PDF should be displayed in the app, the user makes some entries and after confirmation the PDF should be closed and archived. To do this, I want to use shutil to delete the PDF file in the current directory and move it to a new directory. However, I always get the error message that the file is already being used by other processes, even if I call the close() or destroy() method from QPdfView.

    Am I missing something? What do I have to do, to release a PDF-file from usage by QPdfView and use it with another process?

    1 Reply Last reply
    0
    • B Blumi

      Edit:

      import sys
      from PySide6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication
      from PySide6.QtPdf import QPdfDocument
      from PySide6.QtPdfWidgets import QPdfView
      import os
      
      
      class MainWindow(QWidget):
          def __init__(self):
              super().__init__()
              self.resize(1000, 800)
              self.setLayout(QVBoxLayout())
      
              self.pdf = QPdfView()
              self.document = QPdfDocument()
              self.document.load(r"C:\Users\Basti-PC\Documents\test.pdf")
              self.pdf.setDocument(self.document)
              self.button = QPushButton("Click me")
      
              self.button.clicked.connect(self.on_button_click)
      
              self.layout().addWidget(self.pdf)
              self.layout().addWidget(self.button)
      
          def on_button_click(self):
              self.pdf.document().close()
              self.pdf.close()
              os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
      
      
      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      app.exec()
      

      Thats the code I have a problem with. When i run this under windows I get the following error:

      Traceback (most recent call last):
        File "C:\Users\Basti-PC\PycharmProjects\qtTesting\main.py", line 28, in on_button_click
          os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
      PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Basti-PC\\Documents\\test.pdf'
      

      On my mac everything works fine. But windows throws this errors. Any help?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #3

      @Blumi
      First thing to check is that it is not due to Python retaining a reference. Add

      self.pdf = None
      self.document = None
      

      before trying to remove.

      Next get rid of the QPdfView (i.e. don't create one) and see if behaviour happens on just QPdfDocument code.

      If the message is to be believed some other process has it open, not your app. Might be a process spawned by, say, the QPdfView, I don't know. Use Windows tools to discover which process still has it open.

      JonBJ 1 Reply Last reply
      2
      • B Offline
        B Offline
        Blumi
        wrote on last edited by
        #2

        Edit:

        import sys
        from PySide6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication
        from PySide6.QtPdf import QPdfDocument
        from PySide6.QtPdfWidgets import QPdfView
        import os
        
        
        class MainWindow(QWidget):
            def __init__(self):
                super().__init__()
                self.resize(1000, 800)
                self.setLayout(QVBoxLayout())
        
                self.pdf = QPdfView()
                self.document = QPdfDocument()
                self.document.load(r"C:\Users\Basti-PC\Documents\test.pdf")
                self.pdf.setDocument(self.document)
                self.button = QPushButton("Click me")
        
                self.button.clicked.connect(self.on_button_click)
        
                self.layout().addWidget(self.pdf)
                self.layout().addWidget(self.button)
        
            def on_button_click(self):
                self.pdf.document().close()
                self.pdf.close()
                os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
        
        
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        app.exec()
        

        Thats the code I have a problem with. When i run this under windows I get the following error:

        Traceback (most recent call last):
          File "C:\Users\Basti-PC\PycharmProjects\qtTesting\main.py", line 28, in on_button_click
            os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
        PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Basti-PC\\Documents\\test.pdf'
        

        On my mac everything works fine. But windows throws this errors. Any help?

        JonBJ 1 Reply Last reply
        0
        • B Blumi

          Edit:

          import sys
          from PySide6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication
          from PySide6.QtPdf import QPdfDocument
          from PySide6.QtPdfWidgets import QPdfView
          import os
          
          
          class MainWindow(QWidget):
              def __init__(self):
                  super().__init__()
                  self.resize(1000, 800)
                  self.setLayout(QVBoxLayout())
          
                  self.pdf = QPdfView()
                  self.document = QPdfDocument()
                  self.document.load(r"C:\Users\Basti-PC\Documents\test.pdf")
                  self.pdf.setDocument(self.document)
                  self.button = QPushButton("Click me")
          
                  self.button.clicked.connect(self.on_button_click)
          
                  self.layout().addWidget(self.pdf)
                  self.layout().addWidget(self.button)
          
              def on_button_click(self):
                  self.pdf.document().close()
                  self.pdf.close()
                  os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
          
          
          app = QApplication(sys.argv)
          window = MainWindow()
          window.show()
          app.exec()
          

          Thats the code I have a problem with. When i run this under windows I get the following error:

          Traceback (most recent call last):
            File "C:\Users\Basti-PC\PycharmProjects\qtTesting\main.py", line 28, in on_button_click
              os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
          PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Basti-PC\\Documents\\test.pdf'
          

          On my mac everything works fine. But windows throws this errors. Any help?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @Blumi
          First thing to check is that it is not due to Python retaining a reference. Add

          self.pdf = None
          self.document = None
          

          before trying to remove.

          Next get rid of the QPdfView (i.e. don't create one) and see if behaviour happens on just QPdfDocument code.

          If the message is to be believed some other process has it open, not your app. Might be a process spawned by, say, the QPdfView, I don't know. Use Windows tools to discover which process still has it open.

          JonBJ 1 Reply Last reply
          2
          • B Offline
            B Offline
            Blumi
            wrote on last edited by
            #4

            Tried both of your tips. Even without the QPdfView() the error happens. I used the resource monitor from windows to see which process blocks the file, but it only said that it is blocked by python.exe.
            Don't know what windows is doing different then mac to keep the file blocked after using it.
            More tips and hints would be appriciated.. :(

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Blumi
              wrote on last edited by
              #5

              Nvm. The tip to remove retaining references from self.document worked. I thought it didn't because i used ...

               def on_button_click(self):
                      self.document.close()
                      self.pdf.document = None
                      os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")  
              

              ...but i had to use...

               def on_button_click(self):
                      self.document.close()
                      self.document = None
                      os.remove(r"C:\Users\Basti-PC\Documents\test.pdf")
              

              with this its not even necessary to close() / None the QPdfView().

              Thanks a lot!!!

              1 Reply Last reply
              0
              • B Blumi has marked this topic as solved on
              • JonBJ JonB

                @Blumi
                First thing to check is that it is not due to Python retaining a reference. Add

                self.pdf = None
                self.document = None
                

                before trying to remove.

                Next get rid of the QPdfView (i.e. don't create one) and see if behaviour happens on just QPdfDocument code.

                If the message is to be believed some other process has it open, not your app. Might be a process spawned by, say, the QPdfView, I don't know. Use Windows tools to discover which process still has it open.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @JonB said in How can I release a pdf document for use again after it has been used with QPdfView in PyQt?:

                self.pdf = None
                self.document = None
                

                self.pdf = None gets rid of self.pdf.document. self.document = None is necessary because you have self.document = QPdfDocument().

                1 Reply Last reply
                2
                • A Armanta referenced this topic on

                • Login

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