Bug 694852 - ps2write: Files differ only by paper size A4/Letter, A4 version prints duplex, Letter version not
Summary: ps2write: Files differ only by paper size A4/Letter, A4 version prints duplex...
Status: RESOLVED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: PS Writer (show other bugs)
Version: 9.10
Hardware: All All
: P4 major
Assignee: Ken Sharp
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-12-17 09:08 UTC by Till Kamppeter
Modified: 2013-12-18 06:56 UTC (History)
1 user (show)

See Also:
Customer:
Word Size: ---


Attachments
5003.PPD_Spec_v4.3.pdf (1022.14 KB, application/pdf)
2013-12-17 09:08 UTC, Till Kamppeter
Details
4-pages-letter-duplex-gs.ps (213.62 KB, application/postscript)
2013-12-17 09:10 UTC, Till Kamppeter
Details
4-pages-a4-duplex-gs.ps (213.62 KB, application/postscript)
2013-12-17 09:11 UTC, Till Kamppeter
Details
5003.PPD_Spec_v4.3.ps (9.31 MB, application/postscript)
2013-12-17 09:12 UTC, Till Kamppeter
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Till Kamppeter 2013-12-17 09:08:22 UTC
Created attachment 10468 [details]
5003.PPD_Spec_v4.3.pdf

I have an HP Color LaserJet CM3530 MFP. It is loaded with A4 paper but set up so that it also prints Letter-sized pages on thne A4 paper automatically.

Attached is a PDF file (5003.PPD_Spec_v4.3.pdf) which has all pages Letter-sized and which I convert into PostScript using Ghostscript 9.10 with the "ps2write" output device. I do not do any page size reformatting (no "-sPAPERSIZE=...", "-dPDFFitPage", ...). Command line is

gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=ps2write -sOUTPUTFILE=%stdout -dLanguageLevel=3 -r600 -dCompressFonts=false -dNoT3CCITT -dNOINTERPOLATE -c 'save pop' -f <input file>

The resulting file is post-processed, especially it gets

<</Duplex true /Tumble false>> setpagedevice

added to its Setup section.

The file is attached (4-pages-letter-duplex-gs.ps, only first 4 pages).

If I send this PostScript file to the printer without further filtering ("nc -w1 <host name or IP of printer> 9100 < 4-pages-letter-duplex-gs.ps"), it gets printed one-sided, though it contains the setpagedevice expression to tell the printer to print double-sided.

Now I edit 4-pages-letter-duplex-gs.ps replacing all occurences of (in the beginning of each page)

<</Type/Page/MediaBox [0 0 612 792]

by

<</Type/Page/MediaBox [0 0 595 842]

(changing the only page size settings in non-comment lines to A4). This is attached as 4-pages-a4-duplex-gs.ps and also contains only the first 4 pages. If I print this file unfiltered it comes out double-sided and I did not change anything in the duplex mode, I only changed the numbers for the paper size.

If I convert the original file with Poppler, conserving its Letter page size (attached 5003.PPD_Spec_v4.3.ps) I get double-sided output with Letter-sized pages. So the printer is principally capable of printing Letter-sized documents double-sided. In Poppler the setpagedevice expression for the paper size is only used when the page size differs from the previous page's size, especially if all pages have the same size, the setpagedevice for the page size is only used on the first page.

Probably Ghostscript issues the setpagedevice for the paper size on each page where the current page size differ's from the paper which the printer is using. It should not do so, even when the page size mismatches the size loaded into the printer it should issue a setpagedevice for the paper size only if the page sizes differs from the previous page's size of the PostScript document.

This was discussed between me, Chris Liddell and Ken Sharp on IRC:

<tkamppeter> chrisl, I have a problem with the PS output of GS with ps2write. On my HP Color LaserJet CM3530 MFP it prints only in duplex when it has page size setting A4 not when its page sizes are Letter. Poppler output prints duplex in both Letter and A4 format for me.
<kens> I'm not sure how we could be affecting that.
<kens> Except that possibly we do a setpagedevice on every page, which would defeat duplexing
<tkamppeter> kens, chrisl, I can mail you the files.
<kens> Well we're going to need to see them. THe simplest file possible would be best (empty pages)
* sojic (~sojic@92.55.124.152) has joined #ghostscript
<chrisl> I thought duplexing had been fixed a while back - maybe not. If we do a setpagedevice on each page, that would be less than great
<tkamppeter> kens, chrisl, you have the files in your mailbox now.
* TEvan (545e3c3d@gateway/web/freenode/ip.84.94.60.61) has joined #ghostscript
<kens> Looks to me like we (potentially) do a setpagedevice on every page, because the MediaBox is used and its 595x842
<tkamppeter> kens, chrisl, I have first generated the letter file from 5003.PPD_Spec_v4.3.pdf (official PPD documentation) with Ghostscript 9.10, ps2write device and I have even set the paper size to A4, buty GS uses the file's paper size.
<kens> Ghostscript always uses the MediaBox from teh PDF file, unelss you tell it to take specific action not to.
<kens> I'd really much rather have 2 blank pages than 4 pages filled with extrabeous stuff
<tkamppeter> kens, chrisl, for the A4 version I simply edited the letter version, changing the numbers in all MediaBox lines to the A4 size. The letter version does not print duplex, but the A4 version does. The [paper loaded in my printer is A4.
<kens> tkamppeter, I'mnot sure why this is a bug then.
<kens> If you have A4 media, and the jobrequests A4, tehn we won't issue a request to switch media size, because we are already using the correct size.
<tkamppeter> kens, chrisl, if I convert this PDF file usinmg Poppler, it prints duplex independent whether the page size is letter or A4.
<kens> That sounds to me more like a Poppler bug ;-)
<tkamppeter> kens, Poppler bug? Poppler does it the correct way.
<kens> Depends how you define correctness.
<kens> You have a printer with A4 media.
<kens> You run a job which requests dsuplex and A4
<kens> You get Duiplex.
<kens> You have a job which requests Letter, so the request cannot be matched
<kens> YTou don't get Duplex
<tkamppeter> kens, but in Poppler I can even print some internet-downloaded US manual file in duplex, but in GS I cannot do it.
<kens> I'm not at all sure that printing Duplex on A4 when the job requests duplex on Letter is 'correct'
<tkamppeter> kens, but is it then correct to print a letter job with duplex requested one-sided on A4?
<kens> Ordinarily I would suggest that you manually set the media to the printer media, and select PDFFitPage
<kens> tkamppeter we cannot know that the printer does not support letter
<kens> So we requestg letter when we see that we need it and teh current media is not letter
* sojic_ (~sojic@92.55.124.152) has joined #ghostscript
<kens> What if hte PDF file had different sized pages ?
<tkamppeter> kens, so I have to supply -dPDFFitPage to GS so that it sets the size of all pages to the requested paper size?
<kens> tkamppeter no
<kens> YOu need to szelect a *fixed* media size, and tehn have GS scale all pages to that fixed size, by adding PDFFitPage
<kens> Otherwise ps2write will request the media for each page, if it is not the same as the current media
<tkamppeter> kens, this would be something like "gs -sDEVICE=ps2write -sPAPERSIZE=a4 -dPDFFitPage ..."?
<kens> More accurately, the output from ps2write always requests that the iunterpreter uses media which matches the MediaBox. If this is already the media in use, then no change takes place
<kens> tkamppeter I suggest you try it
<chrisl> kens: does the opfread code interrogate the device before trying to set the page size to the MediaBox?
<kens> chrisl I don't believe so, it simply requests the media each time
<kens> If the requested media is the same as the current one then setpagedevice does nothing
<tkamppeter> kens, Poppler only compares the current page's size with the previous page's size and issues setpagedevice only if they differ.
<kens> tkamppeter, well we don't.
* sojic has quit (Ping timeout: 260 seconds)
<kens> We levae it up to the interpreter, which is what you're supposed to do
<tkamppeter> kens, would be great if you could do like in Poppler, at least with an option.
<kens> tkamppeter then open an enhancement bug request
<chrisl> kens: Hmm, then both *should* behave the same, surely, based on your testing with setpagedevice
<kens> chrisl I never tested with Duplex, distiller doesn't support it
<chrisl> kens: true, but IIRC your testing showed that even an empty dict caused the device to "change"
<kens> Only as far as the count of pages was concerned, I have no evidence that it reset duplex
<chrisl> It's not resetting duplex, but it will "defeat" duplex because a device change should cause the page to eject
<kens> That's what I mean, I have no evidence that it does so
<kens> All I was looking at was the PageCount
<chrisl> Hmm, ick.... I'd hate to think it was resetting random parts of the device.... :-(
<kens> Modifying opdfread to not emit a setpagedevice is not going to be trivial, it means storing the previous MediaBox and comparing with the new one.
<kens> I'm too busy to look at it just now tkamppeter, if you want me to please raise an enhancement bug or I will forget it
<tkamppeter> kens, OK.
<chrisl> tkamppeter: I was wondering if there was (or could be added) a way to add arbitrary command line options for when CUPS calls Ghostscript?
* maeve (6a440841@gateway/web/freenode/ip.106.68.8.65) has joined #ghostscript
<Robin_Watts_> https://github.com/kogmbh/WebODF
<chrisl> AGPL though
<kens> OK that's my P1 sent off to Henry, tkamppeter if you open that report for me I can have a look at it now (it took me less time than I expected to look at the bug)
<chrisl> kens: it's going to be a bit of a pain because the pages are surrounded by save/restores....
<kens> THat's going to make life difficult
<kens> As we will restore back to the previous interpreter state, and therefore page device
<chrisl> It probably means adding an initial setpagedevice during the job setup.
<kens> Yes I was thinking that
<kens> If we use the first page media that hsould be sufficient
* maeve has quit (Ping timeout: 272 seconds)
<kens> If the media changes after that, then we need to issue a new request anyway
<chrisl> Yes, which buggers duplex anyway
<kens> Yes true, but you can't reasonably duplex different sized pages
<kens> At least not without the kind of jiggering with media size and fitting that I described earlier
<chrisl> I doubt it will come up very often, and I think it is reasonable to ignore duplex under those circumstances
<kens> I don't plan to do any more than store the initial MediaBox and not execute setpagedevice if the requested one is the same. If we do execute setpagedevice then we will store the new MediaBox for further comparison.
<tkamppeter> kens, if a duplexed document has diffferent page sizes, at each change duplex has to be reset, if you have a document for example with 3x a4, then 2x A3, and then 3x A4, you get page 1 and 2 on the first sheet, page 3 on the 2nd sheet (with blank back), page 4 and 5 on the third sheet, page 6 and 7 on the 4th sheet and page 8 on the 5th sheet (with blank back). The first two sheets are A4, trhe 3rd is A3, and the last two are A4 again.
<kens> tkamppeter yes I'maaware of that
<kens> However, I believe that duplexing will survive this, as the request dictionary is merged with the current dictionary.
<kens> So if /Duplex is true and we execute a change in media, /Duplex will remain true.
<kens> So if you change media size, the duplex request will remain.
<kens> Of course, if you change to a medium where the printer does not support duplexing, and then back again, it will be lost.
<kens> To be honest this seems to me to be 'tough, that's how it works' expecting to maintain duplexing after changing media to one where duplexing is not supported is just plain daft
<tkamppeter> kens, the printer is most probably set to print Letter pages on A4 without asking the user and this works perfectly with Poppler. It duplexes the Letter pages from Poppler. So the printer shoul;d be able to duplex Letter.
<kens> tkamppeter the interpreter will use the Policy in force, which is usually 'select nearest and crop' or 'select nearest and scale'
<kens> If the PostScript program does not request another media change, then the duplexing works.
<kens> But if it does (because for example the PDF page is actually a different size) then duplexing is aborted.
<kens> If the new media supports duplexing in the printer then the the following pages will again be duplexed, if it does not then no further duplexing will take place, even if you switch back to the original media
<kens> chrisl actually it seems we do check the media agains the current media by executing currentpagedevice and we only execute setpagedevice if it is not the same
<chrisl> kens: yeh, I thought that - wasn't sure, the code seems overly complex for such a simple check....
<kens> It should be OK to extend that check against a stored media size and not bother if we have already requested that media.
Comment 1 Till Kamppeter 2013-12-17 09:10:17 UTC
Created attachment 10469 [details]
4-pages-letter-duplex-gs.ps
Comment 2 Till Kamppeter 2013-12-17 09:11:10 UTC
Created attachment 10470 [details]
4-pages-a4-duplex-gs.ps
Comment 3 Till Kamppeter 2013-12-17 09:12:32 UTC
Created attachment 10471 [details]
5003.PPD_Spec_v4.3.ps
Comment 4 Ken Sharp 2013-12-18 04:28:57 UTC
I believe commit 5ddd13eda3853d53315b7ba656ccd32ad9814c34 will fix this but I have no practical way to test it.
Comment 5 Till Kamppeter 2013-12-18 06:56:04 UTC
Works perfectly now, thank you very much for the quick fix.