Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot open pdf file with Adobe Acrobat Reader after transformation #1527

Closed
musman920 opened this issue Jan 4, 2023 · 10 comments · Fixed by #1563
Closed

Cannot open pdf file with Adobe Acrobat Reader after transformation #1527

musman920 opened this issue Jan 4, 2023 · 10 comments · Fixed by #1563

Comments

@musman920
Copy link

I am trying to create a blank page. rotate and merge 1 pdf file on that page and append another page.
The functionality seems to work but after creating the new pdf file It does not open in adobe Acrobat reader.
It gives the error ''

Environment

Mac on intel chip
python 3.10
pypdf 3.2.0

Code + PDF

This is a minimal, complete example that shows the issue from your answer of stackoverflow:
(My code also includes Transformation)

from pypdf import PdfReader, PdfWriter, Transformation
from pypdf.generic import RectangleObject

reader = PdfReader("dhl.pdf")
writer = PdfWriter()


desired_width = 100
desired_height = 100
r = RectangleObject([0, 0, desired_width, desired_height])

for page in reader.pages[:10]:
    old_width = page.mediabox.width
    old_height = page.mediabox.height

    a1 = desired_width / old_width
    a2 = desired_height / old_height
    factor = min(a1, a2)

    new_width = float(old_width * factor)
    new_height = float(old_height * factor)

    dx = (desired_width - new_width) / 2
    dy = (desired_height - new_height) / 2
    op = Transformation().translate(tx=dx, ty=dy)

    page.scale_to(width=new_width, height=new_height)
    page.add_transformation(op)
    page.mediabox = r
    page.artbox = r
    page.cropbox = r
    page.bleedbox = r
    page.trimbox = r
    writer.add_page(page)

with open("foo.pdf", "wb") as fp:
    writer.write(fp)

Traceback

This is the complete Traceback I see when opening with acrobat reader
dhl.pdf
foo.pdf
:

An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.

@MartinThoma
Copy link
Member

Interesting. I can open those files just fine. Can you confirm that you can open it e.g. with the Google chrome PDF viewer?

@musman920
Copy link
Author

thank you for the response.
it is opening in browser and xpdf viewer.
Only problem is opening in adobe acrobat reader.
Our organization has multiple computers and we have to deal with pdf alot. The standard is Adobe Acrobat Reader and It should be open in Acrobat Reader.
it says "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.".
What could be the issue here?

@MartinThoma
Copy link
Member

MartinThoma commented Jan 4, 2023

I have no idea.

A first thing to check is if the PDFs are actually non-standard compliant. will do that after work.

Which version of Adobe Acrobat Reader do you use? (If it's not the latest, please upgrade + check again)

It could also be a bug in Acorbat Reader

@musman920
Copy link
Author

musman920 commented Jan 4, 2023

That would be greate if you can check.
I have the latest acrobat reader and tested on multiple systems.
The result is the same error on all systems.
This is the sample generated pdf with test data.

@MartinThoma
Copy link
Member

I have the latest acrobat reader and tested on multiple systems.

Which version do you have?

@musman920
Copy link
Author

musman920 commented Jan 4, 2023

adobe Acrobat Reader Version 2022.003.20281

Checked for updates again, it states it is already updated.

gruße

@MartinThoma
Copy link
Member

VeraPDF fails, but 3heights doesn't.

That means the PDF is not fully standard compliant, but not so badly broken that it should matter.

Which means that I don't know how to continue

@MartinThoma
Copy link
Member

image

@mrknwk
Copy link

mrknwk commented Jan 4, 2023

The problem is the excessive intrinsic precision addressed in #1376, I guess.

@musman920
Copy link
Author

this indeed fixes the issue :) Cheers !
Just need to modify
pypdf/generic/_base.py#L353
return f"{self:f}".rstrip("0") ->
return f"{self:.19f}".rstrip("0")
Now acrobat reader can open the transformed file.

MartinThoma pushed a commit that referenced this issue Feb 4, 2023
Decimal was replaced by float in order to fix bugs.

It might also improve speed in some cases.
It is a preparation for #1567

Fixes #1527
Fixes #1376
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 a pull request may close this issue.

3 participants