]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Save edited PDF to temp file before replacing original feature-pdf-editor 10318/head
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sat, 2 Aug 2025 13:45:46 +0000 (09:45 -0400)
committershamoon <4887959+shamoon@users.noreply.github.com>
Sat, 2 Aug 2025 13:45:46 +0000 (09:45 -0400)
src/documents/bulk_edit.py

index 8ef807da8507440ad6c8ab432d9e3a8d679352d1..13c95ce6c84f25a0b3de052e3e5693985a1f8306 100644 (file)
@@ -542,9 +542,13 @@ def edit_pdf(
                     dst.pages[-1].rotate(op["rotate"], relative=True)
 
         if update_document:
+            temp_path = doc.source_path.with_suffix(".tmp.pdf")
             pdf = pdf_docs[0]
             pdf.remove_unreferenced_resources()
-            pdf.save(doc.source_path)
+            # save the edited PDF to a temporary file in case of errors
+            pdf.save(temp_path)
+            # replace the original document with the edited one
+            temp_path.replace(doc.source_path)
             doc.checksum = hashlib.md5(doc.source_path.read_bytes()).hexdigest()
             doc.page_count = len(pdf.pages)
             doc.save()