From 52ff180ed45cf78175d746264e1fe0066d9d17e3 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 12 Jul 2023 09:53:41 -0400 Subject: [PATCH] pdf: Add --dump-contents flag This dumps the /Content stream of a page, which is possibly useful for debugging. --- Userland/Utilities/pdf.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Userland/Utilities/pdf.cpp b/Userland/Utilities/pdf.cpp index 83d8dc8f20..73fbe29395 100644 --- a/Userland/Utilities/pdf.cpp +++ b/Userland/Utilities/pdf.cpp @@ -85,6 +85,9 @@ static PDF::PDFErrorOr pdf_main(Main::Arguments arguments) StringView in_path; args_parser.add_positional_argument(in_path, "Path to input image file", "FILE"); + bool dump_contents = false; + args_parser.add_option(dump_contents, "Dump page contents", "dump-contents", {}); + u32 page_number = 1; args_parser.add_option(page_number, "Page number (1-based)", "page", {}, "PAGE"); @@ -114,6 +117,20 @@ static PDF::PDFErrorOr pdf_main(Main::Arguments arguments) warnln("--page {} out of bounds, must be between 1 and {}", page_number, document->get_page_count()); return 1; } + int page_index = page_number - 1; + + if (dump_contents) { + auto page = TRY(document->get_page(page_index)); + auto contents = TRY(page.page_contents(*document)); + for (u8 c : contents.bytes()) { + if (c < 128) + out("{:c}", c); + else + out("\\{:03o}", c); + } + + return 0; + } if (!render_path.is_empty()) { #if !defined(AK_OS_SERENITY) @@ -122,7 +139,7 @@ static PDF::PDFErrorOr pdf_main(Main::Arguments arguments) Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/Base/res/fonts", source_root)); #endif - TRY(save_rendered_page(document, page_number - 1, render_path)); + TRY(save_rendered_page(document, page_index, render_path)); return 0; }