@echo off :: ---------------------------------------------------------------------------- :: pretty_pdf.bat :: ---------------------------------------------------------------------------- :: Copyright (c) 2008, Andrew Strotheide (andrew@strotheide.com) :: All rights reserved. :: :: Redistribution and use in source and binary forms, with or without :: modification, are permitted provided that the following conditions are met: :: :: * Redistributions of source code must retain the above copyright notice :: this list of conditions and the following disclaimer. :: * Redistributions in binary form must reproduce the above copyright :: notice, this list of conditions and the following disclaimer in the :: documentation and/or other materials provided with the distribution. :: * Neither the name of Andrew Strotheide nor the names of other :: contributors may be used to endorse or promote products derived from :: this software without specific prior written permission. :: :: THIS SOFTWARE IS PROVIDED BY ANDREW STROTHEIDE "AS IS" AND ANY EXPRESS OR :: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF :: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO :: EVENT SHALL ANDREW STROTHEIDE BE LIABLE FOR ANY DIRECT, INDIRECT, :: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT :: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, :: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF :: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING :: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, :: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. :: ---------------------------------------------------------------------------- :: :: Description: :: ------------ :: This program is a wrapper to pretty-print files using a2ps and convert the :: formatted PostScript output to PDF using GhostScript (on Windows, without :: Cygwin). The most important feature is the support for wildcards in the :: list of files to be pretty-printed. This is normally difficult because :: a2ps was originally written for UNIX and expects wildcards to have already :: been expanded by the command interpreter before program invocation, whereas :: DOS and Windows programs receive the wildcard characters directly and must :: perform their own expansions. Since a2ps cannot handle wildcards that have :: not been expanded, this batch program provides a rudimentary means by which :: to expand the names before providing them to a2ps. Also, the output from :: a2ps is a PostScript file, which is generally of little use when running :: under Windows. Therefore, this program takes the next step of converting :: the output from PostScript to PDF so it is accessible from programs such :: as Adobe Reader. :: :: Prerequisites: :: -------------- :: In order to use this program, you must install some third-party software. :: Installation of such software is subject to the license agreements of each :: package, which is almost certainly different than the license under which :: this batch program is provided. The following programs are required: :: * a2ps, which is available as part of the gnuwin32 suite of programs on :: SourceForge, but can be installed standalone :: (http://gnuwin32.sourceforge.net/packages/a2ps.htm). :: * GhostScript, which has two distributions: AFPL or GNU license :: (http://pages.cs.wisc.edu/~ghost/). :: * Optionally, you may wish to install a PostScript viewer, such as GSView :: which will allow you to preview pure PostScript files and convert or :: print them manually. GSView requires GhostScript and is available from :: from the GhostScript link above. :: :: Usage: :: ------ :: prettty_pdf input_files ... :: * The output file must have a .pdf extension and may optionally include :: a full pathname. :: * One or more input file must be provided. :: * Wildcard characters are acceptable in the input file listing (ex. *.c) :: * Executables for GhostScript and a2ps must be accessible directly from :: the command line, so their binary folders must be in the PATH variable :: (see http://www.computerhope.com/issues/ch000549.htm for assistance). :: :: ---------------------------------------------------------------------------- :: Initialization :: ---------------------------------------------------------------------------- SET EXITCODE=0 SETLOCAL ENABLEDELAYEDEXPANSION SET USAGE=Usage: %0 {output_file.pdf} input_file1 [input_file2 ...] :: ---------------------------------------------------------------------------- :: Errorlevels :: ---------------------------------------------------------------------------- SET EXIT_NO_OUTPUT_FILE=1 SET EXIT_OUTPUT_FILE_EXT=2 SET EXIT_NO_OUTPUT_PATH=3 SET EXIT_NO_INPUT_FILES=4 SET EXIT_BAD_INPUT_FILES=5 SET EXIT_PS_EXISTS=6 SET EXIT_GENERATE=7 SET EXIT_PS_MISSING=8 SET EXIT_PDF_EXISTS=9 SET EXIT_CONVERT=10 SET EXIT_PDF_MISSING=11 :: ---------------------------------------------------------------------------- :: Validate output file :: ---------------------------------------------------------------------------- IF ()==(%~1) GOTO Error_No_Output_File IF NOT EXIST (%~dp1) GOTO Error_No_Output_Path 2> NUL IF NOT (.pdf)==(%~x1) GOTO Error_Output_File_Ext SET PS_NAME="%~f1.ps" SET PDF_NAME="%~f1" IF EXIST %PS_NAME% GOTO Error_PS_Exists IF EXIST %PDF_NAME% GOTO Error_PDF_Exists SHIFT :: ---------------------------------------------------------------------------- :: Validate input files :: ---------------------------------------------------------------------------- IF ()==(%*) GOTO Error_No_Input_Files SET INPUT_FILES= FOR /F %%f in ('dir /l/b/-p %*') DO SET INPUT_FILES=!INPUT_FILES! "%%f" IF NOT ERRORLEVEL 0 GOTO Error_Bad_Input_Files :: ---------------------------------------------------------------------------- :: Use a2ps to create PostScript code listing :: ---------------------------------------------------------------------------- :Generate ECHO Generating temporary PostScript file... a2ps %INPUT_FILES% --medium=letter --toc -o %PS_NAME% IF NOT ERRORLEVEL 0 GOTO Error_Generate IF NOT EXIST %PS_NAME% GOTO Error_PS_Missing :: ---------------------------------------------------------------------------- :: Use GhostScript to convert PostScript to PDF :: ---------------------------------------------------------------------------- :Convert ECHO Generating PDF file %PDF_NAME%... gswin32c -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=%PDF_NAME% -q %PS_NAME% -c quit IF NOT ERRORLEVEL 0 GOTO Error_Convert IF NOT EXIST %PDF_NAME% GOTO Error_PDF_Missing IF EXIST %PS_NAME% DEL %PS_NAME% GOTO End :: ---------------------------------------------------------------------------- :: Error Conditions :: ---------------------------------------------------------------------------- :Error_No_Output_File ECHO *** Error: No output file specified. ECHO %USAGE% SET EXITCODE=%EXIT_NO_OUTPUT_FILE% GOTO End :Error_Output_File_Ext ECHO *** Error: Output file extension must be ".pdf". ECHO %USAGE% SET EXITCODE=%EXIT_OUTPUT_FILE_EXT% GOTO End :Error_No_Output_Path ECHO *** Error: Output file path "%~dp1" does not exist. ECHO %USAGE% SET EXITCODE=%EXIT_NO_OUTPUT_PATH% GOTO End :Error_No_Input_Files ECHO *** Error: No input files provided. ECHO %USAGE% SET EXITCODE=%EXIT_NO_INPUT_FILES% GOTO End :Error_Bad_Input_Files ECHO *** Error: Input file list contains invalid item(s). ECHO %USAGE% SET EXITCODE=%EXIT_BAD_INPUT_FILES% GOTO End :Error_PS_Exists ECHO *** Error: Cannot overwrite PostScript file %PS_NAME%; remove it or use a different output name. ECHO %USAGE% SET EXITCODE=%EXIT_PS_EXISTS% GOTO End :Error_Generate ECHO *** Error: A2PS returned an error. SET EXITCODE=%EXIT_GENERATE% GOTO End :Error_PS_Missing ECHO *** Error: The PostScript file was not generated. SET EXITCODE=%EXIT_PS_MISSING% GOTO End :Error_PDF_Exists ECHO *** Error: Cannot overwrite PDF file %PDF_NAME%; remove it or use a different output name. SET EXITCODE=%EXIT_PDF_EXISTS% GOTO End :Error_Convert ECHO *** Error: GhostScript returned an error. SET EXITCODE=%EXIT_CONVERT% GOTO End :Error_PDF_Missing ECHO *** Error: The PDF file was not created. SET EXITCODE=%EXIT_PDF_MISSING% GOTO End :: ---------------------------------------------------------------------------- :: Clean up and exit :: ---------------------------------------------------------------------------- :End EXIT /B %EXITCODE%