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