Search code examples
batch-filepdfcmdcommand-lineforfiles

Batch Script to generate Landscape files with cPDF?


See if anyone can help me. For example:

I have a .pdf document with 4 pages.

With the command below I can make this .pdf document have two pages, that is, two pages on the same sheet in "landscape" format

cpdf -twoup-stack in.pdf -o out.pdf

But I need to make it work on multiple files in the same folder. I tried the command below and it didn't work. If anyone has a solution I would appreciate it, I'm a beginner.

This prints only one file from the folder.

cpdf -twoup-stack path\*.pdf -o out.pdf


Solution

  • Looks like your using the windows version thus from cmd prompt to process all multi-page files in current folder you could use the simpler

    forfiles /m *.pdf /c "cmd /c cpdf -twoup-stack in.pdf -o out-@file"

    so in1.pdf becomes out-in1.pdf

    similar is

    for %f in (*.pdf) do cpdf -twoup-stack in.pdf -o "out-%f"

    It is more convenient to use a batch.cmd file with a different for structure, with some variables to allow drag and drop or specify a default folder etc.

    so if the following 2up.cmd is placed in a work folder with multiple pdfs it will on entering 2up at a cmd prompt or a simple double click on that cmd file then process all the pdf files in that working folder.

    Note a batch file needs %% where a cmd line would just need one but the single %~ inside the brackets is correct ! also define the folder for cpdf to ensure it is the only cpdf command accessed and generally "double quote" to avoid problems with spaces.

    @echo off
    REM ensure we only run in this file.cmd dir not any system dir
    cd /d "%~dp0"
    
    for %%F in ("%~dp0*.pdf") do "c:\path to\cpdf.exe" -twoup-stack "%%F" -o "%%~dpnF-out.pdf"
    

    The result will be a rotated imposition compared to source pages so the next step is usually add a second command at the end to rotate those-out.PDFs.

    for %%F in ("%~dp0*-out.pdf") do "c:\path to\cpdf.exe" -rotate 90 "%%F" -o "%%~dpnF-2up.pdf"

    If you are going to run twice for 4-up it becomes more desirable to delete work files so in that case consider if a 3rd) del "%~dp0*-out.pdf" is also required before 4) ...*-2up.pdf ...-out.pdf and 5) -out.pdf to -4up.pdf

    4-up is generally 2-up twice so I gave hints above about add extra lines at the end, in comments you tried and found only 3 steps as for loops needed for your desired use :-

    This may not be the most efficient way (but is now one single for loop, so retains the logic) it can be used as a template for different executables and different function sequences.
    4-up.cmd

    @echo off
    REM we will be changing % values % during ! runtime !
    setlocal ENABLEDELAYEDEXPANSION
    
    REM ensure we only run in this file.cmd dir not any system dir
    cd /d "%~dp0"
    
    REM we can set a work directory here from command line or drag and drop and use it later
    REM but that's a further customization based on user choices
    
    REM set mode to -twoup or -twoup-stack (see cpdf manual)
    set "mode=-twoup-stack"
    
    REM set cpdf command location
    set "theExe=C:\Users\WDAGUtilityAccount\Desktop\SandBox\apps\PDF\cpdf\New folder\cpdf.exe"
    
    REM using current folder and restricting file set to *.pdf
    
    for %%F in ("%~dp0*.pdf") do (
      set "name=%%~nF"
          echo processing !name!
    
      "%theExe%" %mode% "!name!.pdf" -o "%temp%\!name!-tmp1.pdf" 2>nul
          dir /b "%temp%\!name!-tmp1.pdf"
    
      "%theExe%" %mode% "%temp%\!name!-tmp1.pdf" -o "%temp%\!name!-tmp2.pdf" 2>nul
          dir /b "%temp%\!name!-tmp2.pdf"
    
      "%theExe%"  -rotate 180 "%temp%\!name!-tmp2.pdf" -o "%~dp0!name!-4up.pdf" 2>nul
    
      REM lets remove the temporary files to ensure no mix-up on runs
      del "%temp%\!name!-tmp?.pdf"
          echo deleted tmp files
    
      echo "%~dp0!name!-4up.pdf" done
    )
    pause
    

    You can delete those dir lines echoing progress, if you want a more silent run and then the pause is not required if double click running the cmd file.

    To save your -outputs in a different folder the simplest will be to change end of last
    -o "%~dp0!name!-4up.pdf"
    to point to a different folder but keep the Filename so
    -o "c:\folder name\!name!.pdf"

    To adapt to a remote folder or for drag and drop is beyond this question so try FOR /? to see many other options including adapting to work with sub directories. For a single file drag and drop variant modify similar to https://github.com/GitHubRulesOK/MyNotes/blob/master/AppNotes/SumatraPDF/Addins/N-Up-PDF/2-Up-PDF.cmd