Wednesday 26 August 2015

PDFtk Burst PDF in a Windows Command Prompt (Batch File)

I had a user at work who needed to expand PDF files into their individual pages. 
This is the solution I came up with, using PDFtk and a bit of cmd script.
It creates files in a sub folder called output, so make sure that exists before you try and run it.

<code>
@echo off
setlocal EnableDelayedExpansion
set "source=%~dp0"
set "name=%1"

IF DEFINED name (
 call :burst %name%
 ) else (
 call :usage
 )
goto :xit

:burst
 set "file=%~nx1"
 echo Working on %file%
 for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined mydate set mydate=%%x
 SET "mytime=%mydate:~0,14%"
 pdftk %name% burst output "%source%output\%file%_%mytime%_%%04d.pdf"
 for %%d in (%source%output\doc_data.txt) do del %%d
 echo Done!
 goto :EOF

:usage
echo Nothing to do... drag a file onto me!
goto :EOF

:xit
endlocal
timeout /T 3
<\code>