Browse Source

fix cropbox

Byaidu 1 year ago
parent
commit
eb700c4a5b
3 changed files with 9 additions and 6 deletions
  1. 1 1
      pdf2zh/__init__.py
  2. 4 3
      pdf2zh/converter.py
  3. 4 2
      pdf2zh/pdfinterp.py

+ 1 - 1
pdf2zh/__init__.py

@@ -1,2 +1,2 @@
-__version__ = "1.4.6"
+__version__ = "1.4.7"
 __author__ = "Byaidu"

+ 4 - 3
pdf2zh/converter.py

@@ -86,7 +86,8 @@ class PDFLayoutAnalyzer(PDFTextDevice):
         self._stack: List[LTLayoutContainer] = []
 
     def begin_page(self, page: PDFPage, ctm: Matrix) -> None:
-        (x0, y0, x1, y1) = page.mediabox
+        # (x0, y0, x1, y1) = page.mediabox
+        (x0, y0, x1, y1) = page.cropbox
         (x0, y0) = apply_matrix_pt(ctm, (x0, y0))
         (x1, y1) = apply_matrix_pt(ctm, (x1, y1))
         mediabox = (0, 0, abs(x0 - x1), abs(y0 - y1))
@@ -407,8 +408,8 @@ class TextConverter(PDFConverter[AnyIO]):
             while ptr<len(item): # 识别文字和公式
                 child=item[ptr]
                 if isinstance(child, LTChar):
-                    cur_v=False
-                    ind_v=False
+                    cur_v=False # 公式
+                    ind_v=False # 独立公式
                     fontname=child.fontname.split('+')[-1]
                     if vflag(fontname,child.get_text()): # 识别公式和字符
                         cur_v=True

+ 4 - 2
pdf2zh/pdfinterp.py

@@ -976,7 +976,9 @@ class PDFPageInterpreter:
 
     def process_page(self, page: PDFPage) -> None:
         # log.debug("Processing page: %r", page)
-        (x0, y0, x1, y1) = page.mediabox
+        # print(page.mediabox,page.cropbox)
+        # (x0, y0, x1, y1) = page.mediabox
+        (x0, y0, x1, y1) = page.cropbox
         if page.rotate == 90:
             ctm = (0, -1, 1, 0, -y0, x1)
         elif page.rotate == 180:
@@ -990,7 +992,7 @@ class PDFPageInterpreter:
         self.device.fontmap=self.fontmap # hack
         ops_new=self.device.end_page(page)
         page_objids=[i.objid for i in page.contents]
-        # 上面渲染的时候会根据 mediabox 减掉页面偏移得到真实坐标,这里输出的时候需要用 cm 把页面偏移加回来
+        # 上面渲染的时候会根据 cropbox 减掉页面偏移得到真实坐标,这里输出的时候需要用 cm 把页面偏移加回来
         ops_full=f'{page_objids[0]} 0 obj\n<<>>stream\nq {ops_base}Q 1 0 0 1 {x0} {y0} cm {ops_new}\nendstream\nendobj\n' # ops_base 里可能有图,需要让 ops_new 里的文字覆盖在上面,使用 q/Q 重置位置矩阵
         # if log.isEnabledFor(logging.DEBUG):
         #     log.debug(f'OP_BASE {ops_base}')