Repository: py-pdf/pypdf
Pull request: DOC: Expand file size explanations
Closes https://github.com/py-pdf/pypdf/issues/1786

Changing the viewboxes ("cropping") has no impact on file size
Removing complete pages only has an impact if the connected resources are also removed

Problem statement:
Disproportionately large file sizes
when trying to split large pdf's, output file size is disproportionately large.
here is my code 
```
def split(path, file_name=""):
    reader = PdfReader(open(path, 'br'))
    for (i, page) in enumerate(reader.pages):
        reader = PdfReader(open(path, 'br'))
        writer = PdfWriter()
        writer.add_page(reader.pages[i])
        # output= f'{778+j2*i}(right).pdf'
        output= f'{file_name[4:-4]}-{i}(left).pdf'
        with open(join(getcwd(), "downloads/output-scans/left", output), 'wb') as output_file: writer.write(output_file)


dirlist = listdir(join(getcwd(), "downloads/scans/left"))
for file in dirlist:
    dirlist = listdir(join(getcwd(), "downloads/scans/left"))
    split(join(getcwd(), "downloads/scans/left", file), file)
    exit
```

each output file is the same exact size as the input. I have a hunch this is caused by pypdf trying to cram the entire input file contents into each output.

this does not occur with smaller pdfs.

## Environment

zsh
python 3.9.6
pypdf 3.7.1
Aarch M2 Max

Hints from issue discussion:
Can you please provide the pdf file for analysis 
happens with multiple diferent files. but here is an example
https://drive.google.com/file/d/1I4NiKaWQXnNwml8k6GDpdUP88KUbOeUU/view?usp=sharing
@pubpub-zz I feel like we should extend pdfly to show more details on the size of the contents of a pdf file (images, attachments, fonts). We had such questions a couple of times already and it was most of the time big background images or fonts, iirc
I've just done the analysis using PDF debugger (https://pdfbox.apache.org/1.8/commandline.html) which is listed in https://github.com/py-pdf/awesome-pdf

When you look at the pages they are all using the same resource object (id 2) 
![image](https://user-images.githubusercontent.com/4083478/231532273-1123bbd4-5386-4fd6-a1f6-37d5f570266a.png)

if you crawl deeper, you will find the images of all the pages. So extracting one page only will pull all the images. 
pypdf is doing its job😊, it is just an oddly generated file.

@MartinThoma improving pdfly could be nice but pdf debugger  or pdf vole are already usefull tools
I propose to close the issue. @MartinThoma do you think it worth to create a discussion ?
I was thinking about keeping it open until somebody improves the docs: https://pypdf.readthedocs.io/en/latest/user/file-size.html

1. Changing the viewboxes ("cropping") has no impact on file size
2. Removing complete pages only has an impact if the connected resources are also removed

@DIvkov575 Do you want to improve the documentation?
@pubpub-zz For (2): How does that actually work within pypdf: If a resource has no indirect reference, is it automatically removed? Or could there be objects without references?
> @pubpub-zz For (2): How does that actually work within pypdf: If a resource has no indirect reference, is it automatically removed? Or could there be objects without references?

it is possible to have objects not referenced but I agree it is unusual.
what exists also is some objects which are referenced but no usable. This is common in incremental updates. and but also with pypdf when appending (with add_page, append or during cloning) all the objects linked are sucked. For example if you have some articles in a page that are chained on other pages, the pages objects are copied but are not referenced in the pages list (and therefore not visible). because of this sucking with add_page, I recommend using append() which has been improved.
@MartinThoma sorry about late reply, I would be happy to update docs (if its not too late). 
Note. I have a series of exams coming up, so It may take me a week or two before I'm able to allocate enough time to finish updating the docs .

It's never too late to improve the docs :-)

Don't worry, we don't have time lines / time pressure. Your exams are more important :-)

Commit message: Update docs/user/file-size.md

Co-authored-by: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com>

Patch to review:
diff --git a/docs/user/file-size.md b/docs/user/file-size.md
index a7b2d3cc4..7748ce2aa 100644
--- a/docs/user/file-size.md
+++ b/docs/user/file-size.md
@@ -30,7 +30,7 @@ It depends on the PDF how well this works, but we have seen an 86% file
 reduction (from 5.7 MB to 0.8 MB) within a real PDF.
 
 
-## Remove images
+## Removing Images
 
 
 ```python
@@ -75,3 +75,10 @@ with open("out.pdf", "wb") as f:
 
 Using this method, we have seen a reduction by 70% (from 11.8 MB to 3.5 MB)
 with a real PDF.
+
+## Removing Sources
+
+When a page is deleting (removed from page list), the source will persist(the data may be still be used somewhere else). This will reduce the page count, while not reducing the file size. 
+To not include the code, the pages should not be added using `PdfWriter.append()` selecting the good pages only
+- Issues can arise with poor pdf formatting such as when all pages are linked to the same resource, making dropping refrences useless since there is 1 source for all pages.
+- Cropping / (adjust viewboxes) is an innefective way of reducing filesize, as the external part of the source image will still be present.


Code review comments:
diff_hunk: @@ -75,3 +75,10 @@ with open("out.pdf", "wb") as f:
 
 Using this method, we have seen a reduction by 70% (from 11.8 MB to 3.5 MB)
 with a real PDF.
+
+## Removing Sources
+
+When a page is deleting (removed from page list), the source will persist(the data may be still be used somewhere else). This will reduce the page count, while not reducing the file size. 
+To not include the code, the pages should not be added using `PdfWriter.append()` selecting the good pages only
+- Issues can arise with poor pdf formatting such as when all pages are linked to the same resource, making dropping refrences useless since there is 1 source for all pages.
+- Cropping / (adjust viewboxes) is an innefective way of reducing filesize, as the external part of the source image will still be present.
path: docs/user/file-size.md
text: @user1:
```suggestion
- Cropping is an ineffective way of reducing the file size, as the external part of the source image will still be present. Just the viewboxes are adjusted.
```