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