Before Compressing a PDF, Decide If Image Loss Is OK
below only checks lossless compression of the content stream: the decoded bytes before and after compression should be identical to the extracted text. It does
If you translate PDFs, you may be tempted to shrink them before processing to make the work faster or keep file sizes down. Compression can help, but it matters what it does to your content. Some methods leave text and images untouched, while others delete images outright.
This guide is for people who prepare PDFs for translation and want to know which compression steps are safe to run. It explains what lossless content-stream compression changes, why image removal is a different kind of operation, and how to check your results before you rely on them.
By the end, you will know which step fits your situation, what each one does to your files, and how to verify the output. The examples here come from synthetic samples, so treat them as a starting point and test them on your own files.
This guide uses synthetic samples to show the processing method. Before you process important files, keep the original copies and check your results against the boundaries and acceptance criteria described here.
Can you compress a PDF before translating it? The short answer
Yes, if you use lossless content-stream compression, which leaves text, fonts and image pixels unchanged, but do not remove images unless your task does not need them.
| Your situation | What to do | Why |
|---|---|---|
| You want a smaller file and must keep text and images intact | Compress the content streams | The decoded bytes match the extracted text, and text, fonts and image pixels stay unchanged |
| You only need plain text, such as for text extraction | Remove images with remove_images() | It deletes images completely; it is not compression |
| You still need the images for translation | Do not call remove_images() during translation prep | Pypdf only re-encodes mechanically and cannot judge the semantic content of an image |
| You expect every PDF to get smaller | Check the result instead of assuming it | Not every PDF will shrink |
| The file is important | Keep the original and verify the output | Synthetic tests do not guarantee results on real files |
Understand what lossless compression actually changes
Lossless compression of the content stream is the only approach covered here that keeps your document's data intact. The check is simple: the decoded bytes before and after compression should be identical to the extracted text. This method does not guarantee that every PDF will become smaller. It also should not be confused with JPEG's lossy re-encoding, which discards image data.
- Text and vector paths remain unchanged.
- Font embeddings remain unchanged.
- Original pixel data in images remains unchanged.
- Compression settings are fixed:
compress_content_streamsdoes not accept a compression level parameter and uses the default zlib configuration internally.
The key point is that compressing the content stream does not change what the reader sees. You can run this step during translation preprocessing as part of standardizing your files, without worrying that the original text will become harder to read.
Dependencies are listed in the import section of the code. The code builds its inputs in memory.
from io import BytesIO
import pymupdf
from pypdf import PdfReader, PdfWriter
with pymupdf.open() as source:
page = source.new_page()
page.insert_text((50, 50), "Compression sample")
raw = source.tobytes()
writer = PdfWriter(clone_from=BytesIO(raw))
before = writer.pages[0].get_contents().get_data()
writer.pages[0].compress_content_streams()
out = BytesIO()
writer.write(out)
out.seek(0)
reader = PdfReader(out)
assert reader.pages[0].get_contents().get_data() == before
assert "Compression sample" in reader.pages[0].extract_text()
assert len(reader.pages) == 1
Know why image removal is different
remove_images() is a complete deletion, not a compression step. Once images are removed, you cannot recover them from the processed file, so the operation is irreversible.
Do not use it during translation preparation unless you know the target use does not need images, such as plain text extraction. Many people assume that "compressing images" is the same as "intelligent optimization". That is not the case. Pypdf performs mechanical re-encoding only and cannot perceive what an image means.
Compare the two compression paths
The following code builds a PDF containing repeated images and multi-page content. It shows the effects of lossless structural compression and lossy image processing, side by side.
Note: This code illustrates the API's behavior boundaries with synthetic samples. Verify it in your own environment before you use it.
The two paths behave very differently, so choose based on what your translation workflow needs.
Lossless structural compression
- Compresses the content streams.
- Keeps the decoded bytes identical to the extracted text.
- Leaves text, fonts and image pixels unchanged.
- Can be run during translation prep as part of standardization.
Lossy image processing
- Removing images with
remove_images()deletes them completely. - Images cannot be recovered afterwards.
- Suitable only when images are not needed, such as for plain text extraction.
Handle exceptions with a clear decision
When a check fails or gives an unexpected result, decide what to do before you go further. The table below sets out how to handle common observations.
| Observation | What to do | What not to infer |
|---|---|---|
| The decoded stream varies | Treat it as failing the lossless requirement for this case | That the original is still retained |
| The file size has not decreased | Record the result as it is | That every PDF will be compressed successfully |
| Text read-back is consistent | Complete the inspection | That complex rendering needs no further comparison |
Complex rendering still needs a visual comparison with the original. Text read-back alone does not show how the page looks.
Use this checklist for document translation
File splitting, encryption and attachment processing are part of delivery preparation. Translation does not replace these checks.
- Keep the originals and your processing checklist.
- Run lossless compression only if you need a smaller file and want to keep the content intact.
- Avoid
remove_images()unless images are truly not needed. - Check the output against the decoded-bytes test and your visual comparison.
- Recheck the translated document for page order, hyperlinks and content integrity.
OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation.
Further reading: pypdf file size guide
📊 At a glance
| Approach | Changes images? | Reversible? | Suitable for translation prep? |
|---|---|---|---|
| Lossless content-stream compression | No, pixel data stays unchanged | Decoded bytes match the extracted text | Yes, as a standardization step |
Image removal with remove_images() | Yes, images are deleted | No | Only if images are not needed |
| JPEG-style lossy re-encoding | Yes, image data is discarded | Not restorable from the output | Not covered by this guide's checks |
🎯 Which approach fits you?
If you need a smaller file without changing the content
Use lossless content-stream compression. Confirm that the decoded bytes match the extracted text, then compare the rendered pages with the original.
If you only need the text
Use remove_images(), but only after confirming your target use does not need images. Keep the original in case you need the images later.
If your document contains pictures or diagrams you need for translation
Do not remove images. Use lossless compression if you want a smaller file, and check the output carefully.
If the file is important or sensitive
Keep the original copy, use test files first, and verify every result against the checklist before you rely on it.
❓ FAQ
1. Does lossless compression change the text or images in my PDF?
No. The content stream is compressed, while the text vector paths, font embeddings and original image pixel data remain unchanged.
2. Will compressing the content stream always make my PDF smaller?
No. The method does not guarantee a smaller file for every PDF, so check the size after processing.
3. Can I use remove_images() to shrink a PDF before translation?
Only if the images are not needed, such as for plain text extraction. It deletes the images completely, and that cannot be undone.
4. Is lossless compression the same as JPEG compression?
No. JPEG's re-encoding is lossy, while lossless stream compression keeps the decoded bytes identical to the extracted text.
📝 Summary
Compress content streams losslessly when you need a smaller file without changing your text or images. Avoid removing images unless your task does not need them, and always keep the originals and verify the output.
Try it now: upload a document and get it translated with the layout intact


