2
votes

I am trying to perform pdf to pdfa conversation through ghost script with -sDEVICE=pdfwrite. The conversation is successful but it first page is blank (rest pages are fine); also, adobe reader gives an error "There was an error processing a page. Wrong operand type."

Command:

cmd /c C:\app\others\GhostScript\9_21\bin\gswin64.exe -dPDFA=2 -dBATCH -dNOPAUSE -dNOPLATFONTS -dPDFSETTINGS=/printer -sProcessColorModel=DeviceRGB -sDEVICE=pdfwrite -dCompatibilityLevel=1.7 -dOptimize=true -dPDFACompatibilityPolicy=1 -dAutoRotatePages=/None -sOutputFile="1107.pdf" "test1.pdf"

Note: pdfa file can be read (first page as well) in pdf-xchange viewer, chrome browser. The problem is only with adobe reader.

input pdf: test1.pdf

output pdfa: 1107.pdf

1

1 Answers

2
votes

There are a number of problems with the command line you are using, I'll come to those at the end.

The first point to make is that you should always use current code. 9.21 is out of date, the current version is 9.23. When I run the file through the current version, using the command line supplied I get a number of warnings on stderr (or, since you are using the windowed executable, in the window):

GPL Ghostscript 9.23 (2018-03-21)
Copyright (C) 2018 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 12.
Page 1
GPL Ghostscript 9.23: Setting Overprint Mode to 1
 not permitted in PDF/A-2, overprint mode not set

Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
Attempting to write a DeviceN space with an inappropriate alternate,
have you set ColorConversionStrategy ?
>>showpage, press <return> to continue<<

So that pretty much tells you what's wrong, you haven't set ColorConversionStrategy. All the software which opens the file without complaint is incorrect. If you run the produced PDF file back through Ghostscript to the display it says:

GPL Ghostscript GIT PRERELEASE 9.24 (2018-03-21)
Copyright (C) 2018 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
   **** Error: Considering object with an invalid number -1 as null.
               Output may be incorrect.
   **** Error: Considering object with an invalid number -1 as null.
               Output may be incorrect.
   **** Error reading a content stream. The page may be incomplete.
               Output may be incorrect.
   **** Error: File did not complete the page properly and may be damaged.
               Output may be incorrect.
>>showpage, press <return> to continue<<

A little more poking, by setting -dPDFSTOPONERROR and -dPDFDEBUG gives:

%Resolving: [-1 0]
   **** Error: Considering object with an invalid number -1 as null.
               Output may be incorrect.
%Pattern: << /PaintProc {<< >> .pdfpaintproc} /PatternType 2 /.pattern_uses_tran
sparency false /Matrix [0.000766095 -0.000451741 -0.000306278 -0.000529551 116.3
78 788.13] /Shading {-1 0 resolveR} >>
%Resolving: [-1 0]
   **** Error: Considering object with an invalid number -1 as null.
               Output may be incorrect.
Error: /typecheck in --makepattern--
Operand stack:
   --dict:11/19(L)--   --dict:5/13(L)--   --dict:5/13(L)--   --nostringval--   f
alse   --nostringval--   0.0   --nostringval--   --nostringval--   --dict:5/6(L)
--   --nostringval--   --nostringval--   --nostringval--   DataSource
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval-
-   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   fa
lse   1   %stopped_push   2015   1   3   %oparray_pop   2014   1   3   %oparray_
pop   1998   1   3   %oparray_pop   --nostringval--   --nostringval--   2   1
1   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--
  --nostringval--   --nostringval--   %array_continue   --nostringval--   --nost
ringval--   %loop_continue   --nostringval--   --nostringval--   1958   4   11
 %oparray_pop   --nostringval--   --nostringval--   false   1   %stopped_push
--nostringval--
Dictionary stack:
   --dict:984/1684(ro)(G)--   --dict:1/20(G)--   --dict:83/200(L)--   --dict:83/
200(L)--   --dict:133/256(ro)(G)--   --dict:307/450(ro)(G)--   --dict:33/64(L)--
   --dict:6/9(L)--   --dict:7/20(L)--   --dict:1/1(ro)(G)--   --dict:1/1(ro)(G)-
-
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript GIT PRERELEASE 9.24: Unrecoverable error, exit code 1

Close this window with the close button on the title bar or the system menu.

So you can see that there's an object with an invalid number (-1) and a shading dictionary trying to use that object. That's flatly illegal.

Now, the reason for that is because of the options you've set to pdfwrite.

First thing to note, Ghostscript's pdfwrite device does not 'convert' PDF files. What happens is that the input is interpreted, converted into graphics primitives ready for rendering and then sent to the rendering pieline. However the pdfwrite device, instead of rendering the primitives, repackages them into a PDF file. There are a number of consequences of this which are described in the relevant documentation.

In order to create a PDF/A file, the output file must follow certain rules; it may not contain both RGB and CMYK colours, it can only contain one or the other. So the first thing you should do is set -sColorConversionStrategy to one of RGB, CMYK or UseDeviceIndependentColor . Setting the ProcessColorModel isn't sufficient. If you set ColorConversionStrategy then the ProcessColorModel is set for you automatically. This is the initial problem, fixing that produces a valid PDF file (but not a valid PDF/A file).

The PDF/A file must also contain an ICC Profile, the OutputIntent, unless the PDF file is solely composed of Gray or device-independent colours. Your command line doesn't do that.

The documentation, again, describes how to go about creating a PDF/A file.

Moving on from the basics, you set -dPDFSETTINGS. This is, in my opinion, a really bad idea, especially when trying to create a PDF/A file. Doing that will alter many settings, unless you are absolutely certain that you want all these settings set to the canned defaults you should not use it.

I wouldn't touch -dCompatibilityLevel, the pdfwrite device sets this appropriately for the level of conformance that it requires, based on what it writes to the output file. Unless you are going to add to the PDF file (using pdfmarks) constructs which require a higher level, all this does is restrict the file to being opened by more recent versions of Acrobat.

I wouldn't use -dOptimize, if for no other reason than the fact that it doesn't do anything! If you read the documentation then note 0 under Distiller params states that this can be set and queried, but has no effect.

The pdfwrite equivalent is -dFastWebView, but I still wouldn't use it, because its mostly useless, only speeds up loading of the first page at most, and only when the PDF consumer uses it, which most don't.