1
//========================================================================
5
// Copyright 1998-2003 Glyph & Cog, LLC
7
//========================================================================
9
//========================================================================
11
// Modified under the Poppler project - http://poppler.freedesktop.org
13
// All changes made under the Poppler project to this file are licensed
14
// under GPL version 2 or later
16
// Copyright (C) 2006 Dom Lachowicz <cinamod@hotmail.com>
17
// Copyright (C) 2007-2010 Albert Astals Cid <aacid@kde.org>
18
// Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
20
// To see a description of the changes please see the Changelog file that
21
// came with your tarball or type make ChangeLog if you are building from git
23
//========================================================================
26
#include <poppler-config.h>
33
#include "parseargs.h"
34
#include "printencodings.h"
35
#include "goo/GooString.h"
37
#include "GlobalParams.h"
46
#include "PDFDocFactory.h"
47
#include "CharTypes.h"
48
#include "UnicodeMap.h"
49
#include "PDFDocEncoding.h"
53
static void printInfoString(Dict *infoDict, char *key, char *text,
55
static void printInfoDate(Dict *infoDict, char *key, char *text);
56
static void printBox(char *text, PDFRectangle *box);
58
static int firstPage = 1;
59
static int lastPage = 0;
60
static GBool printBoxes = gFalse;
61
static GBool printMetadata = gFalse;
62
static char textEncName[128] = "";
63
static char ownerPassword[33] = "\001";
64
static char userPassword[33] = "\001";
65
static GBool printVersion = gFalse;
66
static GBool printHelp = gFalse;
67
static GBool printEnc = gFalse;
69
static const ArgDesc argDesc[] = {
70
{"-f", argInt, &firstPage, 0,
71
"first page to convert"},
72
{"-l", argInt, &lastPage, 0,
73
"last page to convert"},
74
{"-box", argFlag, &printBoxes, 0,
75
"print the page bounding boxes"},
76
{"-meta", argFlag, &printMetadata, 0,
77
"print the document metadata (XML)"},
78
{"-enc", argString, textEncName, sizeof(textEncName),
79
"output text encoding name"},
80
{"-listenc",argFlag, &printEnc, 0,
81
"list available encodings"},
82
{"-opw", argString, ownerPassword, sizeof(ownerPassword),
83
"owner password (for encrypted files)"},
84
{"-upw", argString, userPassword, sizeof(userPassword),
85
"user password (for encrypted files)"},
86
{"-v", argFlag, &printVersion, 0,
87
"print copyright and version info"},
88
{"-h", argFlag, &printHelp, 0,
89
"print usage information"},
90
{"-help", argFlag, &printHelp, 0,
91
"print usage information"},
92
{"--help", argFlag, &printHelp, 0,
93
"print usage information"},
94
{"-?", argFlag, &printHelp, 0,
95
"print usage information"},
99
int main(int argc, char *argv[]) {
102
GooString *ownerPW, *userPW;
107
double w, h, wISO, hISO;
118
ok = parseArgs(argDesc, &argc, argv);
119
if (!ok || (argc != 2 && !printEnc) || printVersion || printHelp) {
120
fprintf(stderr, "pdfinfo version %s\n", PACKAGE_VERSION);
121
fprintf(stderr, "%s\n", popplerCopyright);
122
fprintf(stderr, "%s\n", xpdfCopyright);
124
printUsage("pdfinfo", "<PDF-file>", argDesc);
126
if (printVersion || printHelp)
132
globalParams = new GlobalParams();
141
fileName = new GooString(argv[1]);
143
if (textEncName[0]) {
144
globalParams->setTextEncoding(textEncName);
147
// get mapping to output encoding
148
if (!(uMap = globalParams->getTextEncoding())) {
149
error(-1, "Couldn't get text encoding");
155
if (ownerPassword[0] != '\001') {
156
ownerPW = new GooString(ownerPassword);
160
if (userPassword[0] != '\001') {
161
userPW = new GooString(userPassword);
166
if (fileName->cmp("-") == 0) {
168
fileName = new GooString("fd://0");
171
doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
194
if (lastPage < 1 || lastPage > doc->getNumPages()) {
195
lastPage = doc->getNumPages();
199
doc->getDocInfo(&info);
201
printInfoString(info.getDict(), "Title", "Title: ", uMap);
202
printInfoString(info.getDict(), "Subject", "Subject: ", uMap);
203
printInfoString(info.getDict(), "Keywords", "Keywords: ", uMap);
204
printInfoString(info.getDict(), "Author", "Author: ", uMap);
205
printInfoString(info.getDict(), "Creator", "Creator: ", uMap);
206
printInfoString(info.getDict(), "Producer", "Producer: ", uMap);
207
printInfoDate(info.getDict(), "CreationDate", "CreationDate: ");
208
printInfoDate(info.getDict(), "ModDate", "ModDate: ");
212
// print tagging info
213
printf("Tagged: %s\n",
214
doc->getStructTreeRoot()->isDict() ? "yes" : "no");
217
printf("Pages: %d\n", doc->getNumPages());
219
// print encryption info
220
printf("Encrypted: ");
221
if (doc->isEncrypted()) {
222
printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
223
doc->okToPrint(gTrue) ? "yes" : "no",
224
doc->okToCopy(gTrue) ? "yes" : "no",
225
doc->okToChange(gTrue) ? "yes" : "no",
226
doc->okToAddNotes(gTrue) ? "yes" : "no");
232
for (pg = firstPage; pg <= lastPage; ++pg) {
233
w = doc->getPageCropWidth(pg);
234
h = doc->getPageCropHeight(pg);
236
printf("Page %4d size: %g x %g pts", pg, w, h);
238
printf("Page size: %g x %g pts", w, h);
240
if ((fabs(w - 612) < 0.1 && fabs(h - 792) < 0.1) ||
241
(fabs(w - 792) < 0.1 && fabs(h - 612) < 0.1)) {
244
hISO = sqrt(sqrt(2.0)) * 7200 / 2.54;
245
wISO = hISO / sqrt(2.0);
246
for (i = 0; i <= 6; ++i) {
247
if ((fabs(w - wISO) < 1 && fabs(h - hISO) < 1) ||
248
(fabs(w - hISO) < 1 && fabs(h - wISO) < 1)) {
262
for (pg = firstPage; pg <= lastPage; ++pg) {
263
page = doc->getPage(pg);
265
error(-1, "Failed to print boxes for page %d", pg);
268
sprintf(buf, "Page %4d MediaBox: ", pg);
269
printBox(buf, page->getMediaBox());
270
sprintf(buf, "Page %4d CropBox: ", pg);
271
printBox(buf, page->getCropBox());
272
sprintf(buf, "Page %4d BleedBox: ", pg);
273
printBox(buf, page->getBleedBox());
274
sprintf(buf, "Page %4d TrimBox: ", pg);
275
printBox(buf, page->getTrimBox());
276
sprintf(buf, "Page %4d ArtBox: ", pg);
277
printBox(buf, page->getArtBox());
280
page = doc->getPage(firstPage);
282
error(-1, "Failed to print boxes for page %d", firstPage);
284
printBox("MediaBox: ", page->getMediaBox());
285
printBox("CropBox: ", page->getCropBox());
286
printBox("BleedBox: ", page->getBleedBox());
287
printBox("TrimBox: ", page->getTrimBox());
288
printBox("ArtBox: ", page->getArtBox());
295
f = fopen(fileName->getCString(), "rb", "ctx=stm");
297
f = fopen(fileName->getCString(), "rb");
301
fseeko(f, 0, SEEK_END);
302
printf("File size: %u bytes\n", (Guint)ftello(f));
304
fseek64(f, 0, SEEK_END);
305
printf("File size: %u bytes\n", (Guint)ftell64(f));
307
fseek(f, 0, SEEK_END);
308
printf("File size: %d bytes\n", (int)ftell(f));
313
// print linearization info
314
printf("Optimized: %s\n", doc->isLinearized() ? "yes" : "no");
317
printf("PDF version: %d.%d\n", doc->getPDFMajorVersion(), doc->getPDFMinorVersion());
319
// print the metadata
320
if (printMetadata && (metadata = doc->readMetadata())) {
321
fputs("Metadata:\n", stdout);
322
fputs(metadata->getCString(), stdout);
338
// check for memory leaks
339
Object::memCheck(stderr);
345
static void printInfoString(Dict *infoDict, char *key, char *text,
354
if (infoDict->lookup(key, &obj)->isString()) {
356
s1 = obj.getString();
357
if ((s1->getChar(0) & 0xff) == 0xfe &&
358
(s1->getChar(1) & 0xff) == 0xff) {
365
while (i < obj.getString()->getLength()) {
367
u = ((s1->getChar(i) & 0xff) << 8) |
368
(s1->getChar(i+1) & 0xff);
371
u = pdfDocEncoding[s1->getChar(i) & 0xff];
374
n = uMap->mapUnicode(u, buf, sizeof(buf));
375
fwrite(buf, 1, n, stdout);
382
static void printInfoDate(Dict *infoDict, char *key, char *text) {
385
int year, mon, day, hour, min, sec, tz_hour, tz_minute;
390
if (infoDict->lookup(key, &obj)->isString()) {
392
s = obj.getString()->getCString();
393
// TODO do something with the timezone info
394
if ( parseDateString( s, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute ) ) {
395
tmStruct.tm_year = year - 1900;
396
tmStruct.tm_mon = mon - 1;
397
tmStruct.tm_mday = day;
398
tmStruct.tm_hour = hour;
399
tmStruct.tm_min = min;
400
tmStruct.tm_sec = sec;
401
tmStruct.tm_wday = -1;
402
tmStruct.tm_yday = -1;
403
tmStruct.tm_isdst = -1;
404
// compute the tm_wday and tm_yday fields
405
if (mktime(&tmStruct) != (time_t)-1 &&
406
strftime(buf, sizeof(buf), "%c", &tmStruct)) {
419
static void printBox(char *text, PDFRectangle *box) {
420
printf("%s%8.2f %8.2f %8.2f %8.2f\n",
421
text, box->x1, box->y1, box->x2, box->y2);