Pymupdf: Page.showPDFpage error

Created on 12 Nov 2020  路  4Comments  路  Source: pymupdf/PyMuPDF

_Please provide all mandatory information!_

Describe the bug (mandatory)

I use Page.showPDFpage copy page from another pdf,but new page display not same with old page from a special pdf document. Maybe it's page size is too large (width: 42cm height:29.7cm)

To Reproduce (mandatory)

    old_pdf = fitz.open('./temp/old.pdf')
    new_pdf = fitz.open()
    for old_page in old_pdf.pages():
        new_page = new_pdf.newPage(
            old_page.number,
            old_page.rect.width,
            old_page.rect.height
        )
        new_page.showPDFpage(old_page.rect, old_pdf, old_page.number)
    new_pdf.save('new.pdf')

old.pdf
new.pdf

Expected behavior (optional)

other pdf not found this problem

Screenshots (optional)

old_page
new_page

Your configuration (mandatory)

  • win10
  • python 3.7 64bit
  • PyMuPDF 1.18.3, installation method (pip install PyMuPDF).

For example, the output of print(sys.version, "\n", sys.platform, "\n", fitz.__doc__) would be sufficient (for the first two bullets).

3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
 win32

PyMuPDF 1.18.3: Python bindings for the MuPDF 1.18.0 library.
Version date: 2020-11-09 07:36:17.
Built for Python 3.7 on win32 (64-bit).

Additional context (optional)

Add any other context about the problem here.

bug resolved

Most helpful comment

Yes your script works fine. it solve my problem,thank you very much! 馃憤

All 4 comments

The problem here is that the input page is _rotated_.
So you should either rotate new_page in the same way, or specify the rotation parameter in showPDFpage.
Presumably rotate=90 in this case.
You definitely must use showPDFpage(new_page.rect, ..., because the old one is rotated.

To be on the safe side:

  1. set old page rotation to 0 first
  2. create new_page and do showPDFpage
  3. reset old_page rotation back to old value.
  4. (optional) set rotation of new_page to that of old_page

This script works fine:

import fitz

src = fitz.open("old.pdf")
spage = src[0]
oldrot = spage.rotation
spage.setRotation(0)
doc = fitz.open()
npage = doc.newPage(width=spage.rect.width, height=spage.rect.height)
npage.showPDFpage(
    npage.rect,
    src,
    0,
)
npage.setRotation(oldrot)
doc.save("new.pdf")

Yes your script works fine. it solve my problem,thank you very much! 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

akjanik picture akjanik  路  3Comments

cherryjo18 picture cherryjo18  路  3Comments

tanaskumar picture tanaskumar  路  3Comments

Gabriellavoura picture Gabriellavoura  路  4Comments

axlgit picture axlgit  路  3Comments