show_pdf_page overlays page on wrong bbox #2524
-
I am trying to overlay the page with only image with page with only text. Next function works well in many cases: import fitz
def overlay_pdfs(empty_path: str, only_text_path: str, merge_result_path: str):
with fitz.open(only_text_path) as doc_text:
with fitz.open(empty_path) as doc_background:
for i in range(doc_background.page_count):
page_back = doc_background.load_page(i)
rot = page_back.rotation
rect = page_back.rect
if rot % 180 != 0:
rect[2], rect[3] = rect[3], rect[2]
page_back.show_pdf_page(
rect, doc_text,
pno=i, keep_proportion=True,
overlay=True, oc=0,
rotate=rot,
clip=None
)
doc_background.ez_save(
merge_result_path,
encryption=fitz.PDF_ENCRYPT_KEEP,
deflate_fonts=True
) But for certain pair of docs with equal sizes it shifts and resizes text page part (show only a part cuz of privacy): I send this docs to [email protected] To reproduce: overlay_pdfs('only_image.pdf', 'only_text.pdf', 'result.pdf') Notes: I think the problem is related to image document wrong page rotation meta (270), but reset rotation will rotate the document instead of setting rotation angle to 0 in metadata |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I just responded via e-mail.
|
Beta Was this translation helpful? Give feedback.
-
Thank u for your response! Everything works well, so finally I use this implementation: def overlay_pdfs(empty_path: str, only_text_path: str, merge_result_path: str):
with fitz.open(only_text_path) as doc_text:
with fitz.open(empty_path) as doc_background:
doc = fitz.open()
for i in range(doc_background.page_count):
page_back = doc_background.load_page(i)
rot = page_back.rotation
rect = page_back.rect
w, h = rect.br
page_back.set_rotation(0)
new_page = doc.new_page(width=w, height=h)
new_page.show_pdf_page(new_page.rect, doc_background, new_page.number, rotate=-rot)
new_page.show_pdf_page(new_page.rect, doc_text, new_page.number, rotate=0)
doc.ez_save(
merge_result_path,
encryption=fitz.PDF_ENCRYPT_KEEP,
deflate_fonts=True,
pretty=True
) |
Beta Was this translation helpful? Give feedback.
Thank u for your response! Everything works well, so finally I use this implementation: