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