diff --git a/docling_parse/visualize.py b/docling_parse/visualize.py index 972893f..a258c28 100644 --- a/docling_parse/visualize.py +++ b/docling_parse/visualize.py @@ -53,6 +53,13 @@ def parse_args(): help="Enable interactive mode (default: False)", ) + # Add an optional boolean argument for interactive mode + parser.add_argument( + "--display-text", + action="store_true", + help="Enable interactive mode (default: False)", + ) + # Add an argument for the output directory, defaulting to "./tmp" parser.add_argument( "-o", @@ -91,11 +98,17 @@ def parse_args(): args.interactive, args.output_dir, int(args.page), + args.display_text, ) def visualise_v1( - log_level: str, pdf_path: str, interactive: str, output_dir: str, page_num: int + log_level: str, + pdf_path: str, + interactive: str, + output_dir: str, + page_num: int, + display_text: bool, ): parser = pdf_parser_v1() @@ -200,7 +213,12 @@ def visualise_v1( def visualise_v2( - log_level: str, pdf_path: str, interactive: str, output_dir: str, page_num: int + log_level: str, + pdf_path: str, + interactive: str, + output_dir: str, + page_num: int, + display_text: bool, ): parser = pdf_parser_v2(log_level) @@ -214,10 +232,17 @@ def visualise_v2( doc = None - if page_num == -1: - doc = parser.parse_pdf_from_key(doc_key) - else: - doc = parser.parse_pdf_from_key_on_page(doc_key, page_num) + try: + if page_num == -1: + doc = parser.parse_pdf_from_key(doc_key) + else: + doc = parser.parse_pdf_from_key_on_page(doc_key, page_num) + except Exception as exc: + print(f"Could not parse pdf-document: {exc}") + doc = None + + if doc == None: + return parser.unload_document(doc_key) @@ -295,6 +320,9 @@ def visualise_v2( (x[3], H - y[3]), ] + if display_text: + print(row[cells_header.index("text")]) + if "glyph" in row[cells_header.index("text")]: print(f" skip cell -> {row}") continue @@ -328,12 +356,12 @@ def visualise_v2( def main(): - log_level, version, pdf, interactive, output_dir, page = parse_args() + log_level, version, pdf, interactive, output_dir, page, display_text = parse_args() if version == "v1": - visualise_v1(log_level, pdf, interactive, output_dir, page) + visualise_v1(log_level, pdf, interactive, output_dir, page, display_text) elif version == "v2": - visualise_v2(log_level, pdf, interactive, output_dir, page) + visualise_v2(log_level, pdf, interactive, output_dir, page, display_text) else: return -1 diff --git a/src/v2/enums.h b/src/v2/enums.h index ccbe07b..06a4770 100644 --- a/src/v2/enums.h +++ b/src/v2/enums.h @@ -36,7 +36,7 @@ namespace pdflib else if (name=="CID_FONT_TYPE_2" or name=="/CIDFontType2") { return CID_FONT_TYPE_2; } else { - LOG_S(FATAL) << "unknown subtype " << name; + LOG_S(ERROR) << "unknown subtype " << name; return NULL_TYPE; } } @@ -59,7 +59,7 @@ namespace pdflib default: { - LOG_S(FATAL) << "encountered a NULL_ENCODING"; + LOG_S(ERROR) << "encountered a NULL_ENCODING"; return "NULL_ENCODING"; } } @@ -90,7 +90,7 @@ namespace pdflib else if(name=="CMAP_RESOURCES" ) { return CMAP_RESOURCES; } else { - LOG_S(FATAL) << __FILE__ << ":" << __LINE__ << " --> unknown encoding " << name; + LOG_S(ERROR) << __FILE__ << ":" << __LINE__ << " --> unknown encoding " << name; return NULL_ENCODING; } } @@ -109,7 +109,7 @@ namespace pdflib default: { - LOG_S(FATAL) << "encountered a NULL_ENCODING"; + LOG_S(ERROR) << "encountered a NULL_ENCODING"; return "NULL_ENCODING"; } } diff --git a/src/v2/pdf_decoders/stream.h b/src/v2/pdf_decoders/stream.h index 2313116..251bc5c 100644 --- a/src/v2/pdf_decoders/stream.h +++ b/src/v2/pdf_decoders/stream.h @@ -253,7 +253,11 @@ namespace pdflib { if(stack.size()==0) { - LOG_S(FATAL) << "stack-size is zero!"; + std::stringstream message; + message << "stack-size is zero in " << __FILE__ << ":" << __LINE__; + + LOG_S(ERROR) << message.str(); + throw std::logic_error(message.str()); } pdf_state& state = stack.back(); diff --git a/src/v2/pdf_resources/page_dimension.h b/src/v2/pdf_resources/page_dimension.h index c94b0f5..e4ea27b 100644 --- a/src/v2/pdf_resources/page_dimension.h +++ b/src/v2/pdf_resources/page_dimension.h @@ -110,7 +110,7 @@ namespace pdflib } else { - LOG_S(FATAL) << "The page is missing the required '/MediaBox'"; + LOG_S(ERROR) << "The page is missing the required '/MediaBox'"; } if(json_resources.count("/CropBox")) @@ -173,8 +173,12 @@ namespace pdflib } else { - LOG_S(FATAL) << "could not find the page-dimensions: " - << json_resources.dump(4); + std::stringstream ss; + ss << "could not find the page-dimensions: " + << json_resources.dump(4); + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } } diff --git a/src/v2/pdf_resources/page_font.h b/src/v2/pdf_resources/page_font.h index 597f505..97236cd 100644 --- a/src/v2/pdf_resources/page_font.h +++ b/src/v2/pdf_resources/page_font.h @@ -180,8 +180,9 @@ namespace pdflib } else { - LOG_S(FATAL) << "no existing pdf_resources_dir: " - << pdf_resources_dir; + std::string message = "no existing pdf_resources_dir: " + pdf_resources_dir; + LOG_S(ERROR) << message; + throw std::logic_error(message); } utils::timer timer; @@ -1148,7 +1149,11 @@ namespace pdflib } else { - LOG_S(FATAL) << "unknown type in " << __FUNCTION__; + std::stringstream message; + message << "unknown type in " << __FUNCTION__; + + LOG_S(ERROR) << message.str(); + throw std::logic_error(message.str()); } } } @@ -1166,7 +1171,12 @@ namespace pdflib if(not qpdf_font.hasKey("/ToUnicode")) { auto tmp = to_json(qpdf_font); - LOG_S(FATAL) << "qpdf-font: " << tmp.dump(); + + std::stringstream ss; + ss << "qpdf-font: " << tmp.dump(); + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } auto qpdf_obj = qpdf_font.getKey("/ToUnicode"); diff --git a/src/v2/pdf_resources/page_font/base_font.h b/src/v2/pdf_resources/page_font/base_font.h index 4f98d02..28a0cd4 100644 --- a/src/v2/pdf_resources/page_font/base_font.h +++ b/src/v2/pdf_resources/page_font/base_font.h @@ -157,9 +157,15 @@ namespace pdflib return bbox[3]; } - LOG_S(FATAL) << "properties does not have key 'Ascender': " - << properties.dump(2); - + { + std::stringstream ss; + ss << "properties does not have key 'Ascender': " + << properties.dump(2); + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); + } + return -1.; } @@ -179,9 +185,15 @@ namespace pdflib return bbox[1]; } - LOG_S(FATAL) << "properties does not have key 'Descender': " - << properties.dump(2); + { + std::stringstream ss; + ss << "properties does not have key 'Descender': " + << properties.dump(2); + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); + } + return -1.; } @@ -194,9 +206,15 @@ namespace pdflib return properties["FontBBox"].get >(); } - LOG_S(FATAL) << "properties does not have key 'FontBBox': " - << properties.dump(2); - + { + std::stringstream ss; + ss << "properties does not have key 'FontBBox': " + << properties.dump(2); + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); + } + return {0.0, 0.0, 0.0, 0.0}; } diff --git a/src/v2/pdf_resources/page_font/base_fonts.h b/src/v2/pdf_resources/page_font/base_fonts.h index a05c33f..8536751 100644 --- a/src/v2/pdf_resources/page_font/base_fonts.h +++ b/src/v2/pdf_resources/page_font/base_fonts.h @@ -121,8 +121,6 @@ namespace pdflib { if(norm_name.find(itr->first)!=std::string::npos) { - //return itr->first; - // we have to be careful that "Helvetica" is not returned for Helvetice-Bold! if(result.size()<(itr->first).size()) { @@ -136,8 +134,8 @@ namespace pdflib return result; } - LOG_S(FATAL) << "unkown " << font_name << "[norm_name=" << norm_name << "]"; - + LOG_S(ERROR) << "unkown " << font_name << "[norm_name=" << norm_name << "]"; + return "Unknown"; } @@ -269,7 +267,11 @@ namespace pdflib if(fontname=="unknown") { - LOG_S(FATAL) << "no FontName found in " << filename; + std::stringstream ss; + ss << "no FontName found in " << filename; + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } return fontname; diff --git a/src/v2/pdf_resources/page_font/cmap.h b/src/v2/pdf_resources/page_font/cmap.h index 6aba203..4da953f 100644 --- a/src/v2/pdf_resources/page_font/cmap.h +++ b/src/v2/pdf_resources/page_font/cmap.h @@ -434,7 +434,7 @@ namespace pdflib if(_map.count(begin+i)==1) { - LOG_S(FATAL) << "overwriting number c=" << begin+i; + LOG_S(WARNING) << "overwriting number c=" << begin+i; } _map[begin + i] = tmp; diff --git a/src/v2/pdf_resources/page_font/font_cid.h b/src/v2/pdf_resources/page_font/font_cid.h index d7d645d..192810c 100644 --- a/src/v2/pdf_resources/page_font/font_cid.h +++ b/src/v2/pdf_resources/page_font/font_cid.h @@ -87,7 +87,8 @@ namespace pdflib if(file.fail()) { LOG_S(ERROR) << "filename does not exists: " << filename; - LOG_S(FATAL) << "unknown data-file!"; + + } bool cmap=false; @@ -143,8 +144,11 @@ namespace pdflib if(file.fail()) { - LOG_S(ERROR) << "filename does not exists: " << filename; - LOG_S(FATAL) << "unknown data-file!"; + std::stringstream ss; + ss << "filename does not exists: " << filename; + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } std::vector col_inds = {}; @@ -246,7 +250,7 @@ namespace pdflib } else { - LOG_S(FATAL) << "we should never arrive here!"; + LOG_S(ERROR) << "all options exhausted for " << __FUNCTION__; } } diff --git a/src/v2/pdf_resources/page_font/glyphs.h b/src/v2/pdf_resources/page_font/glyphs.h index a360dfa..33c81fb 100644 --- a/src/v2/pdf_resources/page_font/glyphs.h +++ b/src/v2/pdf_resources/page_font/glyphs.h @@ -152,8 +152,11 @@ namespace pdflib if(file.fail()) { - LOG_S(ERROR) << "filename does not exists: " << filename; - LOG_S(FATAL) << "unknown data-file!"; + std::stringstream ss; + ss << "filename does not exists: " << filename; + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } std::string line; @@ -218,8 +221,11 @@ namespace pdflib if(file.fail()) { - LOG_S(ERROR) << "filename does not exists: " << filename; - LOG_S(FATAL) << "unknown data-file!"; + std::stringstream ss; + ss << "filename does not exists: " << filename; + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } std::string line; diff --git a/src/v2/pdf_resources/page_fonts.h b/src/v2/pdf_resources/page_fonts.h index 4d66a0b..f88dbe5 100644 --- a/src/v2/pdf_resources/page_fonts.h +++ b/src/v2/pdf_resources/page_fonts.h @@ -77,15 +77,25 @@ namespace pdflib { if(page_fonts.count(font_name)==1) { - return page_fonts[font_name]; + return page_fonts.at(font_name); } else { std::stringstream ss; + ss << "font_name [" << font_name << "] is not known: "; for(auto itr=page_fonts.begin(); itr!=page_fonts.end(); itr++) - ss << itr->first << ", "; + { + if(itr==page_fonts.begin()) + { + ss << itr->first; + } + else + { + ss << ", " << itr->first; + } + } - LOG_S(FATAL) << "font_name [" << font_name << "] is not known: " << ss.str(); + throw std::logic_error(ss.str()); } return (page_fonts.begin()->second); @@ -102,17 +112,24 @@ namespace pdflib nlohmann::json& val = pair.value(); LOG_S(INFO) << "decoding font: " << key;// << "\n" << val.dump(2); - assert(qpdf_fonts.hasKey(key)); + //assert(qpdf_fonts.hasKey(key)); - pdf_resource page_font; - page_font.set(key, val, qpdf_fonts.getKey(key)); - - if(page_fonts.count(key)==1) - { - LOG_S(FATAL) << "We are overwriting a font!! BE CAREFUL!"; - } - - page_fonts[key] = page_font; + if(qpdf_fonts.hasKey(key)) + { + pdf_resource page_font; + page_font.set(key, val, qpdf_fonts.getKey(key)); + + if(page_fonts.count(key)==1) + { + LOG_S(ERROR) << "We are overwriting a font!"; + } + + page_fonts[key] = page_font; + } + else + { + LOG_S(ERROR) << "qpdf does not have key: " << key; + } } } diff --git a/src/v2/pdf_resources/page_grphs.h b/src/v2/pdf_resources/page_grphs.h index a20460f..873febc 100644 --- a/src/v2/pdf_resources/page_grphs.h +++ b/src/v2/pdf_resources/page_grphs.h @@ -83,9 +83,17 @@ namespace pdflib { std::stringstream ss; for(auto itr=page_grphs.begin(); itr!=page_grphs.end(); itr++) - ss << itr->first << ", "; + { + ss << itr->first << ", "; + } - LOG_S(FATAL) << "graphics state with name '" << grph_name << "' is not known: " << ss.str(); + { + std::stringstream ss; + ss << "graphics state with name '" << grph_name << "' is not known: " << ss.str(); + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); + } } return (page_grphs.begin()->second); @@ -109,7 +117,11 @@ namespace pdflib if(page_grphs.count(key)==1) { - LOG_S(FATAL) << "We are overwriting a grph!! BE CAREFUL!"; + std::stringstream ss; + ss << "we are overwriting a grph!"; + + LOG_S(ERROR) << ss.str(); + //throw std::logic_error(ss.str()); } page_grphs[key] = page_grph; diff --git a/src/v2/pdf_resources/page_xobject.h b/src/v2/pdf_resources/page_xobject.h index 7155d87..d035617 100644 --- a/src/v2/pdf_resources/page_xobject.h +++ b/src/v2/pdf_resources/page_xobject.h @@ -196,7 +196,11 @@ namespace pdflib } catch(const std::exception& exc) { - LOG_S(FATAL) << "encountered an error: " << exc.what(); + std::stringstream ss; + ss << "encountered an error: " << exc.what(); + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } return stream; diff --git a/src/v2/qpdf/instruction.h b/src/v2/qpdf/instruction.h index 7750f21..4461ebd 100644 --- a/src/v2/qpdf/instruction.h +++ b/src/v2/qpdf/instruction.h @@ -86,9 +86,13 @@ namespace pdflib return obj.getStringValue(); } else - { - LOG_S(FATAL) << "can not decode a string value for key: " << key - << " and value: " << val; + { + std::stringstream ss; + ss << "can not decode a string value for key: " << key + << " and value: " << val; + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } return "null"; @@ -105,9 +109,13 @@ namespace pdflib return obj.getUTF8Value(); } else - { - LOG_S(FATAL) << "can not decode a string value for key: " << key - << " and value: " << val; + { + std::stringstream ss; + ss << "can not decode a string value for key: " << key + << " and value: " << val; + + LOG_S(ERROR) << ss.str(); + throw std::logic_error(ss.str()); } return "null";