Home > Software design >  Batch Script to generate Landscape files with cPDF?
Batch Script to generate Landscape files with cPDF?

Time:08-30

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

CodePudding user response:

You need to concatenate the multiple 4 page document into one file, then process them afterwards.

If you can safely run the command with all the files use this:

cpdf -merge p*.pdf AND -twoup-stack -o out.pdf

If you are on Linux/*BSD/macOS and need take care with filenames, use this:

find . -type f -name p\*.pdf -print0 | xargs -0 cpdf -merge -stdout | cpdf -twoup-stack -stdin -o out.pdf

The find looks for any file in the current directory starting with p and ending in .pdf.

Then pass that through xargs -0 to safely run cpdf having it output to stdout.

Finally stack the file, two per page, and write to out.pdf.

CodePudding user response:

Looks like your using the windows version thus from cmd prompt to process all 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 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 last del *-out.pdf is also required

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

  • Related