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
33static const char *ownerPassword = "\001";
34static const char *userPassword = "\001";
35static const char *outputFile = "\001";
36static const char *options = "\001";
37
38int 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#if POPPLER_CHECK_VERSION(22, 6, 0)
142 PDFDoc aDoc( std::make_unique<GooString>(pFileName),
143 std::optional<GooString>(pOwnerPasswordStr),
144 std::optional<GooString>(pUserPasswordStr) );
145
146 PDFDoc aErrDoc( std::make_unique<GooString>(pErrFileName),
147 std::optional<GooString>(pOwnerPasswordStr),
148 std::optional<GooString>(pUserPasswordStr) );
149#else
150 PDFDoc aDoc( pFileName,
151 pOwnerPasswordStr,
152 pUserPasswordStr );
153
154 PDFDoc aErrDoc( pErrFileName,
155 pOwnerPasswordStr,
156 pUserPasswordStr );
157#endif
158
159 // Check various permissions for aDoc.
160 PDFDoc &rDoc = aDoc.isOk()? aDoc: aErrDoc;
161
162 pdfi::PDFOutDev aOutDev(&rDoc);
163 if (!strcmp(options, "SkipImages")) {
164 aOutDev.setSkipImages(true);
165 }
166
167 // tell the receiver early - needed for proper progress calculation
168 const int nPages = rDoc.isOk()? rDoc.getNumPages(): 0;
170
171 // virtual resolution of the PDF OutputDev in dpi
172 static const int PDFI_OUTDEV_RESOLUTION = 7200;
173
174 // do the conversion
175 for (int i = 1; i <= nPages; ++i)
176 {
177 rDoc.displayPage(&aOutDev,
178 i,
181 0, true, true, true);
182 rDoc.processLinks(&aOutDev, i);
183 }
184
185 return 0;
186}
187
188/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define _O_BINARY
void setSkipImages(bool bSkipImages)
static void setPageNum(int nNumPages)
int i
#define PDFI_OUTDEV_RESOLUTION
Definition: pdfihelper.hxx:38
static const char * options
Definition: wrapper_gpl.cxx:36
int main(int argc, char **argv)
Definition: wrapper_gpl.cxx:38
FILE * g_binary_out
Definition: wrapper_gpl.cxx:31
static const char * outputFile
Definition: wrapper_gpl.cxx:35
static const char * userPassword
Definition: wrapper_gpl.cxx:34
static const char * ownerPassword
Definition: wrapper_gpl.cxx:33