1

I want to print a pdf file with selected printer name via Ghostscript.NET. This application is actually a wrapper of CefSharp browser. When user click on a download button, it download and print out with selected printer directly.

Visual studio with print

You can see the screenshot above, I pass the printer name thru URL query string. Also, I have set the option -dNOPAUSE & -dNOPROMPT. But why it still come out a print window, and the default selected printer is not what I have set?

Any idea what's going wrong here?

EDIT: Mar 4, 2016 11:31am

For ghostscript in command line, it will be like

"C:\Program Files (x86)\gs\gs9.18\bin\gswin32c.exe"^
 -empty^
 -dPrinted^
 -dBATCH^
 -dNOPAUSE^
 -dNOPROMPT^
 -sFONTPATH=C:\Windows\Fonts^
 -dNOSAFER^
 -dNumCopies=1^
 -sDEVICE=mswinpr2^
 -sOutputFile="%printer%Brother MFC-295CN Printer"^
 -f C:\Users\Dell\AppData\Local\Temp\file-tmp.pdf

but then it still show the print prompt.

HABJAN
  • 9,212
  • 3
  • 35
  • 59
Js Lim
  • 3,625
  • 6
  • 42
  • 80
  • 1
    Well, the URL and your log statement don't really have anything to do with it. Are you sure the syntax you're using for the output file is correct? Further, I'd be very surprised if it were *even possible* for an application to silently print to any printer it liked. – Rob Mar 04 '16 at 03:08
  • @Rob I'm new to windows form app. The syntax is correct, I even try ghostscript in cmd, but it still shows the prompt – Js Lim Mar 04 '16 at 03:35
  • Are you sure the printer name is 100% correct and it exists in the printer list ? – HABJAN Mar 04 '16 at 09:21

1 Answers1

3

Add -dQueryUser=3 to your command line to print directly to the default printer using that printer's default settings.

If the printer you are printing ot is not the Windows default printer, (on this system) then you must specify the printer in the print dialog. This is because there are 2 disconnected entities.

1) The printer you send the data to. 2) The creation of data in the correct format.

The mswinpr2 device uses the Windows print system to take the rendered output from Ghostscript and convert it into whatever the printer understands (PostScript, HP-PCL, Epson ESC/P etc, etc). It then sends that data to the named printer. NB this is why the driver for that printer must be available locally.

However, the mswinpr2 device doesn't know which printer device driver corresponds to a given named printer of the form "%printer%Name", you have to tell it that. If the named printer is the windows default printer then you can use -dQueryUser=3 to say so, otherwise you need to present the printer dialog so that the user can select the correct printer and that's what mswinpr2 uses to create the data that it then sends to the printer.

I believe that its perfectly possible to get this wrong! If you select a PostScript printer in the printer dialog, but then set -sOuputFile=%printer%HP-PCL printer" the combination will send output formatted as PostScript to a printer which expects PCL, the result in that case will be reams of garbage printout.

You need to exercise caution with the use of this device, which is why the default behaviour is to make you select the printer from the list of available printers.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • +1 for `-dQueryUser=3`. But what I want to achieve is different module print with different printer. When popup a dialog box, the user may choose the wrong printer for the module. Is there any alternative can achieve this? – Js Lim Mar 07 '16 at 09:37
  • Currently there is not. We looked into this on Sunday and someone is addressing the logical disconnect in the code now, with luck a fix will be committed in time for the next release. Since you mention you are creating an application, I'd just like to draw your attention to the licence for Ghostscript which is AGPL, so if you plan on distributing your app, you'll need to comply with the licence conditions (or seek a commercial licence) – KenS Mar 07 '16 at 13:08