2023-06-26 20:42:10 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <LibCore/MappedFile.h>
|
2023-07-10 09:00:30 -04:00
|
|
|
#include <LibPDF/CommonNames.h>
|
2023-06-26 20:42:10 -04:00
|
|
|
#include <LibPDF/Document.h>
|
|
|
|
|
2023-07-10 09:00:30 -04:00
|
|
|
static PDF::PDFErrorOr<void> print_document_info_dict(PDF::Document& document)
|
|
|
|
{
|
|
|
|
if (auto info_dict = TRY(document.info_dict()); info_dict.has_value()) {
|
|
|
|
if (auto title = TRY(info_dict->title()); title.has_value())
|
|
|
|
outln("Title: {}", title);
|
|
|
|
if (auto author = TRY(info_dict->author()); author.has_value())
|
|
|
|
outln("Author: {}", author);
|
|
|
|
if (auto subject = TRY(info_dict->subject()); subject.has_value())
|
|
|
|
outln("Subject: {}", subject);
|
|
|
|
if (auto keywords = TRY(info_dict->keywords()); keywords.has_value())
|
|
|
|
outln("Keywords: {}", keywords);
|
|
|
|
if (auto creator = TRY(info_dict->creator()); creator.has_value())
|
|
|
|
outln("Creator: {}", creator);
|
|
|
|
if (auto producer = TRY(info_dict->producer()); producer.has_value())
|
|
|
|
outln("Producer: {}", producer);
|
|
|
|
if (auto creation_date = TRY(info_dict->creation_date()); creation_date.has_value())
|
|
|
|
outln("Creation date: {}", creation_date);
|
|
|
|
if (auto modification_date = TRY(info_dict->modification_date()); modification_date.has_value())
|
|
|
|
outln("Modification date: {}", modification_date);
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-07-11 13:39:04 -04:00
|
|
|
static PDF::PDFErrorOr<void> print_document_info(PDF::Document& document)
|
|
|
|
{
|
|
|
|
outln("PDF Version: {}.{}", document.version().major, document.version().minor);
|
|
|
|
outln("Number of pages: {}", document.get_page_count());
|
|
|
|
TRY(print_document_info_dict(document));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-06-26 20:42:10 -04:00
|
|
|
static PDF::PDFErrorOr<int> pdf_main(Main::Arguments arguments)
|
|
|
|
{
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
2023-07-10 15:31:42 -04:00
|
|
|
StringView password;
|
|
|
|
args_parser.add_option(password, "Password for decrypting PDF, if needed", "password", {}, "PASS");
|
|
|
|
|
2023-06-26 20:42:10 -04:00
|
|
|
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()) {
|
2023-07-10 15:31:42 -04:00
|
|
|
if (password.is_empty()) {
|
|
|
|
warnln("PDF requires password, pass in using --password");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!document->security_handler()->try_provide_user_password(password)) {
|
|
|
|
warnln("invalid password '{}'", password);
|
|
|
|
return 1;
|
|
|
|
}
|
2023-06-26 20:42:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY(document->initialize());
|
|
|
|
|
2023-07-11 13:39:04 -04:00
|
|
|
TRY(print_document_info(*document));
|
2023-06-26 20:42:10 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<int> 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;
|
|
|
|
}
|