Update ooo320-m1
[ooovba.git] / sdext / source / pdfimport / xpdfwrapper / wrapper_gpl.cxx
blobbbb329c298fca7eb637ab739c8533489d9d343d7
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: wrapper_gpl.cxx,v $
7 * $Revision: 1.1.2.1 $
9 * last change: $Author: cmc $ $Date: 2008/08/25 16:17:55 $
11 * The Contents of this file are made available subject to
12 * the terms of GNU General Public License Version 2.
15 * GNU General Public License, version 2
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License as
22 * published by the Free Software Foundation; either version 2 of
23 * the License, or (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public
31 * License along with this program; if not, write to the Free
32 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
33 * Boston, MA 02110-1301, USA.
35 ************************************************************************/
37 #include "pdfioutdev_gpl.hxx"
38 //#include "SecurityHandler.h"
39 #ifdef WNT
40 # include <io.h>
41 # include <fcntl.h> /*_O_BINARY*/
42 #endif
44 FILE* g_binary_out=stderr;
46 #ifndef SYSTEM_POPPLER
47 static char ownerPassword[33] = "\001";
48 static char userPassword[33] = "\001";
49 static char outputFile[256] = "\001";
51 static ArgDesc argDesc[] = {
52 {(char*)"-f", argString, outputFile, sizeof(outputFile),
53 (char*)"output file for binary streams"},
54 {(char*)"-opw", argString, ownerPassword, sizeof(ownerPassword),
55 (char*)"owner password (for encrypted files)"},
56 {(char*)"-upw", argString, userPassword, sizeof(userPassword),
57 (char*)"user password (for encrypted files)"},
58 {NULL, argString, NULL, 0, NULL }
60 #else
61 static const char *ownerPassword = "\001";
62 static const char *userPassword = "\001";
63 static const char *outputFile = "\001";
64 #endif
66 int main(int argc, char **argv)
68 #ifndef SYSTEM_POPPLER
69 // parse args; initialize to defaults
70 if( !parseArgs(argDesc, &argc, argv) )
71 return 1;
72 #else
73 int k = 0;
74 while (k < argc)
76 if (!strcmp(argv[k], "-f"))
78 outputFile = argv[k+1];
79 --argc;
80 for (int j = k; j < argc; ++j)
81 argv[j] = argv[j+1];
83 else if (!strcmp(argv[k], "-opw"))
85 ownerPassword = argv[k+1];
86 --argc;
87 for (int j = k; j < argc; ++j)
88 argv[j] = argv[j+1];
90 else if (!strcmp(argv[k], "-upw"))
92 userPassword = argv[k+1];
93 --argc;
94 for (int j = k; j < argc; ++j)
95 argv[j] = argv[j+1];
97 ++k;
99 #endif
101 if( argc < 2 )
102 return 1;
104 // read config file
105 globalParams = new GlobalParams(
106 #ifndef SYSTEM_POPPLER
107 (char*)""
108 #endif
110 globalParams->setErrQuiet(gTrue);
111 #if !defined(SYSTEM_POPPLER) || defined(_MSC_VER)
112 globalParams->setupBaseFonts(NULL);
113 #endif
115 // try to read a possible open password form stdin
116 char aPwBuf[34];
117 aPwBuf[33] = 0;
118 if( ! fgets( aPwBuf, sizeof(aPwBuf)-1, stdin ) )
119 aPwBuf[0] = 0; // mark as empty
120 else
122 for( unsigned int i = 0; i < sizeof(aPwBuf); i++ )
124 if( aPwBuf[i] == '\n' )
126 aPwBuf[i] = 0;
127 break;
132 // PDFDoc takes over ownership for all strings below
133 GooString* pFileName = new GooString(argv[1]);
134 GooString* pTempErrFileName = new GooString("_err.pdf");
135 GooString* pTempErrFileNamePath = new GooString(argv[0]);
137 GooString* pErrFileName = new GooString(pTempErrFileNamePath,pTempErrFileName);
140 // check for password string(s)
141 GooString* pOwnerPasswordStr( ownerPassword[0] != '\001'
142 ? new GooString(ownerPassword)
143 : (GooString *)NULL );
144 GooString* pUserPasswordStr( aPwBuf[0] != 0
145 ? new GooString( aPwBuf )
146 : ( userPassword[0] != '\001'
147 ? new GooString(userPassword)
148 : (GooString *)NULL ) );
149 if( outputFile[0] != '\001' )
150 g_binary_out = fopen(outputFile,"wb");
152 #ifdef WNT
153 // Win actually modifies output for O_TEXT file mode, so need to
154 // revert to binary here
155 _setmode( _fileno( g_binary_out ), _O_BINARY );
156 #endif
158 PDFDoc aDoc( pFileName,
159 pOwnerPasswordStr,
160 pUserPasswordStr );
162 PDFDoc aErrDoc( pErrFileName,
163 pOwnerPasswordStr,
164 pUserPasswordStr );
167 // Check various permissions.
168 if ( !aDoc.isOk() )
170 pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aErrDoc) );
172 const int nPages = aErrDoc.isOk() ? aErrDoc.getNumPages() : 0;
174 // tell receiver early - needed for proper progress calculation
175 pOutDev->setPageNum( nPages );
177 // virtual resolution of the PDF OutputDev in dpi
178 static const int PDFI_OUTDEV_RESOLUTION=7200;
180 // do the conversion
181 for( int i=1; i<=nPages; ++i )
183 aErrDoc.displayPage( pOutDev,
185 PDFI_OUTDEV_RESOLUTION,
186 PDFI_OUTDEV_RESOLUTION,
187 0, gTrue, gTrue, gTrue );
188 aErrDoc.processLinks( pOutDev, i );
191 else
194 pdfi::PDFOutDev* pOutDev( new pdfi::PDFOutDev(&aDoc) );
196 // tell receiver early - needed for proper progress calculation
197 pOutDev->setPageNum( aDoc.getNumPages() );
199 // virtual resolution of the PDF OutputDev in dpi
200 static const int PDFI_OUTDEV_RESOLUTION=7200;
202 // do the conversion
203 const int nPages = aDoc.getNumPages();
204 for( int i=1; i<=nPages; ++i )
206 aDoc.displayPage( pOutDev,
208 PDFI_OUTDEV_RESOLUTION,
209 PDFI_OUTDEV_RESOLUTION,
210 0, gTrue, gTrue, gTrue );
211 aDoc.processLinks( pOutDev, i );
214 return 0;