1 | /* |
2 | * Copyright (C) 2010, Pino Toscano <pino@kde.org> |
3 | * Copyright (C) 2017, 2019, Albert Astals Cid <aacid@kde.org> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2, or (at your option) |
8 | * any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include <poppler-document.h> |
21 | #include <poppler-image.h> |
22 | #include <poppler-page.h> |
23 | #include <poppler-page-renderer.h> |
24 | |
25 | #include <cstdlib> |
26 | #include <iostream> |
27 | #include <memory> |
28 | |
29 | #include "parseargs.h" |
30 | |
31 | bool show_help = false; |
32 | bool show_formats = false; |
33 | char out_filename[4096]; |
34 | int doc_page = 0; |
35 | |
36 | static const ArgDesc the_args[] = { { .arg: "-f" , .kind: argFlag, .val: &show_formats, .size: 0, .usage: "show supported output image formats" }, |
37 | { .arg: "--page" , .kind: argInt, .val: &doc_page, .size: 0, .usage: "select page to render" }, |
38 | { .arg: "-o" , .kind: argString, .val: &out_filename, .size: sizeof(out_filename), .usage: "output filename for the resulting PNG image" }, |
39 | { .arg: "-h" , .kind: argFlag, .val: &show_help, .size: 0, .usage: "print usage information" }, |
40 | { .arg: "--help" , .kind: argFlag, .val: &show_help, .size: 0, .usage: "print usage information" }, |
41 | { .arg: nullptr, .kind: argFlag, .val: nullptr, .size: 0, .usage: nullptr } }; |
42 | |
43 | static void error(const std::string &msg) |
44 | { |
45 | std::cerr << "Error: " << msg << std::endl; |
46 | std::cerr << "Exiting..." << std::endl; |
47 | exit(status: 1); |
48 | } |
49 | |
50 | int main(int argc, char *argv[]) |
51 | { |
52 | if (!parseArgs(args: the_args, argc: &argc, argv) || (argc < 2 && !show_formats) || show_help) { |
53 | printUsage(program: argv[0], otherArgs: "DOCUMENT" , args: the_args); |
54 | exit(status: 1); |
55 | } |
56 | |
57 | if (show_formats) { |
58 | const std::vector<std::string> formats = poppler::image::supported_image_formats(); |
59 | std::cout << "Supported image formats:" << std::endl; |
60 | for (const std::string &format : formats) { |
61 | std::cout << " " << format << std::endl; |
62 | } |
63 | exit(status: 0); |
64 | } |
65 | |
66 | if (!out_filename[0]) { |
67 | error(msg: "missing output filename (-o)" ); |
68 | } |
69 | |
70 | if (!poppler::page_renderer::can_render()) { |
71 | error(msg: "renderer compiled without Splash support" ); |
72 | } |
73 | |
74 | const std::string file_name(argv[1]); |
75 | |
76 | std::unique_ptr<poppler::document> doc(poppler::document::load_from_file(file_name)); |
77 | if (!doc.get()) { |
78 | error(msg: "loading error" ); |
79 | } |
80 | if (doc->is_locked()) { |
81 | error(msg: "encrypted document" ); |
82 | } |
83 | |
84 | if (doc_page < 0 || doc_page >= doc->pages()) { |
85 | error(msg: "specified page number out of page count" ); |
86 | } |
87 | std::unique_ptr<poppler::page> p(doc->create_page(index: doc_page)); |
88 | if (!p.get()) { |
89 | error(msg: "NULL page" ); |
90 | } |
91 | |
92 | poppler::page_renderer pr; |
93 | pr.set_render_hint(hint: poppler::page_renderer::antialiasing, on: true); |
94 | pr.set_render_hint(hint: poppler::page_renderer::text_antialiasing, on: true); |
95 | |
96 | poppler::image img = pr.render_page(p: p.get()); |
97 | if (!img.is_valid()) { |
98 | error(msg: "rendering failed" ); |
99 | } |
100 | |
101 | if (!img.save(file_name: out_filename, out_format: "png" )) { |
102 | error(msg: "saving to file failed" ); |
103 | } |
104 | |
105 | return 0; |
106 | } |
107 | |