| 1 |
/** |
| 2 |
* SPDX-FileCopyrightText: (C) 2010 Dominik Seichter <[email protected]> |
| 3 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <podofo/private/PdfDeclarationsPrivate.h> |
| 7 |
#include <podofo/podofo.h> |
| 8 |
|
| 9 |
#include <cstdlib> |
| 10 |
#include <cstdio> |
| 11 |
|
| 12 |
#include <sstream> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#ifdef _WIN32 |
| 16 |
#include <podofo/private/WindowsLeanMean.h> |
| 17 |
#else |
| 18 |
# include <unistd.h> |
| 19 |
# include <sys/types.h> |
| 20 |
# include <sys/wait.h> |
| 21 |
#endif // _WIN32 |
| 22 |
|
| 23 |
using namespace std; |
| 24 |
using namespace PoDoFo; |
| 25 |
|
| 26 |
void print_help() |
| 27 |
{ |
| 28 |
printf("Usage: podofocrop input.pdf output.pdf\n"); |
| 29 |
printf(" This tool will crop all pages.\n"); |
| 30 |
printf(" It requires ghostscript to be in your PATH\n"); |
| 31 |
printf("\nPoDoFo Version: %s\n\n", PODOFO_VERSION_STRING); |
| 32 |
} |
| 33 |
|
| 34 |
void crop_page(PdfPage& page, const Rect& cropBox) |
| 35 |
{ |
| 36 |
PdfArray arr; |
| 37 |
/* |
| 38 |
printf("%f %f %f %f\n", |
| 39 |
rCropBox.X, |
| 40 |
rCropBox.Y, |
| 41 |
rCropBox.Width, |
| 42 |
rCropBox.Height); |
| 43 |
*/ |
| 44 |
cropBox.ToArray(arr); |
| 45 |
page.GetDictionary().AddKey("MediaBox", arr); |
| 46 |
} |
| 47 |
|
| 48 |
string get_ghostscript_output(const string_view& inputPath) |
| 49 |
{ |
| 50 |
string output; |
| 51 |
constexpr unsigned BufferLen = 256; |
| 52 |
char buffer[BufferLen]; |
| 53 |
|
| 54 |
#ifdef _WIN32 |
| 55 |
DWORD count; |
| 56 |
char cmd[BufferLen]; |
| 57 |
|
| 58 |
STARTUPINFOA si; |
| 59 |
PROCESS_INFORMATION pi; |
| 60 |
|
| 61 |
ZeroMemory(&si, sizeof(si)); |
| 62 |
si.cb = sizeof(si); |
| 63 |
ZeroMemory(&pi, sizeof(pi)); |
| 64 |
|
| 65 |
// Fenster nicht sichtbar |
| 66 |
si.dwFlags = STARTF_USESHOWWINDOW; |
| 67 |
si.wShowWindow = SW_HIDE; |
| 68 |
|
| 69 |
// Ausgabe umleiten |
| 70 |
HANDLE pipe; |
| 71 |
CreatePipe(&pipe, 0, 0, 0); |
| 72 |
si.dwFlags |= STARTF_USESTDHANDLES; |
| 73 |
//si.hStdOutput=pipe_wr; |
| 74 |
si.hStdError = pipe; |
| 75 |
|
| 76 |
_snprintf(cmd, BufferLen, "gs -dSAFER -sDEVICE=bbox -sNOPAUSE -q %s -c quit", inputPath.data()); |
| 77 |
printf("Running %s\n", cmd); |
| 78 |
if (!CreateProcessA(NULL, cmd, NULL, NULL, TRUE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &si, &pi)) |
| 79 |
{ |
| 80 |
printf("CreateProcess failed."); |
| 81 |
exit(1); |
| 82 |
} |
| 83 |
|
| 84 |
while (ReadFile(pipe, buffer, BufferLen, &count, NULL) && GetLastError() != ERROR_BROKEN_PIPE && count > 0) |
| 85 |
{ |
| 86 |
printf("%s", buffer); |
| 87 |
output.append(buffer, count); |
| 88 |
} |
| 89 |
|
| 90 |
// eigenes Handle schliessen |
| 91 |
CloseHandle(pipe); |
| 92 |
CloseHandle(pi.hProcess); |
| 93 |
CloseHandle(pi.hThread); |
| 94 |
#else |
| 95 |
pid_t pid; |
| 96 |
int p[2]; |
| 97 |
int count; |
| 98 |
|
| 99 |
pipe(p); |
| 100 |
|
| 101 |
if ((pid = fork()) == 0) |
| 102 |
{ |
| 103 |
// Child, launch ghostscript |
| 104 |
close(p[0]); // Close unused read end |
| 105 |
|
| 106 |
dup2(p[1], 2); // redirect stderr to stdout |
| 107 |
dup2(p[1], 1); |
| 108 |
|
| 109 |
//printf("HELLO\n"); |
| 110 |
execlp("gs", "gs", "-dSAFER", "-sDEVICE=bbox", |
| 111 |
"-sNOPAUSE", "-q", inputPath.data(), "-c", "quit", NULL); |
| 112 |
printf("Fatal error, cannot launch ghostscript\n"); |
| 113 |
exit(0); |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
close(p[1]); // Close unused write end |
| 118 |
while ((count = read(p[0], buffer, BufferLen)) > 0) |
| 119 |
{ |
| 120 |
output.append(buffer, count); |
| 121 |
} |
| 122 |
wait(NULL); |
| 123 |
} |
| 124 |
#endif // _WIN32 |
| 125 |
return output; |
| 126 |
} |
| 127 |
|
| 128 |
vector<Rect> get_crop_boxes(const string_view& input) |
| 129 |
{ |
| 130 |
vector<Rect> rects; |
| 131 |
string output = get_ghostscript_output(input); |
| 132 |
|
| 133 |
stringstream ss(output); |
| 134 |
string line; |
| 135 |
Rect curRect; |
| 136 |
bool haveRect = false; |
| 137 |
while (std::getline(ss, line)) |
| 138 |
{ |
| 139 |
if (strncmp("%%BoundingBox: ", line.c_str(), 15) == 0) |
| 140 |
{ |
| 141 |
int x, y, w, h; |
| 142 |
if (sscanf(line.c_str() + 15, "%i %i %i %i\n", &x, &y, &w, &h) != 4) |
| 143 |
{ |
| 144 |
printf("Failed to read bounding box's four numbers from '%s'\n", line.c_str() + 15); |
| 145 |
exit(1); |
| 146 |
} |
| 147 |
curRect = Rect(static_cast<double>(x), |
| 148 |
static_cast<double>(y), |
| 149 |
static_cast<double>(w - x), |
| 150 |
static_cast<double>(h - y)); |
| 151 |
haveRect = true; |
| 152 |
} |
| 153 |
else if (strncmp("%%HiResBoundingBox: ", line.c_str(), 17) == 0) |
| 154 |
{ |
| 155 |
if (haveRect) |
| 156 |
{ |
| 157 |
// I have no idea, while gs writes BoundingBoxes twice to stdout .. |
| 158 |
printf("Using bounding box: [ %f %f %f %f ]\n", |
| 159 |
curRect.X, |
| 160 |
curRect.Y, |
| 161 |
curRect.Width, |
| 162 |
curRect.Height); |
| 163 |
rects.push_back(curRect); |
| 164 |
haveRect = false; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return rects; |
| 170 |
} |
| 171 |
|
| 172 |
void Main(const cspan<string_view>& args) |
| 173 |
{ |
| 174 |
PdfCommon::SetMaxLoggingSeverity(PdfLogSeverity::None); |
| 175 |
|
| 176 |
if (args.size() != 3) |
| 177 |
{ |
| 178 |
print_help(); |
| 179 |
exit(-1); |
| 180 |
} |
| 181 |
|
| 182 |
auto inputPath = args[1]; |
| 183 |
auto outputPath = args[2]; |
| 184 |
|
| 185 |
printf("Cropping file:\t%s\n", inputPath.data()); |
| 186 |
printf("Writing to :\t%s\n", outputPath.data()); |
| 187 |
|
| 188 |
vector<Rect> cropBoxes = get_crop_boxes(inputPath); |
| 189 |
|
| 190 |
PdfMemDocument doc; |
| 191 |
doc.Load(inputPath); |
| 192 |
|
| 193 |
if (cropBoxes.size() != doc.GetPages().GetCount()) |
| 194 |
{ |
| 195 |
printf("Number of cropboxes obtained form ghostscript does not match with page count (%u, %u)\n", |
| 196 |
static_cast<unsigned>(cropBoxes.size()), doc.GetPages().GetCount()); |
| 197 |
PODOFO_RAISE_ERROR(PdfErrorCode::InvalidHandle); |
| 198 |
} |
| 199 |
|
| 200 |
for (unsigned i = 0; i < doc.GetPages().GetCount(); i++) |
| 201 |
{ |
| 202 |
auto& page = doc.GetPages().GetPageAt(i); |
| 203 |
crop_page(page, cropBoxes[i]); |
| 204 |
} |
| 205 |
|
| 206 |
doc.Save(outputPath); |
| 207 |
} |