Repository: python-pillow/Pillow

Issue:
PIL.PdfParser.PdfFormatError: xref entry duplicated (and not identical)
Hi boss
My program encountered a problem and I don't know how to solve it, please help, thanks
I have sent the relevant documents to email: [aclark@python-pillow.org]

this is my code:
```python
my_bytes = open(tiffilepath, 'rb').read()
my_image = Image.open(BytesIO(my_bytes))
my_image.save(fp=dstfilepath, append=True)
my_image.close()
```
error:
```pytb
    my_image.save(fp=outpdfpath, append=True)
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\Image.py", line 2431, in save
    save_handler(self, fp, filename)
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfImagePlugin.py", line 52, in _save
    existing_pdf = PdfParser.PdfParser(f=fp, filename=filename, mode="r+b")
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfParser.py", line 379, in __init__
    self.read_pdf_info()
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfParser.py", line 545, in read_pdf_info
    self.read_trailer()
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfParser.py", line 639, in read_trailer
    self.read_prev_trailer(self.trailer_dict[b"Prev"])
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfParser.py", line 642, in read_prev_trailer
    trailer_offset = self.read_xref_table(xref_section_offset=xref_section_offset)
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfParser.py", line 965, in read_xref_table
    check_format_condition(
  File "C:\Users\S0049\AppData\Local\Programs\Python\Python39\lib\site-packages\PIL\PdfParser.py", line 78, in check_format_condition
    raise PdfFormatError(error_message)
PIL.PdfParser.PdfFormatError: xref entry duplicated (and not identical)
```


Discussion:
So to be clear, you're not able to attach the TIFF image here? I presume because you're not comfortable posting the image publicly?
Having seen [another issue that you've created recently](https://github.com/pymupdf/PyMuPDF/issues/2320), it appears that you just don't think you can post TIFF images here.

I know that GitHub will say 'We don't support that file type' if you try and upload a TIFF image directly. However, you can either
a) create a zip of the image, and then upload that
or
b) just change the file extension to something else, from '.tiff' to say '.txt', and then upload that
Thank you for your patient guidance. By dragging and dropping, I was able to upload files. Thank you very much: 
[TIFF.zip](https://github.com/python-pillow/Pillow/files/11217259/TIFF.zip)

Yes, just because I don't know how to upload the problematic file, I'm very sorry

> So to be clear, you're not able to attach the TIFF image here? I presume because you're not comfortable posting the image publicly?

no, just because I don't know how to upload the problematic file, I'm very sorry
How did you create this PDF?

Resolving patch:
diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py
--- a/src/PIL/PdfParser.py
+++ b/src/PIL/PdfParser.py
@@ -957,14 +957,11 @@ def read_xref_table(self, xref_section_offset):
                 check_format_condition(m, "xref entry not found")
                 offset = m.end()
                 is_free = m.group(3) == b"f"
-                generation = int(m.group(2))
                 if not is_free:
+                    generation = int(m.group(2))
                     new_entry = (int(m.group(1)), generation)
-                    check_format_condition(
-                        i not in self.xref_table or self.xref_table[i] == new_entry,
-                        "xref entry duplicated (and not identical)",
-                    )
-                    self.xref_table[i] = new_entry
+                    if i not in self.xref_table:
+                        self.xref_table[i] = new_entry
         return offset
 
     def read_indirect(self, ref, max_nesting=-1):


Test patch:
diff --git a/Tests/images/duplicate_xref_entry.pdf b/Tests/images/duplicate_xref_entry.pdf
new file mode 100644
Binary files /dev/null and b/Tests/images/duplicate_xref_entry.pdf differ
diff --git a/Tests/test_pdfparser.py b/Tests/test_pdfparser.py
--- a/Tests/test_pdfparser.py
+++ b/Tests/test_pdfparser.py
@@ -117,3 +117,9 @@ def test_pdf_repr():
     assert pdf_repr(b"a)/b\\(c") == rb"(a\)/b\\\(c)"
     assert pdf_repr([123, True, {"a": PdfName(b"b")}]) == b"[ 123 true <<\n/a /b\n>> ]"
     assert pdf_repr(PdfBinary(b"\x90\x1F\xA0")) == b"<901FA0>"
+
+
+def test_duplicate_xref_entry():
+    pdf = PdfParser("Tests/images/duplicate_xref_entry.pdf")
+    assert pdf.xref_table.existing_entries[6][0] == 1197
+    pdf.close()