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 5 : 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 : static const char *options = "\001";
32 :
33 5 : int main(int argc, char **argv)
34 : {
35 5 : int k = 0;
36 26 : while (k < argc)
37 : {
38 16 : if (!strcmp(argv[k], "-f"))
39 : {
40 0 : outputFile = argv[k+1];
41 0 : argc -= 2;
42 0 : for (int j = k; j < argc; ++j)
43 0 : argv[j] = argv[j+2];
44 : }
45 16 : else if (!strcmp(argv[k], "-o"))
46 : {
47 1 : options = argv[k+1];
48 1 : argc -= 2;
49 1 : for (int j = k; j < argc; ++j)
50 0 : argv[j] = argv[j+2];
51 : }
52 :
53 15 : else if (!strcmp(argv[k], "-opw"))
54 : {
55 0 : ownerPassword = argv[k+1];
56 0 : argc -= 2;
57 0 : for (int j = k; j < argc; ++j)
58 0 : argv[j] = argv[j+2];
59 : }
60 15 : else if (!strcmp(argv[k], "-upw"))
61 : {
62 0 : userPassword = argv[k+1];
63 0 : argc -= 2;
64 0 : for (int j = k; j < argc; ++j)
65 0 : argv[j] = argv[j+2];
66 : }
67 16 : ++k;
68 : }
69 :
70 : // read config file
71 5 : globalParams = new GlobalParams();
72 5 : globalParams->setErrQuiet(gTrue);
73 : #if defined(_MSC_VER)
74 : globalParams->setupBaseFonts(NULL);
75 : #endif
76 :
77 : // try to read a possible open password form stdin
78 : char aPwBuf[129];
79 5 : aPwBuf[128] = 0;
80 5 : if( ! fgets( aPwBuf, sizeof(aPwBuf)-1, stdin ) )
81 0 : aPwBuf[0] = 0; // mark as empty
82 : else
83 : {
84 5 : for( unsigned int i = 0; i < sizeof(aPwBuf); i++ )
85 : {
86 5 : if( aPwBuf[i] == '\n' )
87 : {
88 5 : aPwBuf[i] = 0;
89 5 : break;
90 : }
91 : }
92 : }
93 :
94 : // PDFDoc takes over ownership for all strings below
95 5 : GooString* pFileName = new GooString(argv[1]);
96 5 : GooString* pErrFileName = new GooString(argv[2]);
97 :
98 : // check for password string(s)
99 5 : GooString* pOwnerPasswordStr( aPwBuf[0] != 0
100 0 : ? new GooString( aPwBuf )
101 5 : : (ownerPassword[0] != '\001'
102 0 : ? new GooString(ownerPassword)
103 10 : : nullptr ) );
104 5 : GooString* pUserPasswordStr( userPassword[0] != '\001'
105 0 : ? new GooString(userPassword)
106 5 : : nullptr );
107 5 : if( outputFile[0] != '\001' )
108 0 : g_binary_out = fopen(outputFile,"wb");
109 :
110 : #ifdef WNT
111 : // Win actually modifies output for O_TEXT file mode, so need to
112 : // revert to binary here
113 : _setmode( _fileno( g_binary_out ), _O_BINARY );
114 : #endif
115 :
116 : PDFDoc aDoc( pFileName,
117 : pOwnerPasswordStr,
118 5 : pUserPasswordStr );
119 :
120 : PDFDoc aErrDoc( pErrFileName,
121 : pOwnerPasswordStr,
122 10 : pUserPasswordStr );
123 :
124 : // Check various permissions for aDoc.
125 5 : PDFDoc &rDoc = aDoc.isOk()? aDoc: aErrDoc;
126 :
127 10 : pdfi::PDFOutDev aOutDev(&rDoc);
128 5 : if (!strcmp(options, "SkipImages")) {
129 1 : aOutDev.setSkipImages(true);
130 : }
131 :
132 : // tell the receiver early - needed for proper progress calculation
133 5 : const int nPages = rDoc.isOk()? rDoc.getNumPages(): 0;
134 5 : pdfi::PDFOutDev::setPageNum(nPages);
135 :
136 : // virtual resolution of the PDF OutputDev in dpi
137 : static const int PDFI_OUTDEV_RESOLUTION = 7200;
138 :
139 : // do the conversion
140 10 : for (int i = 1; i <= nPages; ++i)
141 : {
142 : rDoc.displayPage(&aOutDev,
143 : i,
144 : PDFI_OUTDEV_RESOLUTION,
145 : PDFI_OUTDEV_RESOLUTION,
146 5 : 0, gTrue, gTrue, gTrue);
147 5 : rDoc.processLinks(&aOutDev, i);
148 : }
149 :
150 10 : return 0;
151 15 : }
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|