简体   繁体   English

从 Windows 命令行打印 PDF

[英]Printing PDFs from Windows Command Line

I'm trying to print all pdfs in current dir.我正在尝试打印当前目录中的所有 pdf。 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.当我在 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.sh with 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

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找不到旧的 Foxit Reader 版本
  • As @pilkch said 2Printer adds a report page正如@pilkch 所说,2Printer 添加了一个报告页面
  • Adobe Reader opens a gui 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).这是一个使用文件名调用的简单 exe,它会打印到默认打印机(或您指定的打印机)。 From the site:从网站:

PDFtoPrinter is a program for printing PDF files from the Windows command line. PDFtoPrinter 是一个用于从 Windows 命令行打印 PDF 文件的程序。 The program is designed generally for the Windows command line and also for use with the vDos DOS emulator.该程序通常是为 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"

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.命令行 API 不受官方支持,因此它可能会在没有警告的情况下更改或删除。
  2. Send a print command to Reader loads up the GUI, with seemingly no way to prevent it.向 Reader 发送打印命令会加载 GUI,但似乎无法阻止它。 I needed the process to be transparent to the user.我需要这个过程对用户透明。

I stumbled across this blog , that suggests using Foxit Reader .我偶然发现了这个博客,它建议使用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 是免费的,其 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???为什么软件供应商还在这样做???

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.在我上面显示的情况下,打印机名称和驱动程序名称匹配 - 但您的信息可能不同。

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, ie我还会将文件名变量包装在 dbl 引号中,以便您可以轻松处理名称中带有空格的文件,即

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

IHTH IHTH

I had the similar problem with printing multiple PDF files in a row and found only workaround by using 2Printer software.我在连续打印多个 PDF 文件时遇到了类似的问题,只能通过使用 2Printer 软件找到解决方法。 Command line example to print PDF files:打印 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/

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.我正在使用 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 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).我只是创建了一个命令来循环遍历目录,并为每个 pdf 文件 (FOR %%f IN *.pdf) 静默打开阅读器 (/t) 获取下一个 PDF (%%f) 并将其发送到打印队列 ( SPST-SMPICK),然后在我将其发送到打印队列后删除每个 PDF (del%%f)。 Shashank showed an example of moving the files to another directory if that what you need to do Shashank 展示了将文件移动到另一个目录的示例,如果您需要这样做的话

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

Using Acrobat reader is not a good solution, especially command line attributes are not documented.使用 Acrobat reader 不是一个好的解决方案,尤其是没有记录命令行属性。 Additionally Acrobat reader's window stays open after printing process.此外,Acrobat 阅读器的窗口在打印过程后保持打开状态。 PDF files are well known by printer drivers, so you may find better tools, like 2Printer.exe or RawFilePrinter.exe. PDF 文件在打印机驱动程序中广为人知,因此您可能会找到更好的工具,例如 2Printer.exe 或 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 有更好的支持和清晰的许可流程(您只需支付一次捐赠,您就可以在许多您喜欢的项目中重新分发 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

@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..试试这个..也许你有一些其他版本的阅读器所以这就是问题所在..

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

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");

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.虽然它会在完成后让 Acrobat Reader 的实例继续运行,但这不会在下次运行此脚本时造成任何问题。

@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.虽然之后您可以单独终止 Acrobat Reader 进程,但这可能会关闭您不想关闭的其他打开的 PDF 文档。

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 !编辑:正如“huysentruitw”所提到的,这仅适用于 txt 文件! Sorry !对不起!

When I double checked i realized I'm using GhostScript , as "Multiverse IT" proposed.当我仔细检查时,我意识到我正在使用GhostScript ,正如“Multiverse IT”所提议的那样。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM