From efad31dac17c3462b328a390f6c6b66d52a371b6 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 26 Jun 2023 20:42:10 -0400 Subject: [PATCH] Utilities: Add a `pdf` utility This utility will learn tricks such as extracting images from PDFs and dumping tables from PDFs so that we can create code from specs. It also allows testing LibPDF things in lagom, and allows testing reading large amounts of PDFs using a shell script. --- Meta/Lagom/CMakeLists.txt | 3 ++ Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/pdf.cpp | 47 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 Userland/Utilities/pdf.cpp diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index b3261e85a5..9894b5d327 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -555,6 +555,9 @@ if (BUILD_LAGOM) target_link_libraries(ntpquery LibCore LibMain) endif() + add_executable(pdf ../../Userland/Utilities/pdf.cpp) + target_link_libraries(pdf LibCore LibPDF LibMain) + add_executable(sql ../../Userland/Utilities/sql.cpp) target_link_libraries(sql LibCore LibFileSystem LibIPC LibLine LibMain LibSQL) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index f6c490f644..73f89eacc3 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -123,6 +123,7 @@ target_link_libraries(notify PRIVATE LibGfx LibGUI) target_link_libraries(open PRIVATE LibDesktop LibFileSystem) target_link_libraries(passwd PRIVATE LibCrypt) target_link_libraries(paste PRIVATE LibGUI) +target_link_libraries(pdf PRIVATE LibPDF) target_link_libraries(pgrep PRIVATE LibRegex) target_link_libraries(pixelflut PRIVATE LibImageDecoderClient LibIPC LibGfx) target_link_libraries(pkill PRIVATE LibRegex) diff --git a/Userland/Utilities/pdf.cpp b/Userland/Utilities/pdf.cpp new file mode 100644 index 0000000000..b53cb7d668 --- /dev/null +++ b/Userland/Utilities/pdf.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023, Nico Weber + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +static PDF::PDFErrorOr pdf_main(Main::Arguments arguments) +{ + Core::ArgsParser args_parser; + + StringView in_path; + args_parser.add_positional_argument(in_path, "Path to input image file", "FILE"); + + args_parser.parse(arguments); + + auto file = TRY(Core::MappedFile::map(in_path)); + + auto document = TRY(PDF::Document::create(file->bytes())); + + if (auto handler = document->security_handler(); handler && !handler->has_user_password()) { + // FIXME: Add a flag for passing in a password and suggest it in this diag. + warnln("PDF requires password"); + return 1; + } + + TRY(document->initialize()); + + outln("{} pages", document->get_page_count()); + + return 0; +} + +ErrorOr serenity_main(Main::Arguments arguments) +{ + auto maybe_error = pdf_main(move(arguments)); + if (maybe_error.is_error()) { + auto error = maybe_error.release_error(); + warnln("{}", error.message()); + return 1; + } + return 0; +}