1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
//========================================================================
//
// pdftohtml.cc
//
//
// Copyright 1999-2000 G. Ovtcharov
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2007-2008 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#include "config.h"
#include <poppler-config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include <time.h>
#include "parseargs.h"
#include "goo/GooString.h"
#include "goo/gmem.h"
#include "Object.h"
#include "Stream.h"
#include "Array.h"
#include "Dict.h"
#include "XRef.h"
#include "Catalog.h"
#include "Page.h"
#include "PDFDoc.h"
#include "PDFDocFactory.h"
#include "HtmlOutputDev.h"
#include "PSOutputDev.h"
#include "GlobalParams.h"
#include "Error.h"
#include "DateInfo.h"
#include "goo/gfile.h"
#ifndef GHOSTSCRIPT
# define GHOSTSCRIPT "gs"
#endif
static int firstPage = 1;
static int lastPage = 0;
static GBool rawOrder = gTrue;
GBool printCommands = gTrue;
static GBool printHelp = gFalse;
GBool printHtml = gFalse;
GBool complexMode=gFalse;
GBool ignore=gFalse;
//char extension[5]=".png";
double scale=1.5;
GBool noframes=gFalse;
GBool stout=gFalse;
GBool xml=gFalse;
static GBool errQuiet=gFalse;
static GBool noDrm=gFalse;
GBool showHidden = gFalse;
GBool noMerge = gFalse;
static char ownerPassword[33] = "";
static char userPassword[33] = "";
static char gsDevice[33] = "png16m";
static GBool printVersion = gFalse;
static GooString* getInfoString(Dict *infoDict, char *key);
static GooString* getInfoDate(Dict *infoDict, char *key);
static char textEncName[128] = "";
static const ArgDesc argDesc[] = {
{"-f", argInt, &firstPage, 0,
"first page to convert"},
{"-l", argInt, &lastPage, 0,
"last page to convert"},
/*{"-raw", argFlag, &rawOrder, 0,
"keep strings in content stream order"},*/
{"-q", argFlag, &errQuiet, 0,
"don't print any messages or errors"},
{"-h", argFlag, &printHelp, 0,
"print usage information"},
{"-help", argFlag, &printHelp, 0,
"print usage information"},
{"-p", argFlag, &printHtml, 0,
"exchange .pdf links by .html"},
{"-c", argFlag, &complexMode, 0,
"generate complex document"},
{"-i", argFlag, &ignore, 0,
"ignore images"},
{"-noframes", argFlag, &noframes, 0,
"generate no frames"},
{"-stdout" ,argFlag, &stout, 0,
"use standard output"},
{"-zoom", argFP, &scale, 0,
"zoom the pdf document (default 1.5)"},
{"-xml", argFlag, &xml, 0,
"output for XML post-processing"},
{"-hidden", argFlag, &showHidden, 0,
"output hidden text"},
{"-nomerge", argFlag, &noMerge, 0,
"do not merge paragraphs"},
{"-enc", argString, textEncName, sizeof(textEncName),
"output text encoding name"},
{"-dev", argString, gsDevice, sizeof(gsDevice),
"output device name for Ghostscript (png16m, jpeg etc)"},
{"-v", argFlag, &printVersion, 0,
"print copyright and version info"},
{"-opw", argString, ownerPassword, sizeof(ownerPassword),
"owner password (for encrypted files)"},
{"-upw", argString, userPassword, sizeof(userPassword),
"user password (for encrypted files)"},
{"-nodrm", argFlag, &noDrm, 0,
"override document DRM settings"},
{NULL}
};
int main(int argc, char *argv[]) {
PDFDoc *doc = NULL;
GooString *fileName = NULL;
GooString *docTitle = NULL;
GooString *author = NULL, *keywords = NULL, *subject = NULL, *date = NULL;
GooString *htmlFileName = NULL;
GooString *psFileName = NULL;
HtmlOutputDev *htmlOut = NULL;
PSOutputDev *psOut = NULL;
GBool ok;
char *p;
char extension[16] = "png";
GooString *ownerPW, *userPW;
Object info;
char * extsList[] = {"png", "jpeg", "bmp", "pcx", "tiff", "pbm", NULL};
// parse args
ok = parseArgs(argDesc, &argc, argv);
if (!ok || argc < 2 || argc > 3 || printHelp || printVersion) {
fprintf(stderr, "pdftohtml version %s\n", PACKAGE_VERSION);
fprintf(stderr, "%s\n", popplerCopyright);
fprintf(stderr, "%s\n", "Copyright 1999-2003 Gueorgui Ovtcharov and Rainer Dorsch");
fprintf(stderr, "%s\n\n", xpdfCopyright);
if (!printVersion) {
printUsage("pdftohtml", "<PDF-file> [<html-file> <xml-file>]", argDesc);
}
exit(1);
}
// init error file
//errorInit();
// read config file
globalParams = new GlobalParams();
if (errQuiet) {
globalParams->setErrQuiet(errQuiet);
printCommands = gFalse; // I'm not 100% what is the differecne between them
}
if (textEncName[0]) {
globalParams->setTextEncoding(textEncName);
if( !globalParams->getTextEncoding() ) {
goto error;
}
}
// open PDF file
if (ownerPassword[0]) {
ownerPW = new GooString(ownerPassword);
} else {
ownerPW = NULL;
}
if (userPassword[0]) {
userPW = new GooString(userPassword);
} else {
userPW = NULL;
}
fileName = new GooString(argv[1]);
if (fileName->cmp("-") == 0) {
delete fileName;
fileName = new GooString("fd://0");
}
doc = PDFDocFactory().createPDFDoc(fileName, ownerPW, userPW);
if (userPW) {
delete userPW;
}
if (ownerPW) {
delete ownerPW;
}
if (!doc->isOk()) {
goto error;
}
// check for copy permission
if (!doc->okToCopy()) {
if (!noDrm) {
error(-1, "Copying of text from this document is not allowed.");
goto error;
}
fprintf(stderr, "Document has copy-protection bit set.\n");
}
// construct text file name
if (argc == 3) {
GooString* tmp = new GooString(argv[2]);
p=tmp->getCString()+tmp->getLength()-5;
if (!xml)
if (!strcmp(p, ".html") || !strcmp(p, ".HTML"))
htmlFileName = new GooString(tmp->getCString(),
tmp->getLength() - 5);
else htmlFileName =new GooString(tmp);
else
if (!strcmp(p, ".xml") || !strcmp(p, ".XML"))
htmlFileName = new GooString(tmp->getCString(),
tmp->getLength() - 5);
else htmlFileName =new GooString(tmp);
delete tmp;
} else {
p = fileName->getCString() + fileName->getLength() - 4;
if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF"))
htmlFileName = new GooString(fileName->getCString(),
fileName->getLength() - 4);
else
htmlFileName = fileName->copy();
// htmlFileName->append(".html");
}
if (scale>3.0) scale=3.0;
if (scale<0.5) scale=0.5;
if (complexMode) {
//noframes=gFalse;
stout=gFalse;
}
if (stout) {
noframes=gTrue;
complexMode=gFalse;
}
if (xml)
{
complexMode = gTrue;
noframes = gTrue;
noMerge = gTrue;
}
// get page range
if (firstPage < 1)
firstPage = 1;
if (lastPage < 1 || lastPage > doc->getNumPages())
lastPage = doc->getNumPages();
doc->getDocInfo(&info);
if (info.isDict()) {
docTitle = getInfoString(info.getDict(), "Title");
author = getInfoString(info.getDict(), "Author");
keywords = getInfoString(info.getDict(), "Keywords");
subject = getInfoString(info.getDict(), "Subject");
date = getInfoDate(info.getDict(), "ModDate");
if( !date )
date = getInfoDate(info.getDict(), "CreationDate");
}
info.free();
if( !docTitle ) docTitle = new GooString(htmlFileName);
/* determine extensions of output backgroun images */
{int i;
for(i = 0; extsList[i]; i++)
{
if( strstr(gsDevice, extsList[i]) != (char *) NULL )
{
strncpy(extension, extsList[i], sizeof(extension));
break;
}
}}
rawOrder = complexMode; // todo: figure out what exactly rawOrder do :)
// write text file
htmlOut = new HtmlOutputDev(htmlFileName->getCString(),
docTitle->getCString(),
author ? author->getCString() : NULL,
keywords ? keywords->getCString() : NULL,
subject ? subject->getCString() : NULL,
date ? date->getCString() : NULL,
extension,
rawOrder,
firstPage,
doc->getCatalog()->getOutline()->isDict());
delete docTitle;
if( author )
{
delete author;
}
if( keywords )
{
delete keywords;
}
if( subject )
{
delete subject;
}
if( date )
{
delete date;
}
if (htmlOut->isOk())
{
doc->displayPages(htmlOut, firstPage, lastPage, 72, 72, 0,
gTrue, gFalse, gFalse);
if (!xml)
{
htmlOut->dumpDocOutline(doc->getCatalog());
}
}
if( complexMode && !xml && !ignore ) {
int h=xoutRound(htmlOut->getPageHeight()/scale);
int w=xoutRound(htmlOut->getPageWidth()/scale);
//int h=xoutRound(doc->getPageHeight(1)/scale);
//int w=xoutRound(doc->getPageWidth(1)/scale);
psFileName = new GooString(htmlFileName->getCString());
psFileName->append(".ps");
psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(),
doc->getCatalog(), NULL, firstPage, lastPage, psModePS, w, h);
psOut->setDisplayText(gFalse);
doc->displayPages(psOut, firstPage, lastPage, 72, 72, 0,
gTrue, gFalse, gFalse);
delete psOut;
/*sprintf(buf, "%s -sDEVICE=png16m -dBATCH -dNOPROMPT -dNOPAUSE -r72 -sOutputFile=%s%%03d.png -g%dx%d -q %s", GHOSTSCRIPT, htmlFileName->getCString(), w, h,
psFileName->getCString());*/
GooString *gsCmd = new GooString(GHOSTSCRIPT);
GooString *tw, *th, *sc;
gsCmd->append(" -sDEVICE=");
gsCmd->append(gsDevice);
gsCmd->append(" -dBATCH -dNOPROMPT -dNOPAUSE -r");
sc = GooString::fromInt(static_cast<int>(72*scale));
gsCmd->append(sc);
gsCmd->append(" -sOutputFile=");
gsCmd->append("\"");
gsCmd->append(htmlFileName);
gsCmd->append("%03d.");
gsCmd->append(extension);
gsCmd->append("\" -g");
tw = GooString::fromInt(static_cast<int>(scale*w));
gsCmd->append(tw);
gsCmd->append("x");
th = GooString::fromInt(static_cast<int>(scale*h));
gsCmd->append(th);
gsCmd->append(" -q \"");
gsCmd->append(psFileName);
gsCmd->append("\"");
// printf("running: %s\n", gsCmd->getCString());
if( !executeCommand(gsCmd->getCString()) && !errQuiet) {
error(-1, "Failed to launch Ghostscript!\n");
}
unlink(psFileName->getCString());
delete tw;
delete th;
delete sc;
delete gsCmd;
delete psFileName;
}
delete htmlOut;
// clean up
error:
if(doc) delete doc;
if(globalParams) delete globalParams;
if(htmlFileName) delete htmlFileName;
HtmlFont::clear();
// check for memory leaks
Object::memCheck(stderr);
gMemReport(stderr);
return 0;
}
static GooString* getInfoString(Dict *infoDict, char *key) {
Object obj;
GooString *s1 = NULL;
if (infoDict->lookup(key, &obj)->isString()) {
s1 = new GooString(obj.getString());
}
obj.free();
return s1;
}
static GooString* getInfoDate(Dict *infoDict, char *key) {
Object obj;
char *s;
int year, mon, day, hour, min, sec, tz_hour, tz_minute;
char tz;
struct tm tmStruct;
GooString *result = NULL;
char buf[256];
if (infoDict->lookup(key, &obj)->isString()) {
s = obj.getString()->getCString();
// TODO do something with the timezone info
if ( parseDateString( s, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute ) ) {
tmStruct.tm_year = year - 1900;
tmStruct.tm_mon = mon - 1;
tmStruct.tm_mday = day;
tmStruct.tm_hour = hour;
tmStruct.tm_min = min;
tmStruct.tm_sec = sec;
tmStruct.tm_wday = -1;
tmStruct.tm_yday = -1;
tmStruct.tm_isdst = -1;
mktime(&tmStruct); // compute the tm_wday and tm_yday fields
if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S+00:00", &tmStruct)) {
result = new GooString(buf);
} else {
result = new GooString(s);
}
} else {
result = new GooString(s);
}
}
obj.free();
return result;
}
|