从 Windows 命令行打印 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19124808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:57:30  来源:igfitidea点击:

Printing PDFs from Windows Command Line

windowspdfprintingcmdsh

提问by Kay

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.pdfeverything's working fine.

我正在尝试打印当前目录中的所有 pdf。当我在 cmd ( singlepdf.sh) 中调用这个 bash 脚本时: '"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe"' /t Gemeinde_348_BioID_842_alt.pdf一切正常。

When calling multiplepdfs.shwith this content:

multiplepdfs.sh使用此内容调用时:

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

回声显示文件在循环中被正确寻址 - 但随后我收到错误 "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

编辑:顺便说一句,我安装了 msys mingw

回答by scripts

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
  • 找不到旧的 Foxit Reader 版本
  • 正如@pilkch 所说,2Printer 添加了一个报告页面
  • Adobe Reader 打开一个 gui

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

在搜索了更多之后,我发现了这个: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:

这是一个使用文件名调用的简单 exe,它会打印到默认打印机(或您指定的打印机)。从网站:

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.

PDFtoPrinter 是一个用于从 Windows 命令行打印 PDF 文件的程序。该程序通常是为 Windows 命令行设计的,也适用于 vDos DOS 模拟器。

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

要将 PDF 文件打印到默认的 Windows 打印机,请使用以下命令:

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):

如果要打印到网络打印机,请使用出现在 Windows 打印对话框中的名称,如下所示(并注意名称开头的两个反斜杠和服务器名称后的单个反斜杠):

PDFtoPrinter.exe filename.pdf "\SERVER\PrinterName"

回答by kim3er

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

我在使用 Acrobat Reader 执行此任务时遇到了两个问题。

  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.
  1. 命令行 API 不受官方支持,因此它可能会在没有警告的情况下更改或删除。
  2. 向 Reader 发送打印命令会加载 GUI,但似乎无法阻止它。我需要这个过程对用户透明。

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.

我偶然发现了这个博客,它建议使用Foxit Reader。Foxit Reader 是免费的,其 API 几乎与 Acrobat Reader 相同,但关键是有文档记录并且不会为打印作业加载 GUI。

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???

一个警告,不要只是点击安装过程而不注意,它也会尝试安装无关的软件。为什么软件供应商还在这样做???

回答by Michael Gargan

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

想将此添加到仍在寻找简单解决方案的人们的回复中。我正在使用 Foxit Software 的免费产品 - FoxItReader。
这是适用于无声打印的版本的链接 - 较新版本的无声打印功能仍然无法使用。 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

我只是创建了一个命令来循环遍历目录,并为每个 pdf 文件 (FOR %%f IN *.pdf) 静默打开阅读器 (/t) 获取下一个 PDF (%%f) 并将其发送到打印队列 ( SPST-SMPICK),然后在我将其发送到打印队列后删除每个 PDF (del%%f)。Shashank 展示了将文件移动到另一个目录的示例,如果您需要这样做的话

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

回答by Lei

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:

我在连续打印多个 PDF 文件时遇到了类似的问题,只能通过使用 2Printer 软件找到解决方法。打印 PDF 文件的命令行示例:

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

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

非商业用途免费,请访问http://doc2prn.com/

回答by zuko

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)

使用 Acrobat reader 不是一个好的解决方案,尤其是没有记录命令行属性。此外,Acrobat 阅读器的窗口在打印过程后保持打开状态。PDF 文件在打印机驱动程序中广为人知,因此您可能会找到更好的工具,例如 2Printer.exe 或 RawFilePrinter.exe。在我看来,RawFilePrinter 有更好的支持和清晰的许可流程(您只需支付一次捐赠,您就可以在许多您喜欢的项目中重新分发 RawFilePrinter - 即使是新版本也可以使用以前购买的许可证)

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

最新版本下载:http: //bigdotsoftware.pl/index.php/rawfileprinter

回答by shellter

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".

当您将字符串括在单引号中时,这会使所有内容都包含在有效字符串中,包括"字符。通过删除单引号,shell 会将 dbl 引号作为字符串“包装器”处理。

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

我还会将文件名变量包装在 dbl 引号中,以便您可以轻松处理名称中带有空格的文件,即

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

IHTH

IHTH

回答by rhp997

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.

注意:要查找打印机信息,请右键单击您的打印机并选择属性。在我上面显示的情况下,打印机名称和驱动程序名称匹配 - 但您的信息可能不同。

回答by Shashank Bhat

@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..

试试这个..也许你有一些其他版本的阅读器所以这就是问题所在..

回答by Multiverse IT

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.

今天我正在寻找这个解决方案,我尝试了 PDFtoPrinter,但我遇到了问题(我尝试打印的 PDF 建议他们使用不正确的纸张尺寸,这会导致打印作业挂起,并且在解决之前没有打印任何其他内容)。在我努力寻找替代方案的过程中,我想起了 GhostScript 和与之相关的实用程序。我找到了 GSView 和它的关联程序 GSPrint(参考https://www.ghostscript.com/)。这两个都需要 GhostScript ( https://www.ghostscript.com/) 但是当所有组件都安装好后, GSPrint 工作完美,我能够创建一个计划任务,在夜间自动打印 PDF。

回答by Mike Cheel

Here is another solution:

这是另一个解决方案:

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

1) 下载 SumatraPDF (便携版) - 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.

2)创建一个类库项目,将SumatraPDF.exe解压到项目目录根目录并解封。

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

3) 在项目属性中,转到资源选项卡并将 exe 添加为文件。

4) Add the following class to your library:

4) 将以下类添加到您的库中:

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.

5) 享受从您的代码打印 pdf 文件的乐趣。

Use like this:

像这样使用:

FileInfo file = new FileInfo(@"c:\Sandbox\dummy file.pdf");
SumatraWrapper pdfToPrinter =
    new SumatraWrapper();
pdfToPrinter.Print(file, "My Printer");