1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 #include "pdfioutdev_gpl.hxx"
23 //#include "SecurityHandler.h"
24 #ifdef WNT
25 # include <io.h>
26 # include <fcntl.h> /*_O_BINARY*/
27 #endif
28
29 FILE* g_binary_out=stderr;
30
31 static const char *ownerPassword = "\001";
32 static const char *userPassword = "\001";
33 static const char *outputFile = "\001";
34
main(int argc,char ** argv)35 int main(int argc, char **argv)
36 {
37 int k = 0;
38 while (k < argc)
39 {
40 if (!strcmp(argv[k], "-f"))
41 {
42 outputFile = argv[k+1];
43 --argc;
44 for (int j = k; j < argc; ++j)
45 argv[j] = argv[j+1];
46 }
47 else if (!strcmp(argv[k], "-opw"))
48 {
49 ownerPassword = argv[k+1];
50 --argc;
51 for (int j = k; j < argc; ++j)
52 argv[j] = argv[j+1];
53 }
54 else if (!strcmp(argv[k], "-upw"))
55 {
56 userPassword = argv[k+1];
57 --argc;
58 for (int j = k; j < argc; ++j)
59 argv[j] = argv[j+1];
60 }
61 ++k;
62 }
63
64 if( argc < 2 )
65 return 1;
66
67 // read config file
68 globalParams = new GlobalParams(
69 );
70 globalParams->setErrQuiet(gTrue);
71 #ifdef _MSC_VER
72 globalParams->setupBaseFonts(NULL);
73 #endif
74
75 // try to read a possible open password form stdin
76 char aPwBuf[129];
77 aPwBuf[128] = 0;
78 if( ! fgets( aPwBuf, sizeof(aPwBuf)-1, stdin ) )
79 aPwBuf[0] = 0; // mark as empty
80 else
81 {
82 for( unsigned int i = 0; i < sizeof(aPwBuf); i++ )
83 {
84 if( aPwBuf[i] == '\n' )
85 {
86 aPwBuf[i] = 0;
87 break;
88 }
89 }
90 }
91
92 // PDFDoc takes over ownership for all strings below
93 GooString* pFileName = new GooString(argv[1]);
94 GooString* pTempErrFileName = new GooString("_err.pdf");
95 GooString* pTempErrFileNamePath = new GooString(argv[0]);
96
97 GooString* pErrFileName = new GooString(pTempErrFileNamePath,pTempErrFileName);
98
99
100 // check for password string(s)
101 GooString* pOwnerPasswordStr( aPwBuf[0] != 0
102 ? new GooString( aPwBuf )
103 : (ownerPassword[0] != '\001'
104 ? new GooString(ownerPassword)
105 : (GooString *)NULL ) );
106 GooString* pUserPasswordStr( userPassword[0] != '\001'
107 ? new GooString(userPassword)
108 : (GooString *)NULL );
109 if( outputFile[0] != '\001' )
110 g_binary_out = fopen(outputFile,"wb");
111
112 #ifdef WNT
113 // Win actually modifies output for O_TEXT file mode, so need to
114 // revert to binary here
115 _setmode( _fileno( g_binary_out ), _O_BINARY );
116 #endif
117
118 PDFDoc aDoc( pFileName,
119 pOwnerPasswordStr,
120 pUserPasswordStr );
121
122 PDFDoc aErrDoc( pErrFileName,
123 pOwnerPasswordStr,
124 pUserPasswordStr );
125
126
127 // Check various permissions.
128 if ( !aDoc.isOk()||
129 !aDoc.okToPrint() ||
130 !aDoc.okToChange()||
131 !aDoc.okToCopy()||
132 !aDoc.okToAddNotes() )
133 {
134 pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aErrDoc) );
135
136 const int nPages = aErrDoc.isOk() ? aErrDoc.getNumPages() : 0;
137
138 // tell receiver early - needed for proper progress calculation
139 pOutDev->setPageNum( nPages );
140
141 // virtual resolution of the PDF OutputDev in dpi
142 static const int PDFI_OUTDEV_RESOLUTION=7200;
143
144 // do the conversion
145 for( int i=1; i<=nPages; ++i )
146 {
147 aErrDoc.displayPage( pOutDev,
148 i,
149 PDFI_OUTDEV_RESOLUTION,
150 PDFI_OUTDEV_RESOLUTION,
151 0, gTrue, gTrue, gTrue );
152 aErrDoc.processLinks( pOutDev, i );
153 }
154 }
155 else
156 {
157 pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aDoc) );
158
159 // tell receiver early - needed for proper progress calculation
160 pOutDev->setPageNum( aDoc.getNumPages() );
161
162 // virtual resolution of the PDF OutputDev in dpi
163 static const int PDFI_OUTDEV_RESOLUTION=7200;
164
165 // do the conversion
166 const int nPages = aDoc.getNumPages();
167 for( int i=1; i<=nPages; ++i )
168 {
169 aDoc.displayPage( pOutDev,
170 i,
171 PDFI_OUTDEV_RESOLUTION,
172 PDFI_OUTDEV_RESOLUTION,
173 0, gTrue, gTrue, gTrue );
174 aDoc.processLinks( pOutDev, i );
175 }
176 }
177 return 0;
178 }
179
180