summaryrefslogtreecommitdiff
path: root/xpdf/wrapper/wrapper_gpl.cxx
blob: c7b379614af1f0e979c139ade674c7031fe9b70c (plain)
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
/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: wrapper_gpl.cxx,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: ihi $ $Date: 2008-04-24 18:36:20 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU General Public License Version 2.
 *
 *
 *    GNU General Public License, version 2
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License as
 *    published by the Free Software Foundation; either version 2 of
 *    the License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public
 *    License along with this program; if not, write to the Free
 *    Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *    Boston, MA 02110-1301, USA.
 *
 ************************************************************************/

#include "pdfioutdev_gpl.hxx"
#include "SecurityHandler.h"
#ifdef WNT
# include <io.h>
# include <fcntl.h>  /*_O_BINARY*/
#endif

static char ownerPassword[33] = "\001";
static char userPassword[33]  = "\001";
static char outputFile[256]   = "\001";
static char cfgFileName[256]  = "";

FILE* g_binary_out=stderr;

static ArgDesc argDesc[] = {
  {"-f",          argString,      outputFile,     sizeof(outputFile),
   "output file for binary streams"},
  {"-opw",        argString,      ownerPassword,  sizeof(ownerPassword),
   "owner password (for encrypted files)"},
  {"-upw",        argString,      userPassword,   sizeof(userPassword),
   "user password (for encrypted files)"},
  {NULL, argString, NULL, 0, NULL }
};

int main(int argc, char **argv)
{
    // parse args; initialize to defaults
    if( !parseArgs(argDesc, &argc, argv) )
        return 1;

    if( argc < 2 )
        return 1;

    // read config file
    globalParams = new GlobalParams(cfgFileName);
    globalParams->setErrQuiet(gTrue);
    globalParams->setupBaseFonts(NULL);

    // PDFDoc takes over ownership for all strings below
    GString* pFileName = new GString(argv[1]);

    // check for password string(s)
    GString* pOwnerPasswordStr(
        ownerPassword[0] != '\001' ? new GString(ownerPassword)
        : (GString *)NULL );
    GString* pUserPasswordStr(
        userPassword[0] != '\001' ? new GString(userPassword)
        : (GString *)NULL );
    if( outputFile[0] != '\001' )
        g_binary_out = fopen(outputFile,"wb");

#ifdef WNT
    // Win actually modifies output for O_TEXT file mode, so need to
    // revert to binary here
    _setmode( _fileno( g_binary_out ), _O_BINARY );
#endif

    PDFDoc aDoc( pFileName,
                 pOwnerPasswordStr,
                 pUserPasswordStr );
    // Check various permissions.
   if ( !aDoc.okToPrint() ||
        !aDoc.okToChange()||
        !aDoc.okToCopy()||
        !aDoc.okToAddNotes() )
   {
       return 1;
   }



    if( !aDoc.isOk() )
        return 1;
    if ((userPassword[0] != '\001')||(ownerPassword[0] != '\001'))
        return 1;

    pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aDoc) );

    // tell receiver early - needed for proper progress calculation
    pOutDev->setPageNum( aDoc.getNumPages() );

    // virtual resolution of the PDF OutputDev in dpi
    static const int PDFI_OUTDEV_RESOLUTION=7200;

    // do the conversion
    const int nPages = aDoc.getNumPages();
    for( int i=1; i<=nPages; ++i )
    {
        aDoc.displayPage( pOutDev,
                          i,
                          PDFI_OUTDEV_RESOLUTION,
                          PDFI_OUTDEV_RESOLUTION,
                          0, gTrue, gTrue, gTrue );
        aDoc.processLinks( pOutDev, i );
    }

    return 0;
}