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