(
*,
log_level: str,
pdf_path: str,
interactive: str,
output_dir: Path,
display_text: bool,
log_text: bool,
enforce_same_font: bool,
page_boundary: str = "crop_box", # media_box
category: str = "char", # "both", "sanitized", "original"
page_num: int = -1,
password: str | None = None,
)
| 138 | |
| 139 | |
| 140 | def visualise_py( |
| 141 | *, |
| 142 | log_level: str, |
| 143 | pdf_path: str, |
| 144 | interactive: str, |
| 145 | output_dir: Path, |
| 146 | display_text: bool, |
| 147 | log_text: bool, |
| 148 | enforce_same_font: bool, |
| 149 | page_boundary: str = "crop_box", # media_box |
| 150 | category: str = "char", # "both", "sanitized", "original" |
| 151 | page_num: int = -1, |
| 152 | password: str | None = None, |
| 153 | ): |
| 154 | parser = DoclingPdfParser(loglevel=log_level) |
| 155 | |
| 156 | pdf_doc: PdfDocument = parser.load( |
| 157 | path_or_stream=pdf_path, |
| 158 | lazy=True, |
| 159 | password=password, |
| 160 | boundary_type=PdfPageBoundaryType(page_boundary), |
| 161 | decode_config=DecodeConfig(enforce_same_font=enforce_same_font), |
| 162 | ) |
| 163 | |
| 164 | page_nos = [page_num] |
| 165 | if page_num == -1: |
| 166 | page_nos = [(page_ind + 1) for page_ind in range(pdf_doc.number_of_pages())] |
| 167 | |
| 168 | for page_no in page_nos: |
| 169 | print(f"parsing {pdf_path} on page: {page_no}") |
| 170 | |
| 171 | pdf_page: SegmentedPdfPage = pdf_doc.get_page(page_no=page_no) |
| 172 | |
| 173 | if os.path.exists(str(output_dir)): |
| 174 | pdf_page.save_as_json( |
| 175 | Path(f"{output_dir}/{os.path.basename(pdf_path)}.page_{page_no}.json") |
| 176 | ) |
| 177 | |
| 178 | if category in ["all", "char"]: |
| 179 | img = pdf_page.render_as_image( |
| 180 | cell_unit=TextCellUnit.CHAR, |
| 181 | draw_cells_bbox=(not display_text), |
| 182 | draw_cells_text=display_text, |
| 183 | ) |
| 184 | |
| 185 | if os.path.exists(str(output_dir)): |
| 186 | img.save( |
| 187 | f"{output_dir}/{os.path.basename(pdf_path)}.page_{page_no}.char.png" |
| 188 | ) |
| 189 | |
| 190 | if interactive: |
| 191 | img.show() |
| 192 | |
| 193 | if log_text: |
| 194 | lines = pdf_page.export_to_textlines( |
| 195 | cell_unit=TextCellUnit.CHAR, |
| 196 | add_fontkey=True, |
| 197 | add_fontname=False, |
no test coverage detected