1 //========================================================================
2 //
3 // pdftops.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 // Modified for Debian by Hamish Moffatt, 22 May 2002.
8 //
9 //========================================================================
10
11 //========================================================================
12 //
13 // Modified under the Poppler project - http://poppler.freedesktop.org
14 //
15 // All changes made under the Poppler project to this file are licensed
16 // under GPL version 2 or later
17 //
18 // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19 // Copyright (C) 2007-2008, 2010, 2015, 2017, 2018, 2020, 2021 Albert Astals Cid <aacid@kde.org>
20 // Copyright (C) 2009 Till Kamppeter <till.kamppeter@gmail.com>
21 // Copyright (C) 2009 Sanjoy Mahajan <sanjoy@mit.edu>
22 // Copyright (C) 2009, 2011, 2012, 2014-2016, 2020 William Bader <williambader@hotmail.com>
23 // Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
24 // Copyright (C) 2012 Thomas Freitag <Thomas.Freitag@alfa.de>
25 // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp>
26 // Copyright (C) 2014, 2017 Adrian Johnson <ajohnson@redneon.com>
27 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
28 // Copyright (C) 2019, 2021 Oliver Sander <oliver.sander@tu-dresden.de>
29 // Copyright (C) 2020 Philipp Knechtges <philipp-dev@knechtges.com>
30 // Copyright (C) 2021 Hubert Figuiere <hub@figuiere.net>
31 //
32 // To see a description of the changes please see the Changelog file that
33 // came with your tarball or type make ChangeLog if you are building from git
34 //
35 //========================================================================
36
37 #include "config.h"
38 #include <poppler-config.h>
39 #include <cstdio>
40 #include <cstdlib>
41 #include <cstddef>
42 #include <cstring>
43 #include "parseargs.h"
44 #include "goo/GooString.h"
45 #include "goo/gmem.h"
46 #include "GlobalParams.h"
47 #include "Object.h"
48 #include "Stream.h"
49 #include "Array.h"
50 #include "Dict.h"
51 #include "XRef.h"
52 #include "Catalog.h"
53 #include "Page.h"
54 #include "PDFDoc.h"
55 #include "PDFDocFactory.h"
56 #include "PSOutputDev.h"
57 #include "Error.h"
58 #include "Win32Console.h"
59 #include "sanitychecks.h"
60
61 #ifdef USE_CMS
62 # include <lcms2.h>
63 #endif
64
setPSPaperSize(char * size,int & psPaperWidth,int & psPaperHeight)65 static bool setPSPaperSize(char *size, int &psPaperWidth, int &psPaperHeight)
66 {
67 if (!strcmp(size, "match")) {
68 psPaperWidth = psPaperHeight = -1;
69 } else if (!strcmp(size, "letter")) {
70 psPaperWidth = 612;
71 psPaperHeight = 792;
72 } else if (!strcmp(size, "legal")) {
73 psPaperWidth = 612;
74 psPaperHeight = 1008;
75 } else if (!strcmp(size, "A4")) {
76 psPaperWidth = 595;
77 psPaperHeight = 842;
78 } else if (!strcmp(size, "A3")) {
79 psPaperWidth = 842;
80 psPaperHeight = 1190;
81 } else {
82 return false;
83 }
84 return true;
85 }
86
87 static int firstPage = 1;
88 static int lastPage = 0;
89 static bool level1 = false;
90 static bool level1Sep = false;
91 static bool level2 = false;
92 static bool level2Sep = false;
93 static bool level3 = false;
94 static bool level3Sep = false;
95 static bool origPageSizes = false;
96 static bool doEPS = false;
97 static bool doForm = false;
98 #ifdef OPI_SUPPORT
99 static bool doOPI = false;
100 #endif
101 static int splashResolution = 0;
102 static bool psBinary = false;
103 static bool noEmbedT1Fonts = false;
104 static bool noEmbedTTFonts = false;
105 static bool noEmbedCIDPSFonts = false;
106 static bool noEmbedCIDTTFonts = false;
107 static bool fontPassthrough = false;
108 static bool optimizeColorSpace = false;
109 static bool passLevel1CustomColor = false;
110 static char rasterAntialiasStr[16] = "";
111 static char forceRasterizeStr[16] = "";
112 static bool preload = false;
113 static char paperSize[15] = "";
114 static int paperWidth = -1;
115 static int paperHeight = -1;
116 static bool noCrop = false;
117 static bool expand = false;
118 static bool noShrink = false;
119 static bool noCenter = false;
120 static bool duplex = false;
121 static char ownerPassword[33] = "\001";
122 static char userPassword[33] = "\001";
123 static bool quiet = false;
124 static bool printVersion = false;
125 static bool printHelp = false;
126 static bool overprint = false;
127 static GooString processcolorformatname;
128 static SplashColorMode processcolorformat;
129 static bool processcolorformatspecified = false;
130 #ifdef USE_CMS
131 static GooString processcolorprofilename;
132 static GfxLCMSProfilePtr processcolorprofile;
133 static GooString defaultgrayprofilename;
134 static GfxLCMSProfilePtr defaultgrayprofile;
135 static GooString defaultrgbprofilename;
136 static GfxLCMSProfilePtr defaultrgbprofile;
137 static GooString defaultcmykprofilename;
138 static GfxLCMSProfilePtr defaultcmykprofile;
139 #endif
140
141 static const ArgDesc argDesc[] = { { "-f", argInt, &firstPage, 0, "first page to print" },
142 { "-l", argInt, &lastPage, 0, "last page to print" },
143 { "-level1", argFlag, &level1, 0, "generate Level 1 PostScript" },
144 { "-level1sep", argFlag, &level1Sep, 0, "generate Level 1 separable PostScript" },
145 { "-level2", argFlag, &level2, 0, "generate Level 2 PostScript" },
146 { "-level2sep", argFlag, &level2Sep, 0, "generate Level 2 separable PostScript" },
147 { "-level3", argFlag, &level3, 0, "generate Level 3 PostScript" },
148 { "-level3sep", argFlag, &level3Sep, 0, "generate Level 3 separable PostScript" },
149 { "-origpagesizes", argFlag, &origPageSizes, 0, "conserve original page sizes" },
150 { "-eps", argFlag, &doEPS, 0, "generate Encapsulated PostScript (EPS)" },
151 { "-form", argFlag, &doForm, 0, "generate a PostScript form" },
152 #ifdef OPI_SUPPORT
153 { "-opi", argFlag, &doOPI, 0, "generate OPI comments" },
154 #endif
155 { "-r", argInt, &splashResolution, 0, "resolution for rasterization, in DPI (default is 300)" },
156 { "-binary", argFlag, &psBinary, 0, "write binary data in Level 1 PostScript" },
157 { "-noembt1", argFlag, &noEmbedT1Fonts, 0, "don't embed Type 1 fonts" },
158 { "-noembtt", argFlag, &noEmbedTTFonts, 0, "don't embed TrueType fonts" },
159 { "-noembcidps", argFlag, &noEmbedCIDPSFonts, 0, "don't embed CID PostScript fonts" },
160 { "-noembcidtt", argFlag, &noEmbedCIDTTFonts, 0, "don't embed CID TrueType fonts" },
161 { "-passfonts", argFlag, &fontPassthrough, 0, "don't substitute missing fonts" },
162 { "-aaRaster", argString, rasterAntialiasStr, sizeof(rasterAntialiasStr), "enable anti-aliasing on rasterization: yes, no" },
163 { "-rasterize", argString, forceRasterizeStr, sizeof(forceRasterizeStr), "control rasterization: always, never, whenneeded" },
164 { "-processcolorformat", argGooString, &processcolorformatname, 0, "color format that is used during rasterization and transparency reduction: MONO8, RGB8, CMYK8" },
165 #ifdef USE_CMS
166 { "-processcolorprofile", argGooString, &processcolorprofilename, 0, "ICC color profile to use as the process color profile during rasterization and transparency reduction" },
167 { "-defaultgrayprofile", argGooString, &defaultgrayprofilename, 0, "ICC color profile to use as the DefaultGray color space" },
168 { "-defaultrgbprofile", argGooString, &defaultrgbprofilename, 0, "ICC color profile to use as the DefaultRGB color space" },
169 { "-defaultcmykprofile", argGooString, &defaultcmykprofilename, 0, "ICC color profile to use as the DefaultCMYK color space" },
170 #endif
171 { "-optimizecolorspace", argFlag, &optimizeColorSpace, 0, "convert gray RGB images to gray color space" },
172 { "-passlevel1customcolor", argFlag, &passLevel1CustomColor, 0, "pass custom color in level1sep" },
173 { "-preload", argFlag, &preload, 0, "preload images and forms" },
174 { "-paper", argString, paperSize, sizeof(paperSize), "paper size (letter, legal, A4, A3, match)" },
175 { "-paperw", argInt, &paperWidth, 0, "paper width, in points" },
176 { "-paperh", argInt, &paperHeight, 0, "paper height, in points" },
177 { "-nocrop", argFlag, &noCrop, 0, "don't crop pages to CropBox" },
178 { "-expand", argFlag, &expand, 0, "expand pages smaller than the paper size" },
179 { "-noshrink", argFlag, &noShrink, 0, "don't shrink pages larger than the paper size" },
180 { "-nocenter", argFlag, &noCenter, 0, "don't center pages smaller than the paper size" },
181 { "-duplex", argFlag, &duplex, 0, "enable duplex printing" },
182 { "-opw", argString, ownerPassword, sizeof(ownerPassword), "owner password (for encrypted files)" },
183 { "-upw", argString, userPassword, sizeof(userPassword), "user password (for encrypted files)" },
184 { "-overprint", argFlag, &overprint, 0, "enable overprint emulation during rasterization" },
185 { "-q", argFlag, &quiet, 0, "don't print any messages or errors" },
186 { "-v", argFlag, &printVersion, 0, "print copyright and version info" },
187 { "-h", argFlag, &printHelp, 0, "print usage information" },
188 { "-help", argFlag, &printHelp, 0, "print usage information" },
189 { "--help", argFlag, &printHelp, 0, "print usage information" },
190 { "-?", argFlag, &printHelp, 0, "print usage information" },
191 {} };
192
main(int argc,char * argv[])193 int main(int argc, char *argv[])
194 {
195 std::unique_ptr<PDFDoc> doc;
196 GooString *fileName;
197 GooString *psFileName;
198 PSLevel level;
199 PSOutMode mode;
200 GooString *ownerPW, *userPW;
201 PSOutputDev *psOut;
202 bool ok;
203 int exitCode;
204 bool rasterAntialias = false;
205 std::vector<int> pages;
206 #ifdef USE_CMS
207 cmsColorSpaceSignature profilecolorspace;
208 #endif
209
210 Win32Console win32Console(&argc, &argv);
211 exitCode = 99;
212
213 // parse args
214 ok = parseArgs(argDesc, &argc, argv);
215 if (!ok || argc < 2 || argc > 3 || printVersion || printHelp) {
216 fprintf(stderr, "pdftops version %s\n", PACKAGE_VERSION);
217 fprintf(stderr, "%s\n", popplerCopyright);
218 fprintf(stderr, "%s\n", xpdfCopyright);
219 if (!printVersion) {
220 printUsage("pdftops", "<PDF-file> [<PS-file>]", argDesc);
221 }
222 if (printVersion || printHelp)
223 exit(0);
224 else
225 exit(1);
226 }
227 if ((level1 ? 1 : 0) + (level1Sep ? 1 : 0) + (level2 ? 1 : 0) + (level2Sep ? 1 : 0) + (level3 ? 1 : 0) + (level3Sep ? 1 : 0) > 1) {
228 fprintf(stderr, "Error: use only one of the 'level' options.\n");
229 exit(1);
230 }
231 if ((doEPS ? 1 : 0) + (doForm ? 1 : 0) > 1) {
232 fprintf(stderr, "Error: use only one of -eps, and -form\n");
233 exit(1);
234 }
235 if (level1) {
236 level = psLevel1;
237 } else if (level1Sep) {
238 level = psLevel1Sep;
239 } else if (level2Sep) {
240 level = psLevel2Sep;
241 } else if (level3) {
242 level = psLevel3;
243 } else if (level3Sep) {
244 level = psLevel3Sep;
245 } else {
246 level = psLevel2;
247 }
248 if (doForm && level < psLevel2) {
249 fprintf(stderr, "Error: forms are only available with Level 2 output.\n");
250 exit(1);
251 }
252 mode = doEPS ? psModeEPS : doForm ? psModeForm : psModePS;
253 fileName = new GooString(argv[1]);
254
255 // read config file
256 globalParams = std::make_unique<GlobalParams>();
257 if (origPageSizes) {
258 paperWidth = paperHeight = -1;
259 }
260 if (paperSize[0]) {
261 if (origPageSizes) {
262 fprintf(stderr, "Error: -origpagesizes and -paper may not be used together.\n");
263 exit(1);
264 }
265 if (!setPSPaperSize(paperSize, paperWidth, paperHeight)) {
266 fprintf(stderr, "Invalid paper size\n");
267 delete fileName;
268 goto err0;
269 }
270 }
271 if (quiet) {
272 globalParams->setErrQuiet(quiet);
273 }
274
275 if (!processcolorformatname.toStr().empty()) {
276 if (processcolorformatname.toStr() == "MONO8") {
277 processcolorformat = splashModeMono8;
278 processcolorformatspecified = true;
279 } else if (processcolorformatname.toStr() == "CMYK8") {
280 processcolorformat = splashModeCMYK8;
281 processcolorformatspecified = true;
282 } else if (processcolorformatname.toStr() == "RGB8") {
283 processcolorformat = splashModeRGB8;
284 processcolorformatspecified = true;
285 } else {
286 fprintf(stderr, "Error: Unknown process color format \"%s\".\n", processcolorformatname.c_str());
287 goto err1;
288 }
289 }
290
291 #ifdef USE_CMS
292 if (!processcolorprofilename.toStr().empty()) {
293 processcolorprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(processcolorprofilename.c_str(), "r"));
294 if (!processcolorprofile) {
295 fprintf(stderr, "Error: Could not open the ICC profile \"%s\".\n", processcolorprofilename.c_str());
296 goto err1;
297 }
298 if (!cmsIsIntentSupported(processcolorprofile.get(), INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT) && !cmsIsIntentSupported(processcolorprofile.get(), INTENT_ABSOLUTE_COLORIMETRIC, LCMS_USED_AS_OUTPUT)
299 && !cmsIsIntentSupported(processcolorprofile.get(), INTENT_SATURATION, LCMS_USED_AS_OUTPUT) && !cmsIsIntentSupported(processcolorprofile.get(), INTENT_PERCEPTUAL, LCMS_USED_AS_OUTPUT)) {
300 fprintf(stderr, "Error: ICC profile \"%s\" is not an output profile.\n", processcolorprofilename.c_str());
301 goto err1;
302 }
303 profilecolorspace = cmsGetColorSpace(processcolorprofile.get());
304 if (profilecolorspace == cmsSigCmykData) {
305 if (!processcolorformatspecified) {
306 processcolorformat = splashModeCMYK8;
307 processcolorformatspecified = true;
308 } else if (processcolorformat != splashModeCMYK8) {
309 fprintf(stderr, "Error: Supplied ICC profile \"%s\" is a CMYK profile, but process color format is not CMYK8.\n", processcolorprofilename.c_str());
310 goto err1;
311 }
312 } else if (profilecolorspace == cmsSigGrayData) {
313 if (!processcolorformatspecified) {
314 processcolorformat = splashModeMono8;
315 processcolorformatspecified = true;
316 } else if (processcolorformat != splashModeMono8) {
317 fprintf(stderr, "Error: Supplied ICC profile \"%s\" is a monochrome profile, but process color format is not monochrome.\n", processcolorprofilename.c_str());
318 goto err1;
319 }
320 } else if (profilecolorspace == cmsSigRgbData) {
321 if (!processcolorformatspecified) {
322 processcolorformat = splashModeRGB8;
323 processcolorformatspecified = true;
324 } else if (processcolorformat != splashModeRGB8) {
325 fprintf(stderr, "Error: Supplied ICC profile \"%s\" is a RGB profile, but process color format is not RGB.\n", processcolorprofilename.c_str());
326 goto err1;
327 }
328 }
329 }
330 #endif
331
332 if (processcolorformatspecified) {
333 if (level1 && processcolorformat != splashModeMono8) {
334 fprintf(stderr, "Error: Setting -level1 requires -processcolorformat MONO8");
335 goto err1;
336 } else if ((level1Sep || level2Sep || level3Sep || overprint) && processcolorformat != splashModeCMYK8) {
337 fprintf(stderr, "Error: Setting -level1sep/-level2sep/-level3sep/-overprint requires -processcolorformat CMYK8");
338 goto err1;
339 }
340 }
341
342 #ifdef USE_CMS
343 if (!defaultgrayprofilename.toStr().empty()) {
344 defaultgrayprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultgrayprofilename.c_str(), "r"));
345 if (!checkICCProfile(defaultgrayprofile, defaultgrayprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigGrayData)) {
346 goto err1;
347 }
348 }
349 if (!defaultrgbprofilename.toStr().empty()) {
350 defaultrgbprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultrgbprofilename.c_str(), "r"));
351 if (!checkICCProfile(defaultrgbprofile, defaultrgbprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigRgbData)) {
352 goto err1;
353 }
354 }
355 if (!defaultcmykprofilename.toStr().empty()) {
356 defaultcmykprofile = make_GfxLCMSProfilePtr(cmsOpenProfileFromFile(defaultcmykprofilename.c_str(), "r"));
357 if (!checkICCProfile(defaultcmykprofile, defaultcmykprofilename.c_str(), LCMS_USED_AS_INPUT, cmsSigCmykData)) {
358 goto err1;
359 }
360 }
361 #endif
362
363 // open PDF file
364 if (ownerPassword[0] != '\001') {
365 ownerPW = new GooString(ownerPassword);
366 } else {
367 ownerPW = nullptr;
368 }
369 if (userPassword[0] != '\001') {
370 userPW = new GooString(userPassword);
371 } else {
372 userPW = nullptr;
373 }
374 if (fileName->cmp("-") == 0) {
375 delete fileName;
376 fileName = new GooString("fd://0");
377 }
378
379 doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
380
381 if (userPW) {
382 delete userPW;
383 }
384 if (ownerPW) {
385 delete ownerPW;
386 }
387 if (!doc->isOk()) {
388 exitCode = 1;
389 goto err1;
390 }
391
392 #ifdef ENFORCE_PERMISSIONS
393 // check for print permission
394 if (!doc->okToPrint()) {
395 error(errNotAllowed, -1, "Printing this document is not allowed.");
396 exitCode = 3;
397 goto err1;
398 }
399 #endif
400
401 // construct PostScript file name
402 if (argc == 3) {
403 psFileName = new GooString(argv[2]);
404 } else if (fileName->cmp("fd://0") == 0) {
405 error(errCommandLine, -1, "You have to provide an output filename when reading from stdin.");
406 goto err1;
407 } else {
408 const char *p = fileName->c_str() + fileName->getLength() - 4;
409 if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
410 psFileName = new GooString(fileName->c_str(), fileName->getLength() - 4);
411 } else {
412 psFileName = fileName->copy();
413 }
414 psFileName->append(doEPS ? ".eps" : ".ps");
415 }
416
417 // get page range
418 if (firstPage < 1) {
419 firstPage = 1;
420 }
421 if (lastPage < 1 || lastPage > doc->getNumPages()) {
422 lastPage = doc->getNumPages();
423 }
424 if (lastPage < firstPage) {
425 error(errCommandLine, -1, "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).", firstPage, lastPage);
426 goto err2;
427 }
428
429 // check for multi-page EPS or form
430 if ((doEPS || doForm) && firstPage != lastPage) {
431 error(errCommandLine, -1, "EPS and form files can only contain one page.");
432 goto err2;
433 }
434
435 for (int i = firstPage; i <= lastPage; ++i) {
436 pages.push_back(i);
437 }
438
439 // write PostScript file
440 psOut = new PSOutputDev(psFileName->c_str(), doc.get(), nullptr, pages, mode, paperWidth, paperHeight, noCrop, duplex, /*imgLLXA*/ 0, /*imgLLYA*/ 0,
441 /*imgURXA*/ 0, /*imgURYA*/ 0, psRasterizeWhenNeeded, /*manualCtrlA*/ false, /*customCodeCbkA*/ nullptr, /*customCodeCbkDataA*/ nullptr, level);
442 if (noCenter) {
443 psOut->setPSCenter(false);
444 }
445 if (expand) {
446 psOut->setPSExpandSmaller(true);
447 }
448 if (noShrink) {
449 psOut->setPSShrinkLarger(false);
450 }
451 if (overprint) {
452 psOut->setOverprintPreview(true);
453 }
454
455 if (rasterAntialiasStr[0]) {
456 if (!GlobalParams::parseYesNo2(rasterAntialiasStr, &rasterAntialias)) {
457 fprintf(stderr, "Bad '-aaRaster' value on command line\n");
458 }
459 }
460
461 if (forceRasterizeStr[0]) {
462 PSForceRasterize forceRasterize = psRasterizeWhenNeeded;
463 if (strcmp(forceRasterizeStr, "whenneeded") == 0) {
464 forceRasterize = psRasterizeWhenNeeded;
465 } else if (strcmp(forceRasterizeStr, "always") == 0) {
466 forceRasterize = psAlwaysRasterize;
467 } else if (strcmp(forceRasterizeStr, "never") == 0) {
468 forceRasterize = psNeverRasterize;
469 } else {
470 fprintf(stderr, "Bad '-rasterize' value on command line\n");
471 }
472 psOut->setForceRasterize(forceRasterize);
473 }
474
475 if (splashResolution > 0) {
476 psOut->setRasterResolution(splashResolution);
477 }
478 if (processcolorformatspecified)
479 psOut->setProcessColorFormat(processcolorformat);
480 #ifdef USE_CMS
481 psOut->setDisplayProfile(processcolorprofile);
482 psOut->setDefaultGrayProfile(defaultgrayprofile);
483 psOut->setDefaultRGBProfile(defaultrgbprofile);
484 psOut->setDefaultCMYKProfile(defaultcmykprofile);
485 #endif
486 psOut->setEmbedType1(!noEmbedT1Fonts);
487 psOut->setEmbedTrueType(!noEmbedTTFonts);
488 psOut->setEmbedCIDPostScript(!noEmbedCIDPSFonts);
489 psOut->setEmbedCIDTrueType(!noEmbedCIDTTFonts);
490 psOut->setFontPassthrough(fontPassthrough);
491 psOut->setPreloadImagesForms(preload);
492 psOut->setOptimizeColorSpace(optimizeColorSpace);
493 psOut->setPassLevel1CustomColor(passLevel1CustomColor);
494 #ifdef OPI_SUPPORT
495 psOut->setGenerateOPI(doOPI);
496 #endif
497 psOut->setUseBinary(psBinary);
498
499 psOut->setRasterAntialias(rasterAntialias);
500 if (psOut->isOk()) {
501 for (int i = firstPage; i <= lastPage; ++i) {
502 doc->displayPage(psOut, i, 72, 72, 0, noCrop, !noCrop, true);
503 }
504 } else {
505 delete psOut;
506 exitCode = 2;
507 goto err2;
508 }
509 delete psOut;
510
511 exitCode = 0;
512
513 // clean up
514 err2:
515 delete psFileName;
516 err1:
517 delete fileName;
518 err0:
519
520 return exitCode;
521 }
522