简体   繁体   中英

Ghostscript clips a pdf file

I'm trying to print a pdf with Ghostscript using those settings :

var switches = new List<string>
            {
                @"-empty",
                @"-dPrinted",
                @"-dNOPAUSE",
                @"-dNOSAFER",
                @"-dQUIET",
                @"-dPDFSETTINGS=/printer",
                @"-dNumCopies=1",
                @"-sDEVICE=mswinpr2",
                @"-dCompatibilityLevel=1.4",
                @"-sOutputFile=%printer%" + printerSettings.PrinterName,
                @"-f",
                pdfFileName
            };

but either the pdf or Ghostscript have bad margins and while it's good when I print it to file it clips when I print it on my printer.

Is there any way to add those programatically with Ghostscript ? I tried many different solutions from first pages of google but none of them work and they seem to have no effect on the printed pdf.

When I try to print it out with Adobe or IE it magicaly adds margins as soon as I choose the printer and it prints fine.

How to achive the same with Ghostscript ?

OK well the first thing is that many of the switches you are setting have no effect:

-empty isn't a Ghostscript-understood switch, and I'm slightly surprised it doesn't cause an error.

-dPDFSETTINGS only affects the pdfwrite device, which is why it is documented in the vector devices section.

-dCompatabilityLevel only affects the output of the pdfwrite device.

-dNOSAFER doesn't have any effect, as that's the default setting.

-f is used to 'close' direct PostScript insertion begun with -c, if you don't use -c you don't need -f

Now almost certainly neither Ghostscript nor your PDF has 'bad margins', the most likely explanation for your problem is that the printer you are using cannot print to the boundaries of the page, the left/right edges, and potentially the top and bottom edges are used by the paper transport mechanism, and the printer cannot print there.

In order to deal with that, you will need to reduce the size of the image, which you can 'probably' do by setting -dDEVICEWIDTHPOINTS and -dDEVICEHEIGHTPOINTS and -dFIXEDMEDIA. Its going to be up to you to work out the correct values for width and height.

Added after the comments below

There are two parts to this problem, the first is to deduce the size of the actual available area to print on, and to scale the output to that size. The second is to then reposition the output on the media so that it is all printed. If as you say the content is significantly smaller than the media then you can ignore rescaling it, but the entire solution is presented here for completeness.

Now as previously mentioned, the first part of this is achieved primarily by creating a fixed size canvas; this is done with any of the media selection switches and the addition of -dFIXEDMEDIA.

NOTE if you alter the media size, then you must, obviously, also alter the scale of the contents, or it won't fit. So you also need to set PSFitPage, EPSFitPage or PDFFitPage depending on the type of input (very recent versions of Ghostscript can use -dFitPage no matter what the input type).

As an experiment I used the file /ghostpdl/examples/text_graphic_image.pdf and sent the output to a printer on FILE:

This command line:

gswin32 -dDEVICEHEIGHTPOINTS=391 -dDEVICEWIDTHPOINTS=306 -dFIXEDMEDIA -sDEVICE=mswinpr2 -sOutputFile=%printer%KensPrinter /ghostpdl/examples/text_graphic_image.pdf

produces output where 3/4 the image is clipped away (the content lies off the newly defined fixed canvas size). If I modify that to:

gswin32 -dDEVICEHEIGHTPOINTS=391 -dDEVICEWIDTHPOINTS=306 -dFIXEDMEDIA -dPDFFitPage -sDEVICE=mswinpr2 -sOutptuFile=%printer%KensPrinter /ghostpdl/examples/text_graphic_image.pdf

then the result is a perfect reproduction of the original, at 1/4 the size (half in each direction).

So, the first thing you need to do is establish the actual printable area of the media on your printer, then you can set the width and height correctly as fixed media, and tell Ghostscript to scale the page to fit.

However, this will still leave the printed image at the bottom left of the media. Since that's in an area that can't be printed, you need to shift the printed image until its centred on the page. As I suggested, you can do this with a BeginPage procedure. This:

gswin32 -dDEVICEHEIGHTPOINTS=391 -dDEVICEWIDTHPOINTS=306 -dFIXEDMEDIA -dPDFFitPage -sDEVICE=mswinpr2 -sOutptuFile=%printer%KensPrinter -c "<</BeginPage {100 100 translate}>> setpagedevice" -f /ghostpdl/examples/text_graphic_image.pdf

produces output where the printed image is shifted up and right by 100 points each.

I believe that a little investigation will allow you to figure out where exactly your printer is able to print, and to create appropriately sized unprintable margins.

Note that, for me, the %printer% syntax does not result in a printer selection dialog. I suspect that your code (whatever language that is) is expanding the %p, resulting in a corruption of the name. Or possibly whatever you use to fork a Ghostscritp process does it. In either event you probably need to double the % signs.

You should get this working from the command line first, then work on getting it into an application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM