32

I'm trying to print all pdfs in current dir. When I call this bash script in cmd (singlepdf.sh): '"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe"' /t Gemeinde_348_BioID_842_alt.pdf everything's working fine.

When calling multiplepdfs.sh with this content:

declare -a pdfs=(*.pdf)

for pdf in ${pdfs[@]}; do
  echo -e "\nprinting **$pdf** with AcroRd32.exe...\n"
  '"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe"' /t $pdf
  sleep 3
done

The echo shows that files are addressed correctly in the loop - but then I get the error "C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe": No such file or directory

Can someone help out with this issue?

Edit: BTW, I have msys mingw installed

Kay
  • 2,702
  • 6
  • 32
  • 48

12 Answers12

49

I know this is and old question, but i was faced with the same problem recently and none of the answers worked for me:

  • Couldn't find an old Foxit Reader version
  • As @pilkch said 2Printer adds a report page
  • Adobe Reader opens a gui

After searching a little more i found this: http://www.columbia.edu/~em36/pdftoprinter.html.

It's a simple exe that you call with the filename and it prints to the default printer (or one that you specify). From the site:

PDFtoPrinter is a program for printing PDF files from the Windows command line. The program is designed generally for the Windows command line and also for use with the vDos DOS emulator.

To print a PDF file to the default Windows printer, use this command:

PDFtoPrinter.exe filename.pdf

To print to a specific printer, add the name of the printer in quotation marks:

PDFtoPrinter.exe filename.pdf "Name of Printer"

If you want to print to a network printer, use the name that appears in Windows print dialogs, like this (and be careful to note the two backslashes at the start of the name and the single backslash after the servername):

PDFtoPrinter.exe filename.pdf "\\SERVER\PrinterName"
scripts
  • 1,452
  • 1
  • 19
  • 24
  • I tried to use PDFtoPrinter.exe but it is being flagged by multiple virus scanners I have tried as a trojan. – Mike Cheel Mar 21 '19 at 19:21
  • 21
    I'm the author of PDFtoPrinter.exe. It's written in AutoIt, and some virus scanners flag any AutoIt executable as a trojan, because script-kiddies use AutoIt. Try submitting it to VirusTotal.com or viruscan.jotti.org for a more up-to-date report. – emendelson May 07 '19 at 15:21
  • @emendelson Is there some additional docs about command line references, access to source to have a look how things work on my own? I would like to look for a way to make `PDFtoPrinter.exe` itself sync, so that callers on the shell need to wait for everything to finish. – Thorsten Schöning Jul 07 '20 at 16:09
  • Absolutely everything is on the web page. Could you explain what you mean by "a way to make PDFtoPrinter.exe sync"? If you want to wait until it stops running before doing anything else, you could easily write an AutoIt script (or VBA or anything else) that tests whether the program is running, and waits until it isn't running. If you're asking for PDFtoPrinter.exe to allow only one instance of itself, I think I can do that. Just let me know. – emendelson Jul 08 '20 at 17:08
  • @ThorstenSchöning - please see comment above. I've now added a check for whether the program is already running. The program won't allow itself to be run twice at the same time when it's already running from the same full path. – emendelson Jul 08 '20 at 17:25
  • @emendelson With `sync` I meant a console application blocking further process on the shell until `PDFtoPrinter` itself finishes. It seems to detach from the console according my tests? Am I correct that `PDFtoPrinter` at least waits for the embedded `PDF-Xchange Viewer` to exit? Would be nice, though, if you would consider making things OSS, simply out of interest how exactly things are working. I'm dealing with automatic printing of arbitrary file types for years now and are therefore very interested in the different solutions people come up with. – Thorsten Schöning Jul 08 '20 at 17:48
  • 1
    @ThorstenSchöning - Yes, PDFtoPrinter uses AutoIt's "RunWait" to run the embedded PDF-Xchange Viewer. You're welcome to get in touch with me at the address at the foot of the page about this program; I'd rather clean up the absurd mess of AutoIt code before going public with it, but if you're willing to look at it privately, please get in touch. Meanwhile, I've posted a command-line-interface version named PDFtoPrinterCLI.exe (same site) which you can try. I have no idea whether it will help; please let me know. – emendelson Jul 08 '20 at 18:38
  • 1
    @ThorstenSchöning - also look at the debug option on my web page. If you use this, it shows how to run the print command, and you may not need my program at all. – emendelson Jul 08 '20 at 21:50
  • @emendelson this program is nice but is there any way to stop the shell or the calling process' window from flashing when you run it? I think it has to do with setting/restoring the focus of the window or something. – user3700562 Jun 15 '21 at 19:30
  • @user3700562 I haven't seen that happen, but you're entirely welcome to download the AutoIt source code from the download page and see what you can do. If you find a solution, please let me know and I'll add it to the code. – emendelson Jun 16 '21 at 20:07
  • I found it doesn't handle landscape PDF well. – guogangj Nov 29 '21 at 10:56
  • @emendelson It seems impossible to change the page size. I saw that info about using the exported settings `dat` file but it seems to be useless since PDF-XChange just changes printer's config when printing. How do I get it to print a PDF as is? If the PDF is A5 landscape, it should print in A5 landscape. Instead it prints and A4 portrait. – m4heshd Nov 30 '21 at 20:07
  • @m4heshd I'm afraid I have no idea. This sounds as if it's an issue in the PDF-XChange software, and you should probably ask for support from them. I only put a wrapper around their software. You might want to install their software directly on your machine and experiment. If you can't get the results you're looking for, ask the PDF-Xchange authors to fix it. There is absolutely nothing that I could possibly do. – emendelson Dec 03 '21 at 18:10
  • @emendelson Thank you for the response. I'm aware of everything you just mentioned but I just thought I might be missing the tiniest thing and stuck in a loop. But apparently there seem to be no way to change the page size. Even tried the direct CLI. – m4heshd Dec 03 '21 at 19:11
  • @m4heshd In the PDF-Xchange interface, in the Print dialog, do you have Choose paper source by PDF-page size checked? Try it with and without that option. If you can print to the correct page size in Acrobat Reader, you should be able to print to the correct page size in PDF-Xchange. Experiment with the settings in both. If you can't get it to print correctly from another application, it won't work here. – emendelson Dec 04 '21 at 20:50
  • hey @emendelson I've set up `pdfToPrinter` and managed to use it on my local machine - however calling it from an ssh session does not turn to print anything - would you be able to assist on debugging this strange behavior? – ggonmar Apr 18 '23 at 13:43
  • @ggonmar - I've never tried calling it from an ssh session, and wouldn't know where to begin debugging. I'm sorry - I wish I could help, but maybe someone else can suggest something? – emendelson Apr 18 '23 at 23:52
12

I had two problems with using Acrobat Reader for this task.

  1. The command line API is not officially supported, so it could change or be removed without warning.
  2. Send a print command to Reader loads up the GUI, with seemingly no way to prevent it. I needed the process to be transparent to the user.

I stumbled across this blog, that suggests using Foxit Reader. Foxit Reader is free, the API is almost identical to Acrobat Reader, but crucially is documented and does not load the GUI for print jobs.

A word of warning, don't just click through the install process without paying attention, it tries to install unrelated software as well. Why are software vendors still doing this???

kim3er
  • 6,306
  • 4
  • 41
  • 69
  • 9
    I could not get Foxit Reader (v7.0.6) to print silently in the background. It opens a minimized version of the GUI and does not return control until it is manually closed :( So I ended up using **SumatraPDF**, which has the command line switches **-print-to-default** (or **-print-to** _myprinter_) and **-silent** that allows for completely silent background printing! And the return code is 0 if it succeeds and 1 if it fails. Ref: [http://github.com/sumatrapdfreader/sumatrapdf/wiki/Command-line-arguments](http://github.com/sumatrapdfreader/sumatrapdf/wiki/Command-line-arguments) – Johan Dec 05 '14 at 14:59
  • Command-line printing worked as expected in Foxit v6.2, but seems to be broken in 7.0. – dlchambers Dec 29 '14 at 16:50
  • I can confirm that. Silent printing with Foxit 7.06 opens a the gui, Foxit 6.15 really prints silently – Ello Feb 27 '15 at 10:06
  • 1
    In addition to the issue outlined above in 7.06 and below, the switches have changed. Formerly, there was only `/t` for printing silently, and the printer name was optional. If the printer name was omitted, Foxit would send the file to the default printer. Now `/p` has been designated to send to the default printer, and `/t` **requires** there be a printer named. – wastubbs May 26 '15 at 14:50
  • 2
    SumatraPDF is trash if you want to print a landscape PDF on landscape media! Instead it decides to auto-rotate your PDF. – LittleTreeX Jul 31 '15 at 18:39
  • 1
    The issue with v7 has been confirmed as a bug http://forums.foxitsoftware.com/forum/portable-document-format-pdf-tools/foxit-reader/149617-print-pdf-via-command-line-and-exit-foxit-reader – David Anderson Dec 12 '15 at 22:39
  • 2
    Depending what you're doing, Adobe Reader License doesn't permit using it for headless or printing or printing from a server. And SumatraPDF 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. Adobe is smarter." – HK1 May 01 '17 at 19:30
  • @LittleTreeX Yup. It still does that. So annoying. – m4heshd Dec 01 '21 at 22:42
6

The error message is telling you.

Try just

"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe" /t "$pdf"

When you enclose the string in single-quotes, this makes everything inside a valid string, including the " chars. By removing the single-quotes, the shell will process the dbl-quotes as string "wrappers".

I would also wrap the filename variable in dbl-quotes so you can easily process files with spaces in their names, i.e.

"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe" /t "$pdf"

IHTH

shellter
  • 36,525
  • 7
  • 83
  • 90
  • I tried every type and variation of qoutes and the one in the op was the only one working outside the loop.. With single quotes adobe opens the file but strangely gives message along the lines "Error when opening file.." and does not send file to printer.. – Kay Oct 02 '13 at 07:16
  • Thanks for the edit, I was just posting to ask how you can run bash on Windows. The best I can recommend is to turn add `set -vx` near the top of your script so you can see the what is being executed, and how any what values are being used for variables. Also, you might try ANSI strings, with `$"c:\program ...."` or `$'c:\.....'` instead of just plain dbl or single quotes. Also add to your post the output of `bash --version`. Good luck. – shellter Oct 02 '13 at 09:05
  • or try having your `multipdf` script call `singlepdf` with the file argument? Good luck. – shellter Oct 02 '13 at 09:50
6

Looks like you are missing the printer name, driver, and port - in that order. Your final command should resemble:

AcroRd32.exe /t <file.pdf> <printer_name> <printer_driver> <printer_port>

For example:

"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /t "C:\Folder\File.pdf" "Brother MFC-7820N USB Printer" "Brother MFC-7820N USB Printer" "IP_192.168.10.110"

Note: To find the printer information, right click your printer and choose properties. In my case shown above, the printer name and driver name matched - but your information may differ.

rhp997
  • 191
  • 2
3

First response - wanted to finally give back to a helpful community...

Wanted to add this to the responses for people still looking for simple a solution. I'm using a free product by Foxit Software - FoxItReader.
Here is the link to the version that works with the silent print - newer versions the silent print feature is still not working. FoxitReader623.815_Setup

FOR %%f IN (*.pdf) DO ("C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe" /t %%f "SPST-SMPICK" %%f & del %%f) 

I simply created a command to loop through the directory and for each pdf file (FOR %%f IN *.pdf) open the reader silently (/t) get the next PDF (%%f) and send it to the print queue (SPST-SMPICK), then delete each PDF after I send it to the print queue (del%%f). Shashank showed an example of moving the files to another directory if that what you need to do

FOR %%X in ("%dir1%*.pdf") DO (move "%%~dpnX.pdf" p/)
2

I had the similar problem with printing multiple PDF files in a row and found only workaround by using 2Printer software. Command line example to print PDF files:

2Printer.exe -s "C:\In\*.PDF" -prn "HP LasetJet 1100"

It is free for non-commercial use at http://doc2prn.com/

Lei
  • 37
  • 1
  • 5
    The free version adds a report page to each page so is kind of useless for me as 50% of my printing would be a waste. – pilkch Dec 05 '15 at 03:52
  • As long as it works, we can buy it. But please make sure after buying it works. Can you please confirm its reliable and standard. we have to use it in Enterprises. –  Feb 19 '16 at 09:37
2

Using Acrobat reader is not a good solution, especially command line attributes are not documented. Additionally Acrobat reader's window stays open after printing process. PDF files are well known by printer drivers, so you may find better tools, like 2Printer.exe or RawFilePrinter.exe. In my opinion RawFilePrinter has better support and clear licencing process (you pay donation once and you can redistribute RawFilePrinter in many project you like - even new versions work with previously purchased license)

RawFilePrinter.exe -p "c:\Users\Me\Desktop\mypdffile.pdf" "Canon Printer" 
IF %ERRORLEVEL% 1(
    echo "Error!"
)

Latest version to download: http://bigdotsoftware.pl/index.php/rawfileprinter

Michael A. Schaffrath
  • 1,992
  • 1
  • 14
  • 23
zuko
  • 664
  • 1
  • 8
  • 16
2

The following batch script should achieve what you want. While it will leave an instance of Acrobat Reader running when finished, this will not cause any problems the next time this script is run.

@echo off
for %%f in (*.pdf) do (
  echo Printing %cd%\%%f with Adobe Acrobat Reader...
  start /b "Printing %%f" "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" /p /h "%cd%\%%f"
)

While you could separately kill the Acrobat Reader process afterwards there is the possibility this will close other PDF documents that are open that you didn't want closed.

richhallstoke
  • 1,519
  • 2
  • 16
  • 29
1

Here is another solution:

1) Download SumatraPDF (portable version) - https://www.sumatrapdfreader.org/download-free-pdf-viewer.html

2) Create a class library project and unzip the SumatraPDF.exe to the project directory root and unblock it.

3) Inside the project Properties, go to the Resoruces tab and add the exe as a file.

4) Add the following class to your library:

public class SumatraWrapper : IDisposable
{
    private readonly FileInfo _tempFileForExe = null;
    private readonly FileInfo _exe = null;

    public SumatraWrapper()
    {
        _exe = ExtractExe();
    }

    public SumatraWrapper(FileInfo tempFileForExe)
        : this()
    {
        _tempFileForExe = tempFileForExe ?? throw new ArgumentNullException(nameof(tempFileForExe));
    }

    private FileInfo ExtractExe()
    {
        string tempfile = 
            _tempFileForExe != null ? 
            _tempFileForExe.FullName : 
            Path.GetTempFileName() + ".exe";

        FileInfo exe = new FileInfo(tempfile);
        byte[] bytes = Properties.Resources.SumatraPDF;

        using (FileStream fs = exe.OpenWrite())
        {
            fs.Write(bytes, 0, bytes.Length);
        }

        return exe;
    }

    public bool Print(FileInfo file, string printerName)
    {
        string arguments = $"-print-to \"{printerName}\" \"{file.FullName}\"";
        ProcessStartInfo processStartInfo = new ProcessStartInfo(_exe.FullName, arguments)
        {
            CreateNoWindow = true
        };
        using (Process process = Process.Start(processStartInfo))
        {
            process.WaitForExit();
            return process.ExitCode == 0;
        }
    }

    #region IDisposable Support
    private bool disposedValue = false; // To detect redundant calls

    protected virtual void Dispose(bool disposing)
    {
        if (!disposedValue)
        {
            if (disposing)
            {
                // TODO: dispose managed state (managed objects).
            }

            // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
            // TODO: set large fields to null.
            try
            {
                File.Delete(_exe.FullName);
            }
            catch
            {

            }

            disposedValue = true;
        }
    }

    // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
    // ~PdfToPrinterWrapper() {
    //   // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
    //   Dispose(false);
    // }

    // This code added to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
        Dispose(true);
        // TODO: uncomment the following line if the finalizer is overridden above.
        // GC.SuppressFinalize(this);
    }
    #endregion
}

5) Enjoy printing pdf files from your code.

Use like this:

FileInfo file = new FileInfo(@"c:\Sandbox\dummy file.pdf");
SumatraWrapper pdfToPrinter =
    new SumatraWrapper();
pdfToPrinter.Print(file, "My Printer");
Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
0
@ECHO off set "dir1=C:\TicketDownload" 
FOR %%X in ("%dir1%*.pdf") DO ( "C:\Program Files (x86)\Adobe\Reader 9.0\Reader\AcroRd32.exe" /t "%%~dpnX.pdf" "Microsoft XPS Document Writer" ) 
FOR %%X in ("%dir1%*.pdf") DO (move "%%~dpnX.pdf" p/)

Try this..May be u have some other version of Reader so that is the problem..

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Shashank Bhat
  • 101
  • 2
  • 11
0

Today I was looking for this very solution and I tried PDFtoPrinter which I had an issue with (the PDFs I tried printing suggested they used incorrect paper size which hung the print job and nothing else printed until resolved). In my effort to find an alternative, I remembered GhostScript and utilities associated with it. I found GSView and it's associated program GSPrint (reference https://www.ghostscript.com/). Both these require GhostScript (https://www.ghostscript.com/) but when all the components are installed, GSPrint worked flawlessly and I was able to create a scheduled task that printed PDFs automatically overnight.

-2

Another solution "out of the box"

FOR %X in ("*.pdf") DO (C:\Windows\System32\print.exe /d:"\\printername" "%X.pdf")

Edit : As mentionned by "huysentruitw", this only works for txt files ! Sorry !

When I double checked i realized I'm using GhostScript, as "Multiverse IT" proposed. It looks like so :

"C:\Program Files (x86)\gs\gs\bin\gswin32c.exe" -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=mswinpr2 -sOutputFile="%printer%My-Printer-Name" "c:\My-Pdf-File.pdf"
Julien
  • 19
  • 3