How can I release a pdf document for use again after it has been used with QPdfView in PyQt?
-
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?
-
@Blumi
First thing to check is that it is not due to Python retaining a reference. Addself.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 justQPdfDocument
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. -
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?
-
@Blumi
First thing to check is that it is not due to Python retaining a reference. Addself.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 justQPdfDocument
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. -
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.. :( -
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!!!
-
-
@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 ofself.pdf.document
.self.document = None
is necessary because you haveself.document = QPdfDocument()
.