0

When I use ghostscript in windows cmd with my setup.ps postscript file it prints my pdfs perfectly.

setup.ps

mark
/OutputFile (%printer%HP LaserJet 1018)
/BitsPerPixel  1 
/NoCancel false     
/UserSettings
   <<
      /DocumentName(document) 
      /MaxResolution 360  
   >>

(mswinpr2)finddevice 
putdeviceprops
setdevice
    <<
      /BeginPage {10 -55 translate}
    >>
setpagedevice

CommandLine

start /d "C:\Program Files (x86)\gs\gs9.19\bin" gswin32.exe -sOutputFile="%printer%HP LaserJet 1018" -dBATCH -dNOPAUSE -dFIXEDMEDIA setup.ps a.pdf

(I don't know why it needs sOutputFile in setup.ps and in commandline but it doesn't work otherwise)

Now when I put the same switches in my C# project which uses Ghostscript.NET wrapper.

  private static void CreateSetupPsFile(string printername)
    {
        const string Translationstring = @"{10 -15 translate}";
        string ps = $@"
                      mark
                /OutputFile (%printer%{printername})
                /BitsPerPixel  1 
                /NoCancel false     % don't show the cancel dialog
                /UserSettings
                    <<
                    /DocumentName(document) % name for the Windows spooler
                    /MaxResolution 360  
                    >>

                (mswinpr2)finddevice % select the Windows device driver
                putdeviceprops
              setdevice
                    <<
                    /PageOffset [30 -30] 
                    >>
                    setpagedevice";
        File.WriteAllText("setup.ps", ps);

    }

    private static void PrintA4(string pdfFileName, PrinterSettings printerSettings)
    {
        using (var processor = new GhostscriptProcessor(GsDll))
        {
            CreateSetupPsFile(printerSettings.PrinterName);
            var switches = new List<string>
            {
                $"-sOutputFile=\"%printer%{printerSettings.PrinterName}\"",
                @"-dBATCH",
                @"-dNOPAUSE",
                @"-dFixedMedia",
                "setup.ps",
                "-f",
                pdfFileName

            };
            processor.StartProcessing(switches.ToArray(), null);

    }
}

It totally ignores everything in the setup.ps file. Does anyone know why ? It just ignores and doesn't say what's wrong

Thank you in advance

Update

I managed to run some poscript... Apparently the wrapper needs the postscript to be given like that:

 var switches = new List<string>
                {

                    @"-dBATCH",
                    @"-dNOPAUSE",
                    @"-sDEVICE=mswinpr2",
                    $@"-sOutputFile=%printer%{printerSettings.PrinterName}",
                    "-c",
                    $"<</BeginPage {translateString}>> setpagedevice",
                    "-f",
                    pdfFileName

                };
                processor.StartProcessing(switches.ToArray(), null);

Not like that:

 var switches = new List<string>
                {

                    @"-dBATCH",
                    @"-dNOPAUSE",
                    @"-sDEVICE=mswinpr2",
                    $@"-sOutputFile=%printer%{printerSettings.PrinterName}",
                    $"-c <</BeginPage {translateString}>> setpagedevice -f",
                    pdfFileName

                };
                processor.StartProcessing(switches.ToArray(), null);

It's just unbelievable.

SebOlens
  • 519
  • 3
  • 15
  • Yes, you have to use -c to introduce PostScript, I hadn't realised you were doing that because your original example doesn't include that, it just runs setup.ps. Although the -f is not required (because you didn't use -c) it does no harm so I didn't bother to mention it. It does seem odd that the .NET code wants the switches separated, but I can't help you with that, I know nothing about the code there. – KenS Sep 08 '16 at 13:17
  • For now I'm just trying everything and also I realized the switches are **case sensitive**. I abandoned the solution with setup.ps and I'm trying to make it work.. somehow – SebOlens Sep 08 '16 at 13:30
  • That's because the switches are PostScript, and PostScript is case-sensitive.... – KenS Sep 08 '16 at 14:00

1 Answers1

0

How do you know its ignoring what's in setup.ps ?

Tray adding some debug into the PostScript program, eg

(Inside setup.ps) == flush

The first and most obvious thing I see is that you dealare that setup.ps contains :

<<
  /BeginPage {10 -55 translate}
>>
setpagedevice

Yet your code to create setup.ps contains:

                <<
                /PageOffset [30 -30] 
                >>
                setpagedevice";

Clearly those aren't the same which kind of makes me doubt that you are executing the PostScript code you think you are.

How did you get on with my answer to your previous question ?

Adding an example to do all the hacky setup.ps stuff without using setup.ps or the non-standard extensions.

Firstly you don't need to use finddevice, putdeviceprops or setdevice. Just set the device on the command line and use setpagedevice to set the properties. This is standard PostScript and the way the device is intended to be configured. Ghostscript may use its non-standard stuff behind the scenes, but you don't need to worry about that.

So something like:

gswin32 -sDEVICE=mswinpr2 -sOutputFile=%printer%PrinterName -c "<</BitsPerPixel 1 /NoCancel false /DocumentName (dummy) /MaxResolution 360 /BeginPage {10 10 translate}>> setpagedevice" -f PDF.pdf

Because you aren't using finddevice/putdeviceprops/setdevice you shouldn't need to mess about trying to set OutputFile twice. I'm assuming that you do want the BeginPage but not the PageOffset. I have no idea if you really want all those device-specific settings in there, since I don't know what printer you are using, sop I've just left them.

Obviously I can't test this, because I don't have your printer, but it ought to work. All that messing about in setup.ps is just bad news, I'd avoid it if at all possible.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Ahh yeah I put previous version of setup.ps there, my bad. What exactly (Inside setup.ps) == flush does ? Also where can I get some postscript tutorials because it all looks like gibberish to me. Anyway the problem is not bad settings but that the wrapper does nothing. It works all fine in the command line including the solutions from my previous question but the wrapper just completely ignores anything and prints the same exact stuff every single time.... – SebOlens Sep 08 '16 at 12:57
  • The main reference is the PostScript Language Reference Manual, there is also the Blue Book and Green Book (tutorial and cookbook). All these are available from the Adobe web site (somewhere, they keep changing it). I don't know what you mean by 'the wrapper'. If you mean setup.ps, then its not a wrapper, its a piece of program code. If you found my answer to your previous question useful then etiquette here is to accept the answer..... Again I come back to 'how do you know setup.ps does nothing ?' I can't see any evidence that its being executed. If its not, then it will 'do nothing' – KenS Sep 08 '16 at 13:02
  • I also wouldn't use all that mess you have in setup.ps, that uses Ghostscript-specific non-standard extensions to mess about with driver-specific settings. Unless you have some need for that I'd just drop it all. – KenS Sep 08 '16 at 13:04
  • by "the wrapper" I mean this : https://ghostscriptnet.codeplex.com/ Once again I repeat there is NOTHING wrong with the switches when they are run in commandline. The WRAPPER which is used in .NET to run ghostscript library gives me totally different output with the same input and clips everything – SebOlens Sep 08 '16 at 13:10
  • Right, but you haven't shown any evidence that 'the wrapper' is actually executing setup.ps. One good reason for avoiding its use. You don't need it, you can do all the setup stuff straight from the command line and without using all the hacky PostScript extensions. I'll add an example that does all this stuff (I believe) without it Thanks for accepting my previous answer :-) – KenS Sep 08 '16 at 13:14
  • Yeah but we would rather keep the wrapper and don't use the ghostscript as a process. – SebOlens Sep 08 '16 at 13:31
  • Just to be clear, you are aware of the Ghostscript licence ? Using the Ghostscript.NET code will mean you are linking against Ghostscript, so you need to be careful about the AGPL if you ever have any intention of distributing this code. – KenS Sep 08 '16 at 14:10