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?