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

Failure when set cropbox with mediabox negative value #2736

Closed
djs45 opened this issue Oct 11, 2023 · 2 comments
Closed

Failure when set cropbox with mediabox negative value #2736

djs45 opened this issue Oct 11, 2023 · 2 comments
Labels

Comments

@djs45
Copy link

djs45 commented Oct 11, 2023

Problem with setcropbox when mediabox have negative value

I want to set cropbox of a pdf file where mediabox is :

fitz page mediabox : Rect(-106.673, -362.691, 762.869, 1305.062)
Want To CropBox set To : -25.54 -294.47 415.66 971.34

I've got this error : ValueError: rect not in mediabox

  • Windows 10
  • Python version 3.10.4 64 bits
  • last PyMuPDF version

I send you complete example python and pdf file

prblem_crop_pymupdf.zip

@JorjMcKie JorjMcKie added the bug label Oct 17, 2023
julian-smith-artifex-com added a commit to ArtifexSoftware/PyMuPDF-julian that referenced this issue Nov 17, 2023
@JorjMcKie
Copy link
Collaborator

There is a wrong check for rectangle containment - which will be corrected.

You can already now use the low-level API as a circumvention and do this doc.xref_set_key(page.xref, "CropBox", "[x0 y1 x1 y1]").

Please note that here you must use PDF geometry for the coordinates, which means point (x0, y0) specifies the lower-left and (x1, y1) the upper-right corner. Also observe that the desired value is contained in a string and must be provided exactly as shown. There also is no plausibility check, so this is on your own risk.

After the fix, a session changing the CropBox (and friends) may look like this:

import fitz
doc=fitz.open()
page=doc.new_page()
page.xref
4
# fake a MediaBox for demo purposes
doc.xref_set_key(4, "MediaBox", "[-30 -20 595 842]")

page.cropbox  # default: MediaBox, but in MuPDF geometry!
Rect(-30.0, 0.0, 595.0, 862.0)

page.rect  # equals th (shifted) CropBox : top-left = (0, 0)
Rect(0.0, 0.0, 625.0, 862.0)

# change the CropBox: shift by (10, 10) in both dimensions. Please note:
# To achieve this, 10 must be subtracted from 862! yo must never be negative!
page.set_cropbox(fitz.Rect(-20.0, 0.0, 595.0, 852.0))

# Confirm how page object looks like.
# Note the `-10` in the y0 coordinate.
print(doc.xref_object(4))
<<
  /Type /Page
  /MediaBox [ -30 -20 595 842 ]
  /Rotate 0
  /Resources 3 0 R
  /Parent 2 0 R
  /CropBox [ -20 -10 595 842 ]
>>
page.rect  # and also look at the page rectangle
Rect(0.0, 0.0, 615.0, 852.0)

@JorjMcKie JorjMcKie added the fix developed release schedule to be determined label Nov 19, 2023
JorjMcKie added a commit that referenced this issue Nov 19, 2023
Correctly handle PDF CropBox changes despite of a MediaBox with negative coordinates.
JorjMcKie added a commit that referenced this issue Nov 20, 2023
Correctly handle PDF CropBox changes despite of a MediaBox with negative coordinates.
@JorjMcKie JorjMcKie added Fixed in next release and removed fix developed release schedule to be determined labels Nov 21, 2023
@julian-smith-artifex-com
Copy link
Collaborator

Fixed in 1.23.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants