Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pdf2jpg2pdf.bat

(view source code of pdf2jpg2pdf.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.  
  25. SET GSCommand=
  26. FOR %%A IN (%~n1.*.jpg) DO (
  27. 	REM Make sure to append a trailing space after showpage
  28. 	SET GSCommand=!GSCommand! ^(%%A^) viewJPEG showpage 
  29. )
  30. "%ProgFile%" -dNOSAFER -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -o "%~n1.jpg.pdf" "%ProgDir%.\lib\viewjpeg.ps" -c "%GSCommand%"
  31. POPD
  32.  
  33. ENDLOCAL
  34. GOTO:EOF
  35.  
  36.  
  37. :InvalidPDF
  38. ECHO.
  39. ECHO ←[0;1;31mERROR:←[0m  Please specify a valid PDF file
  40. GOTO Syntax
  41.  
  42.  
  43. :FileExists
  44. ECHO.
  45. ECHO ←[0;1;31mERROR:←[0m  The output file already exists.
  46. ECHO         Move or rename %TextFile% and try again.
  47. GOTO Syntax
  48.  
  49.  
  50. :NoGhostScript
  51. ECHO.
  52. ECHO ←[0;1;31mERROR:←[0m  GhostScript not found
  53.  
  54.  
  55. :Syntax
  56. ECHO.
  57. ECHO PDF2JPG2PDF.bat,  Version 1.00 for Windows 10
  58. ECHO Use GhostScript to convert a "text-based" PDF file to "image-based" PDF
  59. ECHO.
  60. ECHO Usage:  ←[0;1m%~nx0  "pdffile"←[0m
  61. ECHO.
  62. ECHO Where:  ←[0;1mpdffile←[0m     is the PDF file to be converted
  63. ECHO.
  64. ECHO Notes:  The resulting "image-based" PDF will be saved in the same folder
  65. ECHO         under the same name as the "text-based" PDF, but with extension
  66. ECHO         ←[0;1m.jpg.pdf←[0m instead of ←[0;1m.pdf←[0m
  67. ECHO         This batch file requires GhostScript to convert PDF to JPG to PDF.
  68. ECHO         GhostScript can be found at ←[0;1;30mhttps://www.ghostscript.com/←[0m
  69. ECHO.
  70. ECHO Written by Rob van der Woude
  71. ECHO https://www.robvanderwoude.com
  72.  
  73. ENDLOCAL
  74. EXIT /B 1
  75.  

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