0

I'm trying to produce new PDFs that alter dimensions only the first page (using CropBox). I used a modified version of How do I crop pages 3&4 in a multipage pdf using ghostscript

Here is what's strange: everything runs properly, but when I open the PDFs in typical applications (Preview, Acrobat, etc.), they either crash or I get a "Warning: Dimensions of Page May be Out of Range" error. In Acrobat, only one page will display, even tho page count is 2, 45, 60, or whatever.

Even stranger: I emailed the PDFs to someone to see if it was a machine-specific issue. In Gmail, everything looks fine in Google Apps's PDF viewer. So the process 'worked,' but it looks like there's something about the dimensions or page size that is throwing other apps off.

I've tried multiple GS options (dPDFFitPage, dPrinted=false, dUseCropBox, changing paper size to something other than legal), but nothing seems to work.

I'm attaching a version of a PDF that underwent this process and generates these errors as well. https://www.dropbox.com/s/ka13b7bvxmql4d2/imfwb.pdf?dl=0

Modified output is below. xmin, ymin, xmax, ymax, height, width are variables defined elsewhere in the bigger script of which GS is a part. Data are grabbed using pdfinfo

gs \
-o output/#{filename} \
-sDEVICE=pdfwrite \
-c \"<</EndPage {
0 eq {
  pop /Page# where {
    /Page# get
    1 eq {
      (page 1) == flush

      [/CropBox [#{xmin} #{ymin} #{xmax} #{ymax}] /PAGE pdfmark
      true
    }
    {
      (not page 1) == flush

      [/CropBox [0 #{height.to_f} #{width.to_f} #{height.to_f}] /PAGE pdfmark
      true
    } ifelse
  }{
    true
  } ifelse
}
{
  false
}
ifelse
}
>> setpagedevice\" \
 -f #{filename}"
`#{cmd}`
scee1787
  • 3
  • 3

1 Answers1

0

For pages after the first you set

[/CropBox [0 #{height.to_f} #{width.to_f} #{height.to_f}] /PAGE pdfmark

I.e. a crop box with zero height!

E.g. in case of your sample document page 2 has the crop box [0 792.0 612.0 792.0].

This surely is not what you want...

If you really want to "produce new PDFs that alter dimensions only the first page (using CropBox)", why do you change the crop box of later pages at all? Simply don't do anything in that case!


Why "Dimensions of Page May be Out of Range"?

Well, ISO 32000-1 in its normative Annex C declares:

The minimum page size should be 3 by 3 units in default user space

Thus, according to that older PDF specification a page height of 0 indeed is out of range for PDF!

Meanwhile, though, ISO 32000-2 has dropped that requirement, so strictly speaking a page height of zero should be nothing to complain about...

mkl
  • 90,588
  • 15
  • 125
  • 265
  • FWIW the supplied PDF file opens without complaint in Acrobat X. The second page is very very small, as mkl points out. I should also note (as usual) that by using Ghostscript's pdfwrite device in this way you are not merely altering (or adding) a CropBox, you are creating a totally new PDF file. – KenS Jan 08 '18 at 13:12
  • Thx to you both. mkl: re: the non-page 1's, that was my thinking as well. Would simply removing the CropBox line (under not page 1) be the right thing to do? I want to tell GS "just retain non page 1 as is." Re: coordinates, I relied on https://stackoverflow.com/questions/8158295/what-dimensions-do-the-coordinates-in-pdf-cropbox-refer-to but may be reading it wrong. Was under impression first # referred to positioning of lower left corner, not height. Is that not right? Thx again for your help. – scee1787 Jan 08 '18 at 20:35
  • *"Was under impression first # referred to positioning of lower left corner, not height. Is that not right?"* - the first two numbers are the coordinates of one corner of the box, the remaining two of another one, diagonally opposed. As your code sets the y coordinates of both points to the same value, the box had zero height. – mkl Jan 08 '18 at 23:03
  • *"Would simply removing the CropBox line (under not page 1) be the right thing to do?"* - I would assume so but I'm not really deep into gs or ps. – mkl Jan 08 '18 at 23:07