Windows下打印PDF的Script

类别:编程语言 点击:0 评论:0 推荐:

为了能够使用户在下载系统Server生成的PDF文件后,自动打印。有一些第三方软件是可以直接打印PDF,但都需要购买。所以,写了下面一段Script,并且主要解决与客户端AcrobatReader的安装目录无关。


@ECHO OFF
ECHO Usage:  PRINTPDF  pdf_file
ECHO.
ECHO Where:  "pdf_file" is the name of the PDF file to be printed.
ECHO.
ECHO Notes:  This batch file works only if PDF files' default association is any
ECHO         Acrobat Reader version.
ECHO         Modification of the code to check for valid association by Chuck Hicks.
ECHO         Modification of the code to enable long file names by Michael Lintner.
SETLOCAL

:: Command line parsing
IF [%1]==[] GOTO Syntax
SET File2Print=%~1
ECHO.%File2Print% | FIND "?" >NUL
IF NOT ERRORLEVEL 1 GOTO Syntax
IF NOT EXIST "%File2Print%" GOTO Syntax

:: Strip quotes from TEMP variable
SET TEMP=%TEMP:"=%

:: Get the file association from the registry
START /WAIT REGEDIT.EXE /E "%TEMP%.\pdf.dat" "HKEY_CLASSES_ROOT\.pdf"
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\pdf.dat" ^| FIND "@="') DO SET FileType=%%B
SET Filetype=%FileType:"=%

:: Get the print command for this file type from the registry
START /WAIT REGEDIT.EXE /E "%TEMP%.\pdf.dat" "HKEY_CLASSES_ROOT\%FileType%\shell\print\command"
SET PrintCmd=
:: Check for valid association by blocking "{"; modification by Chuck Hicks.
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\pdf.dat" ^| FIND /V "{" ^| FIND "@="') DO SET PrintCmd=%%B
:: If no default was found, then Acrobat Writer may be installed
IF NOT DEFINED PrintCmd GOTO AcroWriter
SET PrintCmd=%PrintCmd:\"="%
SET PrintCmd=%PrintCmd:""="%
SET PrintCmd=%PrintCmd:\\=\%
:: Next line enables long file names, added by Michael Lintner
SET PrintCmd=%PrintCmd:"%1"=%%1%

:: Actual print command has to be stored in a temporary batch file,
:: which is used as the batch language's "eval" substitute
> "%TEMP%.\~PrnPdf.bat" ECHO START "Print" %PrintCmd%
CALL "%TEMP%.\~PrnPdf.bat" %File2Print%

:: Cleanup
DEL "%TEMP%.\pdf.dat"
DEL "%TEMP%.\~PrnPdf.bat"

:: Done
GOTO End

:AcroWriter
ECHO. Not finished
GOTO End

:Syntax
ECHO.
ECHO.
ECHO Written by Andy Chen
ECHO mailto:[email protected]
:End
ENDLOCAL

本文地址:http://com.8s8s.com/it/it25599.htm