PyPDF2如何读取PDF页面的正确大小

huangapple go评论61阅读模式
英文:

How can PyPDF2 read the correct size of a PDF page

问题

我尝试使用PyPDF2获取PDF的宽度和高度:

w, h = page.mediaBox.getWidth(), page.mediaBox.getHeight()
print(w, h) # 显示 595 842

然而,实际上PDF的尺寸是 842 X 595(11.7 X 8.27 英寸)。

英文:

I tried to get width and height of pdf with PyPDF2 with

w, h = page.mediaBox.getWidth(), page.mediaBox.getHeight()
print(w, h) # showing 595 842

However, the pdf is actually 842 X 595(11.7 X 8.27 inch)
PyPDF2如何读取PDF页面的正确大小

答案1

得分: 1

A PDF page can have a "viewing rotation" which causes the PDF viewer to rotate the page before showing it.

The viewing rotation for this page is 90 degrees or 270 degrees. Use PyPDF2 to find out the rotation, and swap the width and height if the rotation is 90 or 270.

You can normalise the pages to remove the viewing rotations whilst leaving the PDF visually intact with:

cpdf in.pdf -upright -o out.pdf

英文:

A PDF page can have a "viewing rotation" which causes the PDF viewer to rotate the page before showing it.

The viewing rotation for this page is 90 degrees or 270 degrees. Use PyPDF2 to find out the rotation, and swap the width and height if the rotation is 90 or 270.

You can normalise the pages to remove the viewing rotations whilst leaving the PDF visually intact with:

cpdf in.pdf -upright -o out.pdf

huangapple
  • 本文由 发表于 2023年2月16日 06:58:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定