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