Print PDF from VBscript or command line without acrobat.exe

18,184

Solution 1

I don't think you can print PDFs with VBScript alone. However, SumatraPDF should be able to do what you want. It's a standalone executable and you can print PDFs to the default printer using the -print-to-default option:

filename = "C:\path\to\some.pdf"

Set sh = CreateObject("WScript.Shell")
sh.Run "sumatrapdf.exe -print-to-default """ & filename & """", 0, True

See the manual for more details.

Solution 2

How about this:

Option Explicit

Const FILE_TO_PRINT = "C:\full\path\to\your\file.pdf"
Dim shl
Dim fldr
Dim files,file

Set shl = CreateObject("Shell.Application")
Set fldr = shl.Namespace("C:\full\path\to\your\")
Set files = fldr.Items


For Each file in files
  If LCase(file.Path) = LCase(FILE_TO_PRINT) Then
    file.InvokeVerbEx("Print")
  End If

Next

Set shl = Nothing
Set fldr = Nothing
Set files = Nothing
WScript.Quit

The Shell.Application object needs the folder that your file is in, and the constant FILE_TO_PRINT needs the full path to the file.

The InvokeVerbEx("Print") opens the file in the associated program like Foxit Reader or Acrobat, and sends it to the default printer.

It has the same effect as right-clicking a file in explorer and clicking 'Print'

Share:
18,184
Brian McGinity
Author by

Brian McGinity

love it, live it, do it

Updated on June 04, 2022

Comments

  • Brian McGinity
    Brian McGinity about 2 years

    Is it possible to print a pdf document to the default printer inside of vbscript or from the command line without AcroRd32.exe?

    Ideally if would be nice to just send the pdf to printer and not need another program.

    • or -

    Is there a 3rd party .exe program which can print the pdf without a dialogue and without opening and without showing in the Windows taskbar?

    The .exe needs to be standalone (so a windows install process is not required).

    I am ok paying up to $100 for the .exe as long as it can be distributed inside of another application. Free is also great.

    This has me stumped.

  • Brian McGinity
    Brian McGinity over 10 years
    This will open adobe, print the doc and keep adobe open on the taskbar. It is possible to then close adobe inside of the vbscript. The problem with this approach is that when adobe opens and closes the entire screen blinks.
  • Ansgar Wiechers
    Ansgar Wiechers over 10 years
    That will only work if some PDF-handling program providing a printing option is installed on the system.
  • Jobbo
    Jobbo over 10 years
    If you're worried about the screen blinking then I assume you're trying to find a solution that does this completely in the background?
  • Brian McGinity
    Brian McGinity over 10 years
    I tried SumantraPDF. It says "Cannot print this file." Sumantra is able to open the file and print within its GUI. Not sure why it will not work from the command line. I've tired PDFs from different sources and both give the same error. If Sumantra worked, this is exactly the type of thing I want.
  • Ansgar Wiechers
    Ansgar Wiechers over 10 years
    Does the system have a default printer configured?
  • Brian McGinity
    Brian McGinity over 10 years
    Hey hey!!!! This is working!!! Nice!! It needed needed the full path to the file. I guess I was expecting a 'file not found' error if that was the case. This is great.
  • Brian McGinity
    Brian McGinity over 10 years
    Yes, completely in the background.
  • Brian McGinity
    Brian McGinity over 10 years
    I was trying from within a dos shell and now just moved it into the vbscript and wow....this is fast. SumantraPDF is amazing.
  • HK1
    HK1 about 7 years
    Depending what you're doing, Adobe Reader License doesn't permit using it for headless or printing or printing from a server. And Go ahead and try Sumatra but be aware that it is slow if you're doing a lot of printing in a batch and you need speed. From the SumatraPDF forum: "In general Sumatra is not great at printing so I wouldn't recommend using it in [high volume scenarios]. Currently we print by generating a bitmap for each page and sending those bitmaps to a printer."