How to print a PDF

Hello, is it possible to send a file to the default printer to be printed?
I already generate it for PDF, but I wanted to send it to the printer right away.

You could perform a windows API (Shell Execute) to print the file.

The ShellExecute command takes a few parameters, one of which is the filename and the other the action - that action can be “print”.

This will cause the OS to print the file with the associated application (basically the same as right clicking in File Explorer and selecting Print).

Hope this helps,

Regards,

Andy
noyantis.com

1 Like

Thanks a lot for the help.
But I’m a little confused with the ShellExcute settings.
I don’t know which parameter to inform the file and the action.

I have the following signature in the project global:
ShellExecute(UNSIGNED,LONG,*CSTRING,LONG,*CSTRING,SIGNED),UNSIGNED,PASCAL,RAW,NAME(‘SHELLEXECUTEA’)

And the ShellExecute signature of win21.dll is different:

ShellExecuteA(
[in, optional] HWND hwnd,
[in, optional] LPCSTR lpOperation,
[in] LPCSTR lpFile,
[in, optional] LPCSTR lpParameters,
[in, optional] LPCSTR lpDirectory,
[in] INT nShowCmd

ShellExecute example here …

Try setting szOperation to ‘print’

1 Like

Seems like you want to do something like lpr, but I don’t know the best way to accomplish it. Maybe call it directly via cmd? lpr | Microsoft Learn

1 Like

It’s true, analyzing the suggestion given above, I found it a little complicated.
Do you know how to call ShellExecute through cmd for printing?
I couldn’t find how to make the call.

Sounds like this thread should be renamed : “How to print a PDF”.

We use the portable version of Sumatra PDF, it is a single 32bit .exe that I call using RUN() with command line parameters to print.

2 Likes

As I had never done anything like this, I didn’t have a starting point.

But I managed with the following solution, using Acrobat via cmd.
Very similar to Julian.

You need to add the path environment variable to the Acrobat DC installation path, the folder where Acrobat.exe is located, to be recognized by cmd.
The printout is sent to the default printer.

Command line options:
Acrobat.exe /s = no splash screen
Acrobat.exe /h = hidden
Acrobat.exe /p = print to default printer

Acrobat.exe /s /h /p MyPdfFile.pdf

To send to a specific printer, follow the command.
Acrobat.exe /t filename.pdf printername drivername portname
printername – The name of your printer.
drivername – Your printer driver’s name. Whatever appears in the Driver Used box when you view your printer’s properties.
portname – The printer’s port. portname cannot contain any “/” characters; if it does, output is routed to the default port for that printer.

How do I change the title of the question?

I have changed it as suggested. (Perhaps you need special privileges to do this?? I had a pencil/edit icon next to the title). *** edit: see Jeff’s answer below

1 Like

There is at least 1 badge that some folks have that let them rename. For example:

image

2 Likes

And look at that, I got more likes! :slight_smile:

2 Likes

In older versions Acrobat.exe /p used to print without UI, but in later versions direct print was not supported anymore, but i have not checked this in a couple of years.

Some additional options for other people reading this topic:

  1. If the printer driver understands PostScript (e.g. HP PS driver) you can use GhostScript to convert a PDF to PS and copy it directly to the printer. Like: open file LPT1 write to fle, close file
  2. Some printers understand PDF. This means you can just write PDF contents to the printer LPT file
    n.b. These are heavy printer and printer driver dependent!

I ended up using Windows Printer/dib API’s because customer kept complaining printing did not work on some printers.