Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pdf2jpg.bat

(view source code of pdf2jpg.bat as plain text)

  1. @ECHO OFF
  2. SETLOCAL ENABLEDELAYEDEXPANSION
  3. :: Check Command Line
  4. IF "%~1"=="" GOTO Syntax
  5. IF NOT EXIST "%~1" GOTO InvalidPDF
  6. IF /I NOT "%~x1"==".pdf" GOTO InvalidPDF
  7. IF NOT "%~2"=="" GOTO Syntax
  8.  
  9. :: Find GhostScript executable
  10. SET ProgDir=
  11. SET ProgFile=
  12. FOR /F "tokens=*" %%A IN ('REG.EXE Query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall /f Ghostscript ^| FIND.EXE /I "HKEY_"') DO (
  13. 	FOR /F "tokens=2*" %%B IN ('REG.EXE Query "%%~A" /v UninstallString') DO (
  14. 		SET ProgDir=%%~dpC
  15. 	)
  16. )
  17. IF "%ProgDir%"=="" GOTO NoGhostScript
  18. FOR /F "tokens=*" %%A IN ('DIR /B /S "%ProgDir%gswin*c.exe"') DO SET ProgFile=%%~fA
  19. IF "%ProgFile%"=="" GOTO NoGhostScript
  20.  
  21. :: Run GhostScript
  22. PUSHD "%~dp1"
  23. "%ProgFile%" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -r600x600 -sOutputFile="%~n1.%%d.jpg" "%~nx1"
  24. POPD
  25.  
  26. ENDLOCAL
  27. GOTO:EOF
  28.  
  29.  
  30. :InvalidPDF
  31. ECHO.
  32. ECHO ←[0;1;31mERROR:←[0m  Please specify a valid PDF file
  33. GOTO Syntax
  34.  
  35.  
  36. :NoGhostScript
  37. ECHO.
  38. ECHO ←[0;1;31mERROR:←[0m  GhostScript not found
  39.  
  40.  
  41. :Syntax
  42. ECHO.
  43. ECHO PDF2JPG.bat,  Version 1.00 for Windows 10
  44. ECHO Use GhostScript to convert a PDF file to JPG images
  45. ECHO.
  46. ECHO Usage:  ←[0;1m%~nx0  "pdffile"←[0m
  47. ECHO.
  48. ECHO Where:  ←[0;1mpdffile←[0m     is the PDF file to be converted
  49. ECHO.
  50. ECHO Notes:  The resulting images will be saved in the same folder under the
  51. ECHO         same name as the PDF, but with extension ←[0;1m.n.jpg←[0m, where ←[0;1mn←[0m
  52. ECHO         is 1..the number of pages in the PDF file.
  53. ECHO         Existing JPG files will be overwritten without prompt for confirmation.
  54. ECHO         This batch file requires GhostScript to convert PDF to JPG.
  55. ECHO         GhostScript can be found at ←[0;1;30mhttps://www.ghostscript.com/←[0m
  56. ECHO.
  57. ECHO Written by Rob van der Woude
  58. ECHO https://www.robvanderwoude.com
  59.  
  60. ENDLOCAL
  61. EXIT /B 1
  62.  

page last modified: 2024-04-16; loaded in 0.0048 seconds