1 /*
2  * Copyright (C) 2007 Jauco Noordzij <jauco@jauco.nl>
3  * Copyright (C) 2007 Dominic Lachowicz <cinamod@hotmail.com>
4  * Copyright (C) 2007 Kouhei Sutou <kou@cozmixng.org>
5  * Copyright (C) 2009 Jakub Wilk <ubanus@users.sf.net>
6  * Copyright (C) 2009, 2010 Albert Astals Cid <aacid@kde.org>
7  * Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #include "config.h"
25 #include <poppler-config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stddef.h>
29 #include <string.h>
30 #ifdef HAVE_DIRENT_H
31 #include <dirent.h>
32 #endif
33 #include <time.h>
34 #include "parseargs.h"
35 #include "goo/GooString.h"
36 #include "goo/gmem.h"
37 #include "Object.h"
38 #include "Stream.h"
39 #include "Array.h"
40 #include "Dict.h"
41 #include "XRef.h"
42 #include "Catalog.h"
43 #include "Page.h"
44 #include "PDFDoc.h"
45 #include "PDFDocFactory.h"
46 #include "ABWOutputDev.h"
47 #include "PSOutputDev.h"
48 #include "GlobalParams.h"
49 #include "Error.h"
50 #include "goo/gfile.h"
51 #include <libxml/parser.h>
52 #include <libxml/tree.h>
53 
54 static int firstPage = 1;
55 static int lastPage = 0;
56 static GBool quiet = gFalse;
57 static GBool printHelp = gFalse;
58 static GBool printVersion = gFalse;
59 static GBool stout = gFalse;
60 static char ownerPassword[33] = "";
61 static char userPassword[33] = "";
62 
63 // static char textEncName[128] = "";
64 
65 static const ArgDesc argDesc[] = {
66   {"-f",      argInt,      &firstPage,     0,
67    "first page to convert"},
68   {"-l",      argInt,      &lastPage,      0,
69    "last page to convert"},
70   {"--stdout"  ,argFlag,    &stout,         0,
71    "use standard output"},
72   {"--opw",    argString,   ownerPassword,  sizeof(ownerPassword),
73    "owner password (for encrypted files)"},
74   {"--upw",    argString,   userPassword,   sizeof(userPassword),
75    "user password (for encrypted files)"},
76   {"-q",       argFlag,     &quiet,         0,
77    "don't print any messages or errors"},
78   {"-v",       argFlag,     &printVersion,  0,
79    "print copyright and version info"},
80   {"-h",       argFlag,     &printHelp,     0,
81    "print usage information"},
82   {"-help",    argFlag,     &printHelp,     0,
83    "print usage information"},
84   {"--help",   argFlag,     &printHelp,     0,
85    "print usage information"},
86   {"-?",       argFlag,     &printHelp,     0,
87    "print usage information"},
88   {NULL}
89 };
90 
main(int argc,char * argv[])91 int main(int argc, char *argv[]) {
92   GBool ok;
93   PDFDoc *doc = NULL;
94   GooString *fileName = NULL;
95 //  GooString *abwFileName = NULL;
96   ABWOutputDev *abwOut = NULL;
97 //  GBool ok;
98   GooString *ownerPW, *userPW;
99   Object info;
100 
101   int result = 1;
102 
103   char * outpName;
104   xmlDocPtr XMLdoc = NULL;
105 
106   // parse args
107   ok = parseArgs(argDesc, &argc, argv);
108   if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
109     fprintf(stderr, "pdftoabw version %s\n", PACKAGE_VERSION);
110     fprintf(stderr, "%s\n", popplerCopyright);
111     fprintf(stderr, "%s\n", xpdfCopyright);
112     if (!printVersion) {
113       printUsage("pdftoabw", "<PDF-file> [abw-file]", argDesc);
114     }
115     goto err0;
116   }
117   globalParams = new GlobalParams();
118   if (quiet) {
119     globalParams->setErrQuiet(quiet);
120   }
121 
122   fileName = new GooString(argv[1]);
123   if (stout || (argc < 3)){
124     outpName = "-";
125   }
126   else {
127     outpName = argv[2];
128   }
129 
130   if (ownerPassword[0]) {
131     ownerPW = new GooString(ownerPassword);
132   } else {
133     ownerPW = NULL;
134   }
135   if (userPassword[0]) {
136     userPW = new GooString(userPassword);
137   } else {
138     userPW = NULL;
139   }
140 
141   if (fileName->cmp("-") == 0) {
142       delete fileName;
143       fileName = new GooString("fd://0");
144   }
145 
146   doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
147 
148   if (userPW) {
149     delete userPW;
150   }
151   if (ownerPW) {
152     delete ownerPW;
153   }
154 
155   if (!doc || !doc->isOk())
156     {
157       fprintf (stderr, "Error opening PDF %s\n", fileName->getCString());
158       goto error;
159     }
160 
161   // check for copy permission
162   if (!doc->okToCopy()) {
163     fprintf(stderr, "Copying of text from this document is not allowed.\n");
164     goto error;
165   }
166 
167   XMLdoc = xmlNewDoc(BAD_CAST "1.0");
168   abwOut = new ABWOutputDev(XMLdoc);
169   abwOut->setPDFDoc(doc);
170 
171   if (lastPage == 0 || lastPage > doc->getNumPages ()) lastPage = doc->getNumPages();
172   if (firstPage < 1) firstPage = 1;
173 
174   if (abwOut->isOk())
175   {
176     doc->displayPages(abwOut, firstPage, lastPage, 72, 72, 0, gTrue, gFalse, gFalse);
177     abwOut->createABW();
178   }
179 
180   if (xmlSaveFormatFileEnc(outpName, XMLdoc, "UTF-8", 1) == -1)
181     {
182       fprintf (stderr, "Error saving to %s\n", outpName);
183       goto error;
184     }
185 
186   result = 0;
187 
188  error:
189   // clean up
190   if(globalParams) delete globalParams;
191   if(doc) delete doc;
192   delete fileName;
193   if(XMLdoc) xmlFreeDoc(XMLdoc);
194   if(abwOut) delete abwOut;
195  err0:
196   // check for memory leaks
197   Object::memCheck(stderr);
198   gMemReport(stderr);
199 
200   return result;
201 }
202