Skip to content

ENH: Preserve OCGs when merging PDFs - #3980

Open
mTangtrongwanit wants to merge 22 commits into
py-pdf:mainfrom
mTangtrongwanit:appendOCGs
Open

mTangtrongwanit wants to merge 22 commits into
py-pdf:mainfrom
mTangtrongwanit:appendOCGs

Conversation

@mTangtrongwanit

Copy link
Copy Markdown

The goal of this PR is to preserve OCGs when appending PDFs together.

In essence, this implementation is based on how while pypdf does track the OCGs via "OC_PROPERTIES" (or "/OCProperties"), it does not preserve them in the merge process.

By translating OCG references from the source PDFs to be appended into the writer to preserve the OCGs, the nested layer ordering and functionality is maintained.

This comes as a result of work by Esri developers to resolve PDF issues in our software and the insights provided in #92.

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

I have left some basic inline remarks, but did not do a full review yet as there are too much changes to be done before. In the future, consider commenting on the issue first about possible implementation details.

Besides the inline comments, where I only marked one occurrence of each issue, please have a look at the test failures - your are referencing sample files which do not exist at the moment and where I am not aware of a parallel PR at the corresponding repository at the moment as well.

Comment thread pypdf/_writer.py Resolved
Comment thread pypdf/_writer.py Resolved
Comment thread pypdf/_writer.py Resolved
Comment thread pypdf/_writer.py Resolved
Comment thread pypdf/_writer.py Resolved
Uploading PDFs with OCGs to be used by the "test_ocg_append.py" script
@mTangtrongwanit

Copy link
Copy Markdown
Author

Thanks for the PR.

I have left some basic inline remarks, but did not do a full review yet as there are too much changes to be done before. In the future, consider commenting on the issue first about possible implementation details.

Besides the inline comments, where I only marked one occurrence of each issue, please have a look at the test failures - your are referencing sample files which do not exist at the moment and where I am not aware of a parallel PR at the corresponding repository at the moment as well.

Thanks for the quick response! There were a number of items I missed in the contribution doc so I apologize for the trouble there.

On that note, I wasn't able to find a process for uploading test files so I just uploaded them to the "resource" folder in my fork though it appears I still need to review the docs on test setup some more.

And to confirm, instead of submitting the PR first, it would have been better to post to the OCG thread (#92) first right?

I'll also review the provided comments as well.

@stefan6419846

Copy link
Copy Markdown
Collaborator

On that note, I wasn't able to find a process for uploading test files so I just uploaded them to the "resource" folder in my fork though it appears I still need to review the docs on test setup some more.

Please avoid uploading such large files to the repository directly. If you are able to provide them under the terms of the sample files repository at https://github.com/py-pdf/sample-files/, you can create a PR there to get them integrated. Otherwise, we would have to download them on the fly, which has its own issues.

In general it is appreciated to have small sample files inside the sample files repository.

And to confirm, instead of submitting the PR first, it would have been better to post to the OCG thread (#92) first right?

As this thread is a larger one, yes, even if it is just about a part of the requested aspects. Especially the correct module to put the code into could have been discussed before, instead of causing both you and me the overhead of at least partially doing it twice.

@mTangtrongwanit

Copy link
Copy Markdown
Author

I do see at least 2 error s on the "windows" pytest ("FileNotFoundError: [Errno 2] No such file or directory: 'D:\a\pypdf\pypdf\resources\ocg_map_test.pdf'") which I hope are resolved once the sample files PR is commited (py-pdf/sample-files#42) as it is from my test file "test_ocg_append.py".

The other error from the "test_workflows.py" is definitely something to dig into though. The error: "Failed: len(actual) = 217,997 vs len(expected) = 217,857" at a glance might indicate that the PDF (with the OCG structures preserved) is bigger than the test's "expected" amount but might be inline with the new OCG preserving behavior?

@stefan6419846

Copy link
Copy Markdown
Collaborator

As soon as the associated sample files is merged, we can update the submodule commit in pypdf and thus allow the new file to be used. Until this is done, I will not look into further reviewing this PR.

The other error from the "test_workflows.py" is definitely something to dig into though. The error: "Failed: len(actual) = 217,997 vs len(expected) = 217,857" at a glance might indicate that the PDF (with the OCG structures preserved) is bigger than the test's "expected" amount but might be inline with the new OCG preserving behavior?

I have not checked whether the original file had OCGs. To resolve this, I would recommend you to run the corresponding test locally and create a proper diff on the data (this would fail CI for too large data). Based on this diff, we can check further.

@mTangtrongwanit

Copy link
Copy Markdown
Author

As soon as the associated sample files is merged, we can update the submodule commit in pypdf and thus allow the new file to be used. Until this is done, I will not look into further reviewing this PR.

The other error from the "test_workflows.py" is definitely something to dig into though. The error: "Failed: len(actual) = 217,997 vs len(expected) = 217,857" at a glance might indicate that the PDF (with the OCG structures preserved) is bigger than the test's "expected" amount but might be inline with the new OCG preserving behavior?

I have not checked whether the original file had OCGs. To resolve this, I would recommend you to run the corresponding test locally and create a proper diff on the data (this would fail CI for too large data). Based on this diff, we can check further.

Hello, I was able to to pull down the "sample-files" via git submodule and updated my test file to target the path to the uploaded OCG PDF.

With this could further review of the PR continue or are there any additional steps I might need to take? I did merge in changes from main to make sure that my changes are still working.

@stefan6419846

Copy link
Copy Markdown
Collaborator

With this could further review of the PR continue or are there any additional steps I might need to take?

You need to push the updated submodule reference for the tests to work correctly.

Afterwards, please create the requested diff as per #3980 (comment) and provide it here in the comments to decide on how to continue with the third failing test. If this test failure is reviewed and a fix pushed, I can start with the actual review.

@mTangtrongwanit

Copy link
Copy Markdown
Author

With this could further review of the PR continue or are there any additional steps I might need to take?

You need to push the updated submodule reference for the tests to work correctly.

Afterwards, please create the requested diff as per #3980 (comment) and provide it here in the comments to decide on how to continue with the third failing test. If this test failure is reviewed and a fix pushed, I can start with the actual review.

Thanks for your patience, I've updated the submodule commit and the tests are working properly.

However, I've tried to look through the documentation to find an example to follow, but I'd like to confirm what kind of diff I need to make (from Adobe Acrobat or a pypdf function I might have missed)? I'm assuming the diff is meant to compare the internal PDF text structures right?

@stefan6419846

Copy link
Copy Markdown
Collaborator

However, I've tried to look through the documentation to find an example to follow, but I'd like to confirm what kind of diff I need to make (from Adobe Acrobat or a pypdf function I might have missed)? I'm assuming the diff is meant to compare the internal PDF text structures right?

There are no direct examples for this, as it happens too rarely to be relevant in most cases.

Please provide the following:

  • The generated file after your changes.
  • A basic file diff, for example generated using git diff old.pdf new.pdf.

@mTangtrongwanit

mTangtrongwanit commented Sep 15, 2026 •

Copy link
Copy Markdown
Author

However, I've tried to look through the documentation to find an example to follow, but I'd like to confirm what kind of diff I need to make (from Adobe Acrobat or a pypdf function I might have missed)? I'm assuming the diff is meant to compare the internal PDF text structures right?

There are no direct examples for this, as it happens too rarely to be relevant in most cases.

Please provide the following:

  • The generated file after your changes.
  • A basic file diff, for example generated using git diff old.pdf new.pdf.

I'm uploading the new file: "Seige_of_Vicksburg_Sample_OCR-crazyones-merged-New-File.pdf"
Seige_of_Vicksburg_Sample_OCR-crazyones-merged-New-File.pdf

The original file is from the repo: https://github.com/py-pdf/pypdf/blob/main/resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf

image

I tried using the "git diff" method, but from what I understand it should mainly check for text differences so I think it would dig into the the changed PDF structure. The diff returned a 0 byte file back so I believe there aren't any visual differences between the two PDFs, but you can note that the newer PDF will have OCGs as an option:
image

Additionally, I've used some online diff checkers and they also reflect that the PDF text matches between the two while also reflecting a size difference (due to the OCG structures being preserved):
image

image

@stefan6419846

Copy link
Copy Markdown
Collaborator

This is the diff (needs the --text flag). As far as I can tell, this mostly adds /OCProperties and copies over the corresponding data to an early object, which then shifts most of the other object indices up by one.

diff --git a/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf b/Seige_of_Vicksburg_Sample_OCR-crazyones-merged-New-File.pdf
index 82d2ad28..2017839b 100644
--- a/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf
+++ b/Seige_of_Vicksburg_Sample_OCR-crazyones-merged-New-File.pdf
@@ -9,7 +9,7 @@ endobj
 <<
 /Type /Pages
 /Count 7
-/Kids [ 4 0 R 57 0 R 17 0 R 25 0 R 33 0 R 40 0 R 48 0 R ]
+/Kids [ 4 0 R 58 0 R 17 0 R 25 0 R 33 0 R 40 0 R 48 0 R ]
 >>
 endobj
 3 0 obj
@@ -17,6 +17,7 @@ endobj
 /Type /Catalog
 /Pages 2 0 R
 /Outlines 56 0 R
+/OCProperties 57 0 R
 >>
 endobj
 4 0 obj
@@ -1527,46 +1528,55 @@ endobj
 endobj
 57 0 obj
 <<
-/Resources 58 0 R
+/OCGs [ 7 0 R 11 0 R ]
+/D <<
+/ON [ 7 0 R 11 0 R ]
+/Order [ 7 0 R 11 0 R ]
+>>
+>>
+endobj
+58 0 obj
+<<
+/Resources 59 0 R
 /Type /Page
-/Contents [ 73 0 R ]
+/Contents [ 74 0 R ]
 /MediaBox [ 0 0 612 792 ]
 /Parent 2 0 R
 >>
 endobj
-58 0 obj
+59 0 obj
 <<
 /Font <<
-/F1 59 0 R
-/F2 65 0 R
-/F3 69 0 R
+/F1 60 0 R
+/F2 66 0 R
+/F3 70 0 R
 >>
 /ProcSet [ /PDF /Text /ImageC /ImageB /ImageI ]
 >>
 endobj
-59 0 obj
+60 0 obj
 <<
 /Type /Font
 /Subtype /Type1
-/Widths 60 0 R
+/Widths 61 0 R
 /FirstChar 67
 /LastChar 122
-/Encoding 61 0 R
-/ToUnicode 62 0 R
+/Encoding 62 0 R
+/ToUnicode 63 0 R
 /BaseFont /HHXGQB+SFTI1440
-/FontDescriptor 63 0 R
+/FontDescriptor 64 0 R
 >>
 endobj
-60 0 obj
+61 0 obj
 [ 684.7 0 0 0 0 0 0 0 0 0 0 0 733.6 0 0 0 0 684.7 0 0 0 0 0 0 0 0 0 0 0 0 489.1 0 0 0 440.2 0 0 489.1 0 0 0 0 0 538 0 0 0 403.5 391.3 0 0 0 0 0 464.6 391.3 ]
 endobj
-61 0 obj
+62 0 obj
 <<
 /BaseEncoding /WinAnsiEncoding
 /Differences [ 27 /ff /fi ]
 >>
 endobj
-62 0 obj
+63 0 obj
 <<
 /Filter /FlateDecode
 /Length 227
@@ -1576,7 +1586,7 @@ x
 �7�*|������io���oC+^��t��{@8�uw��5�ҹɃ���'�5���W�G|��'�����[����B���]���u�����-�Ai$�fd��j�m[3t�_�`���QĤ���K"���ͬ��MoDɷ\�$�����H���*����m�
 endstream
 endobj
-63 0 obj
+64 0 obj
 <<
 /Type /FontDescriptor
 /CapHeight 684
@@ -1586,11 +1596,11 @@ endobj
 /StemV 50
 /Flags 70
 /FontBBox [ -88 -320 1347 942 ]
-/FontFile3 64 0 R
+/FontFile3 65 0 R
 /FontName /HHXGQB+SFTI1440
 >>
 endobj
-64 0 obj
+65 0 obj
 <<
 /Subtype /Type1C
 /Filter /FlateDecode
@@ -1604,23 +1614,23 @@ x
 9����;��A�K���2G�(�������	ܬ�<!o0>>z��tA���O�b><7��Xu^���'Ktŏt��L�L5����.�����K
�����j����G���^$O�@K�5�M�k�5���vO|Իu��[�� (��/@��eye��=�;f��Zx��HXk�T���ٯ4�7��h�As���j�!F�B�d�/��iTk���th�v�| w�����T�Lx�.���>1�P����Zp6:y�1����d�������&���/�V����2��N���Ŧmn�%&Ë��D���h���@1�٤���������*�Cy#{��'��3�x.�5�ϐ"'f)J���H���&df� ���v����3~����@�3�}t@LЮs.Xq�����������^�3^�t�(���LM������2�W{��ϸiW����D����z��C>��A�y�n��h`���Z��z1�YY��\���o�F�Wd�-��� �3v����'��{ଫ�b���C�B�n���L(���Lidyִ@V�'�G��/��������+��Gl�x
 endstream
 endobj
-65 0 obj
+66 0 obj
 <<
 /Type /Font
 /Subtype /Type1
-/Widths 66 0 R
+/Widths 67 0 R
 /FirstChar 44
 /LastChar 116
-/Encoding 61 0 R
-/ToUnicode 62 0 R
+/Encoding 62 0 R
+/ToUnicode 63 0 R
 /BaseFont /YISQAD+SFTI1200
-/FontDescriptor 67 0 R
+/FontDescriptor 68 0 R
 >>
 endobj
-66 0 obj
+67 0 obj
 [ 299.9 0 0 0 0 499.9 0 0 499.9 0 0 0 499.9 499.9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 749.8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 449.9 449.9 0 449.9 0 0 0 0 0 0 0 0 0 499.9 0 0 412.4 0 324.9 ]
 endobj
-67 0 obj
+68 0 obj
 <<
 /Type /FontDescriptor
 /CapHeight 684
@@ -1630,11 +1640,11 @@ endobj
 /StemV 50
 /Flags 70
 /FontBBox [ -91 -320 1380 938 ]
-/FontFile3 68 0 R
+/FontFile3 69 0 R
 /FontName /YISQAD+SFTI1200
 >>
 endobj
-68 0 obj
+69 0 obj
 <<
 /Subtype /Type1C
 /Filter /FlateDecode
@@ -1646,23 +1656,23 @@ x
 �\SK�4�^�x�)�����?z����<ON�_��8��+ݧp����>����+�Y����P��v�4�*����x���4���EF�o�y�%�A�K���u�.�W+7�W������*HN��ܹ����A�Ƒғ[��[UT�_P��֡5ǖ~M߅{�F~��+��Hd�,�P\�0��y�; �0_�������	q*z���?�t���Ra
rt����]�������R��������hͩ����I<'�O�<�9�n����{�lʶ�(�l�b0V�� �.�%���������k��t����!���x>_�4�YZ�6J��*i���2�ZL`BM�.��>��	tv�������h/3\߿�����)�8�Ne�[��@���y�����A�N��K�k���(���Pb�ܯu[�Ve"��,�ѽ�8�O\�fmtb����(@�!��`����r\��\�ޭP�[ZL9-n����z�]�փ��u�}���c��>���J�����\=x���TX�c����{=�����:}�I<���� 4I&�����!�f�6]�*lo!�O���'ڇ�ʏ!r��k�����`��t� X[�3�s��C&��#d,v���A�3�����z�H��>��i�����bz��>�QڙitVTؘ5���Y��q��x
 endstream
 endobj
-69 0 obj
+70 0 obj
 <<
 /Type /Font
 /Subtype /Type1
-/Widths 70 0 R
+/Widths 71 0 R
 /FirstChar 27
 /LastChar 122
-/Encoding 61 0 R
-/ToUnicode 62 0 R
+/Encoding 62 0 R
+/ToUnicode 63 0 R
 /BaseFont /TITXYI+SFRM0900
-/FontDescriptor 71 0 R
+/FontDescriptor 72 0 R
 >>
 endobj
-70 0 obj
+71 0 obj
 [ 599.4 570.8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 285.4 0 285.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 485.2 0 770.5 727.8 0 0 0 0 0 770.5 0 0 0 0 941.7 0 799.2 0 0 0 0 742.1 0 0 1055.9 0 770.5 0 0 0 0 0 0 0 513.8 570.8 456.7 570.8 457.1 314 513.8 570.8 285.4 0 542.3 285.4 856.3 570.8 513.8 570.8 542.3 401.9 405.3 399.6 570.8 542.3 742.1 542.3 542.3 456.7 ]
 endobj
-71 0 obj
+72 0 obj
 <<
 /Type /FontDescriptor
 /CapHeight 684
@@ -1672,11 +1682,11 @@ endobj
 /StemV 50
 /Flags 6
 /FontBBox [ -196 -322 1502 937 ]
-/FontFile3 72 0 R
+/FontFile3 73 0 R
 /FontName /TITXYI+SFRM0900
 >>
 endobj
-72 0 obj
+73 0 obj
 <<
 /Subtype /Type1C
 /Filter /FlateDecode
@@ -1704,7 +1714,7 @@ j
 wK2D���]����:
&�U��V_àۤ�m������TY�hw�~������h�
4�s4�fwԀ���XU��ysdI�����Hv3P̘Xעq���Ƙƍ�ẕ�����9�v��n�� ����
 endstream
 endobj
-73 0 obj
+74 0 obj
 <<
 /Filter /FlateDecode
 /Length 856
@@ -1716,87 +1726,88 @@ x
 endstream
 endobj
 xref
-0 74
+0 75
 0000000000 65535 f 
 0000000015 00000 n 
 0000000054 00000 n 
 0000000155 00000 n 
-0000000221 00000 n 
-0000000430 00000 n 
-0000000569 00000 n 
-0000000629 00000 n 
-0000000680 00000 n 
-0000016700 00000 n 
-0000018315 00000 n 
-0000030757 00000 n 
-0000030809 00000 n 
-0000030964 00000 n 
-0000031026 00000 n 
-0000032073 00000 n 
-0000032194 00000 n 
-0000035801 00000 n 
-0000036024 00000 n 
-0000058520 00000 n 
-0000060177 00000 n 
-0000075149 00000 n 
-0000075304 00000 n 
-0000076353 00000 n 
-0000076474 00000 n 
-0000079914 00000 n 
-0000080137 00000 n 
-0000099631 00000 n 
-0000101276 00000 n 
-0000115562 00000 n 
-0000115717 00000 n 
-0000116770 00000 n 
-0000116891 00000 n 
-0000120421 00000 n 
-0000120644 00000 n 
-0000121413 00000 n 
-0000123054 00000 n 
-0000138750 00000 n 
-0000138905 00000 n 
-0000139026 00000 n 
-0000142960 00000 n 
-0000143194 00000 n 
-0000143972 00000 n 
-0000145615 00000 n 
-0000161462 00000 n 
-0000161617 00000 n 
-0000162663 00000 n 
-0000162784 00000 n 
-0000166647 00000 n 
-0000166892 00000 n 
-0000182043 00000 n 
-0000183700 00000 n 
-0000198958 00000 n 
-0000199113 00000 n 
-0000200170 00000 n 
-0000200291 00000 n 
-0000203823 00000 n 
-0000203845 00000 n 
-0000203958 00000 n 
-0000204073 00000 n 
-0000204251 00000 n 
-0000204425 00000 n 
-0000204506 00000 n 
-0000204806 00000 n 
-0000205008 00000 n 
-0000207074 00000 n 
-0000207252 00000 n 
-0000207466 00000 n 
-0000207668 00000 n 
-0000209657 00000 n 
-0000209835 00000 n 
-0000210198 00000 n 
-0000210395 00000 n 
-0000215368 00000 n 
+0000000242 00000 n 
+0000000451 00000 n 
+0000000590 00000 n 
+0000000650 00000 n 
+0000000701 00000 n 
+0000016721 00000 n 
+0000018336 00000 n 
+0000030778 00000 n 
+0000030830 00000 n 
+0000030985 00000 n 
+0000031047 00000 n 
+0000032094 00000 n 
+0000032215 00000 n 
+0000035822 00000 n 
+0000036045 00000 n 
+0000058541 00000 n 
+0000060198 00000 n 
+0000075170 00000 n 
+0000075325 00000 n 
+0000076374 00000 n 
+0000076495 00000 n 
+0000079935 00000 n 
+0000080158 00000 n 
+0000099652 00000 n 
+0000101297 00000 n 
+0000115583 00000 n 
+0000115738 00000 n 
+0000116791 00000 n 
+0000116912 00000 n 
+0000120442 00000 n 
+0000120665 00000 n 
+0000121434 00000 n 
+0000123075 00000 n 
+0000138771 00000 n 
+0000138926 00000 n 
+0000139047 00000 n 
+0000142981 00000 n 
+0000143215 00000 n 
+0000143993 00000 n 
+0000145636 00000 n 
+0000161483 00000 n 
+0000161638 00000 n 
+0000162684 00000 n 
+0000162805 00000 n 
+0000166668 00000 n 
+0000166913 00000 n 
+0000182064 00000 n 
+0000183721 00000 n 
+0000198979 00000 n 
+0000199134 00000 n 
+0000200191 00000 n 
+0000200312 00000 n 
+0000203844 00000 n 
+0000203866 00000 n 
+0000203965 00000 n 
+0000204078 00000 n 
+0000204193 00000 n 
+0000204371 00000 n 
+0000204545 00000 n 
+0000204626 00000 n 
+0000204926 00000 n 
+0000205128 00000 n 
+0000207194 00000 n 
+0000207372 00000 n 
+0000207586 00000 n 
+0000207788 00000 n 
+0000209777 00000 n 
+0000209955 00000 n 
+0000210318 00000 n 
+0000210515 00000 n 
+0000215488 00000 n 
 trailer
 <<
-/Size 74
+/Size 75
 /Root 3 0 R
 /Info 1 0 R
 >>
 startxref
-216297
+216417
 %%EOF

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left the first round of inline comments.

Please note that once the initial round of tests pass, we will most likely get tons of uncovered lines and branches which need more testing for proper coverage and allowing us to actually merge these changes.

DictionaryObject,
)

if sys.version_info >= (3, 11):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this used for?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a leftover of when I was trying to copy the patterns from one of the other "generic" files. I just forgot to get rid of it when I was testing the file. I've removed this.

cast,
)

from ..constants import (

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this multiline for one single value?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a leftover of when attempting to follow the "generic" file pattern. I've changed it.

values: The array of indirect objects to which the new object should be appended.
value: The indirect object to be appended if it is not already present in the array.
"""
if isinstance(value, IndirectObject) and any(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition looks a bit complex/hard to grasp. Are we able to make this more readable?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored "_append_unique_indirect_reference" and the condition within to be more readable

Comment thread tests/test_ocg_append.py Resolved
Comment thread tests/test_ocg_append.py Outdated

writer.write(RESOURCE_ROOT / "output-ocg_map_test-1.pdf")

def test_ocg_2():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a more specific name.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as part of refactor

Comment thread tests/test_ocg_append.py Outdated
# Trying to get OCGs to append while preserving the OCGs in the original document
reader = PdfReader(SAMPLE_ROOT / "029-optional-content-groups/ocg_map_test.pdf")
writer = PdfWriter()
# writer.append(reader)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as part of refactor

Comment thread tests/test_ocg_append.py Outdated
reader = PdfReader(SAMPLE_ROOT / "029-optional-content-groups/ocg_map_test.pdf")
writer = PdfWriter()
# writer.append(reader)
writer.clone_document_from_reader(reader)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as part of refactor

Comment thread tests/test_ocg_append.py Outdated
# Trigger the append with the modified merge code that include the OCGs
writer.append(reader_append)

writer.write(RESOURCE_ROOT / "output-ocg_map_test-2.pdf")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as part of refactor

@stefan6419846 stefan6419846 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left the first round of inline comments.

Please note that once the initial round of tests pass, we will most likely get tons of uncovered lines and branches which need more testing for proper coverage and allowing us to actually merge these changes.

@mTangtrongwanit

Copy link
Copy Markdown
Author

Sorry this took so long, I've been juggling some other projects and it took time before I could properly sit down for this.

@mTangtrongwanit

Copy link
Copy Markdown
Author

Hello, just checking in to see how the review has been going?

@stefan6419846

Copy link
Copy Markdown
Collaborator

As mentioned in my last comment, I am awaiting your fixes to the current CI failures. Afterwards, I can have another look when my time permits.

@mTangtrongwanit

Copy link
Copy Markdown
Author

As mentioned in my last comment, I am awaiting your fixes to the current CI failures. Afterwards, I can have another look when my time permits.

I can resolve the remaining error in the CI tests by replacing the "Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf" (original: https://github.com/mTangtrongwanit/pypdf-Add-OCGs/blob/appendOCGs/resources/Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf) in the "resources" folder (in my branch) with the newly generated file.

I just want to confirm that would be acceptable?

I've also got another test to add as I've found that choosing to use "clone_from" to make a new PDF vs appending to a blank PDFWriter can lead to notably different file size due to metadata being preserved in the former:
image

I'll take some better screenshots + add testing later when I get a chance.

@stefan6419846

Copy link
Copy Markdown
Collaborator

The main issue has been documented in #3980 (comment), and this diff indicates that the changes are expected and we can most likely replace the existing Seige_of_Vicksburg_Sample_OCR-crazyones-merged.pdf file.

I've also got another test to add as I've found that choosing to use "clone_from" to make a new PDF vs appending to a blank PDFWriter can lead to notably different file size due to metadata being preserved in the former

This is the expected behavior, as arbitrary metadata will not be added when starting from a blank file (see docs).

@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.26%. Comparing base (a3061e3) to head (b58343d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3980      +/-   ##
==========================================
+ Coverage   98.22%   98.26%   +0.03%     
==========================================
  Files          59       60       +1     
  Lines       11920    12161     +241     
  Branches     2249     2326      +77     
==========================================
+ Hits        11709    11950     +241     
  Misses        119      119              
  Partials       92       92              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mTangtrongwanit

Copy link
Copy Markdown
Author

Well, looks like all the standard tests are passing except those 2 codecov ones? At a glance, it seems the issue there is that the branch is just out of date so updating should resolve it right?

The code should be fine as it's largely self contained (barring the few lines in the writer).

@stefan6419846

Copy link
Copy Markdown
Collaborator

Well, looks like all the standard tests are passing except those 2 codecov ones?

Yes, the current set of tests is passing.

At a glance, it seems the issue there is that the branch is just out of date so updating should resolve it right?

Especially with larger changes on main, it is always advisable to sync with main. The failures are unrelated to this and indicates that most of the code you wrote is never run in CI and thus never checked for correctness. This needs to be fixed, as already predicted in #3980 (review).

@mTangtrongwanit

Copy link
Copy Markdown
Author

@stefan6419846 I've applied updates which should resolve the coverage issues that were brought up.

I can work to address other issues that can come up from further review, but I just wanted to check in and see if there were any blockers I missed?

@stefan6419846

Copy link
Copy Markdown
Collaborator

The current blocker mostly is my time to address larger changes besides the smaller changes, security reviews and fixes and my private stuff. Thus it might take a few weeks until I am able to have a proper look at this.

@mTangtrongwanit

Copy link
Copy Markdown
Author

The current blocker mostly is my time to address larger changes besides the smaller changes, security reviews and fixes and my private stuff. Thus it might take a few weeks until I am able to have a proper look at this.

No problem! I'm also fairly tied up on my end as well so I understand.

Looking forward to when you do have time to review though.

@mTangtrongwanit

Copy link
Copy Markdown
Author

Hello, it's been a while and I know time will be tighter with the holidays coming up, but I just wanted to check in if you've had a chance to review this item yet?

@stefan6419846

Copy link
Copy Markdown
Collaborator

My maintainer backlog kept growing since your last change and I am doing my best to prioritize work, but given the size of this PR, I need time I am currently unable to spend besides more critical stuff, thus further delaying it. The upcoming holidays might allow me to block some more time for this, but this depends on further factors I am unable to influence most of the times. As always, I am open for other persons interested in this feature to give it a try and support with the review.

As the next step, I would recommend you to look into the current merge conflicts to allow a clean review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants