Bug 696986 - Add support for nested PostScript forms as nested Form XObjects in pdfwrite
Summary: Add support for nested PostScript forms as nested Form XObjects in pdfwrite
Status: RESOLVED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: PDF Writer (show other bugs)
Version: 9.18
Hardware: PC All
: P4 enhancement
Assignee: Ken Sharp
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-01 04:27 UTC by Terry Burton
Modified: 2021-10-30 08:11 UTC (History)
3 users (show)

See Also:
Customer:
Word Size: ---


Attachments
PS source with nested forms (21.66 KB, application/postscript)
2016-08-01 04:27 UTC, Terry Burton
Details
Distiller output (68.10 KB, application/pdf)
2016-08-01 04:28 UTC, Terry Burton
Details
GS output (93.97 KB, application/pdf)
2016-08-01 04:30 UTC, Terry Burton
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Terry Burton 2016-08-01 04:27:50 UTC
Created attachment 12781 [details]
PS source with nested forms

pdfwrite appears to generate incorrect output on documents with nested forms.

First page renders correctly, after which subsequent pages are incomplete.

Example PS attached...


(Not sure if this applies to other high-level drivers.)
Comment 1 Terry Burton 2016-08-01 04:28:28 UTC
Created attachment 12782 [details]
Distiller output
Comment 2 Terry Burton 2016-08-01 04:30:02 UTC
Created attachment 12783 [details]
GS output
Comment 3 Ken Sharp 2016-08-01 05:45:33 UTC
The pdfwrite device does not support nesting of form XObjects, because Forms are not commonly used in PostScript, are even less commonly used sensibly and still less so nested. Note that until relatively recently we didn't preserve PostScript Forms as PDF Form XObjects in pdfwrite at all.

If you want this to work you will have to use -dUNROLLFORMS, which will result in a PDF file where there are no Form XObjects, each iteration being drawn over again, just as it did before we introduced Forms support.
Comment 4 Terry Burton 2016-08-01 05:54:13 UTC
Hi Ken.

Thanks for the workaround. The PS code is for demonstration purposes anyway.

I'll accept a WONTFIX unless this bugs you enough to improve the entire form framework ;-)

All the best.
Comment 5 Ken Sharp 2016-08-01 06:08:47 UTC
(In reply to Terry Burton from comment #4)
> Hi Ken.
> 
> Thanks for the workaround. The PS code is for demonstration purposes anyway.
> 
> I'll accept a WONTFIX unless this bugs you enough to improve the entire form
> framework ;-)

I'd like to fix it, because on the rare occasions people do use forms sensibly it can make a big difference to the output size of the PDF file, but I won't get to it in the near future.

FWIW current code throws an error and suggests using -dUNROLLFORMS, this was (of course) added in 9.19 after someone complained that a file containing an invalid form didn't work:

-----------------------------------------------------------------------------
GPL Ghostscript GIT PRERELEASE 9.20 (2016-03-23)
Copyright (C) 2015 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Loading NimbusSans-Regular font from /ghostpdl/Resource/Font/NimbusSans-Regular.
.. 4053880 2473827 3047800 1737900 1 done.

  WARNING - Form PaintProc left operands on the stack after execution.
        This is technically illegal and these have been removed,
        if output is incorrect run again with -dUNROLLFORMS.


  WARNING - Form PaintProc left operands on the stack after execution.
        This is technically illegal and these have been removed,
        if output is incorrect run again with -dUNROLLFORMS.

Error: /stackunderflow in --.execform1--
Operand stack:
-----------------------------------------------------------------------------

Pedantically speaking the forms in your file don't leave junk on the stack, but the code for detecting that condition also finds the nested form and throws the warning. Because it also restores the stack to what it thinks is the correct condition it breaks the outer form (the inner processing has removed its form dictionary from the stack).

So usefully it tells you there's a problem and how to fix it, even if the warning is a little misleading, so I plan to leave it like that until I can look at the pdfwrite code to see if it can be made to begin a new Form XObject while its in the middle of an existing one......

If it can't then I'll probably opt to detect the condition and disable the special form processing for high level devices, at least that way the file will work.
Comment 6 Terry Burton 2016-08-01 06:28:30 UTC
Thanks for sharing your thoughts. Sounds like a good plan.
Comment 7 Terry Burton 2016-08-01 18:29:08 UTC
For future reference the latest, most-clear version of the problematic document will be kept here: https://github.com/bwipp/postscriptbarcode/blob/master/contrib/Examples/variable_data_printing/vdp.ps
Comment 8 Ken Sharp 2016-08-08 05:39:42 UTC
This turned out to be not so hard as I expected.

Commit 5f4319612f1837f89b9586a5695018d3554325d8 resolves the problem with current code throwing an error on nested forms.

Commit f7ffeb0319e112a1657987a9e5bad5b0f834a074 allows PDF output to contain
nested forms.

For 9.19 both of these commits are required, I suspect that 9.18 requires only the latter.

For me this produces a file 114,561 bytes in size, compared to the same file executed with -dUNROLLFORMS which comes in at 6,197,754 bytes and the Acrobat Distiller X output which is 91.216 bytes. Additionally the file executes many times faster with this commit.

Note that we still will not preserve forms used in a Pattern colour space, and will only preserve one level of Forms in PostScript output.
Comment 9 Terry Burton 2016-08-08 06:38:10 UTC
Awesome work. Tested and also works for me.

Thanks!
Comment 12 Ken Sharp 2021-10-30 08:11:06 UTC
User disabled due to spam, spam comment marked private to make it invisible