-1

Background

  • My requirement is a feature to watermark PDF docs on demand within a LAMP stack (PHP 7.3).
  • The webapp has a collection of PDF docs loaded into a MySQL table. The docs originated as PDF files with various versions of PDF.
  • My watermarking solution is based on this article using FPDF.
  • I had the problem in this post.
  • So I added Ghostscript to the flow, to downgrade all docs to PDF 1.4.

Ghostscript Command

 gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=/tmp/63d319f2d1018.pdf /tmp/63d319f2cfb51.pdf

Error for one problem doc

  • For most docs, the flow is working and adding watermarks.
  • However, for one doc so far, Ghostscript fails as shown below.
  • The original PDF content, i.e. the input to GhostScript, renders fine in browsers and Acrobat Reader.
  • Here is the Ghostscript error (sorry for the formatting):

Error: /rangecheck in --makepattern-- Operand stack: --dict:11/20(L)-- --dict:14/23(L)-- --dict:14/23(L)-- --nostringval-- false --dict:14/15(L)-- --nostringval-- Execution stack: %interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1862 1 3 %oparray_pop 1861 1 3 %oparray_pop 1845 1 3 %oparray_pop --nostringval-- --nostringval-- 24 1 34 --nostringval-- %for_pos_int_continue --nostringval-- --nostringval-- false 1 %stopped_push --nostringval-- --nostringval-- --nostringval-- %array_continue --nostringval-- false 1 %stopped_push --nostringval-- %loop_continue --nostringval-- --nostringval-- --nostringval-- --nostringval-- 1811 4 10 %oparray_pop --nostringval-- Dictionary stack: --dict:1150/1684(ro)(G)-- --dict:1/20(G)-- --dict:76/200(L)-- --dict:76/200(L)-- --dict:106/127(ro)(G)-- --dict:286/300(ro)(G)-- --dict:22/25(L)-- --dict:4/6(L)-- --dict:25/40(L)-- --dict:3/5(L)-- Current allocation mode is local Last OS error: 2 GPL Ghostscript 8.70: Unrecoverable error, exit code 1 GPL Ghostscript 8.70: ERROR: A pdfmark destination page 34 points beyond the last page 22.

Is there a better Ghostscript command? Or a better approach?

badams
  • 98
  • 1
  • 9
  • I've updated the post indicating that this PDF doc is working otherwise. Maybe there is some "PDF lint" program to make it work in Ghostscript? – badams Jan 27 '23 at 22:59
  • The website and content are not public. Sorry, I can't share the specific document. – badams Jan 28 '23 at 00:01
  • You could try [PDFtk Server](https://www.pdflabs.com/tools/pdftk-server/) to uncompress and recompress the PDF. – Olivier Jan 28 '23 at 09:12
  • 1
    The problem isn't the 'pdfmark destination....', that occurs because an annotation has a destination which is a page number that doesn't exist in the document. Ghostscript simply ignores it (but tells you about it). Your real problem is the 'rangecheck in --makepattern'. This is almost certainly a bug in th einterpreter, and it has almost certainly been fixed. Try using a version of Ghostscript less than 13 years old! – KenS Jan 29 '23 at 09:49
  • @KenS Finally got back to this after time sharing with two other projects. I built the latest Ghostscript, and presto, the app works. – badams Feb 01 '23 at 03:12

1 Answers1

0

You ask if there is a better approach?
So yes, use the second more modern transparent watermark underlay system. By way of explanation here is that 1st type of watermark in the brand leader browser Edge/Chrome and same online file (without the watermark) in a competitive browser viewer where we can remove that annoying feature of obliterating contents. However in all cases GhostScript cannot work with broken files That file probably needs open in Acrobat to fix it. Also note watermarks can be removed by Browsers editors or any PDF Viewer/Editor.

enter image description here enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36
  • Sorry, I've edited the post to indicate that the original PDF doc, the input to Ghostscript, works fine in browsers and Acrobat. – badams Jan 27 '23 at 23:01