Creating a batch OR .bat file to linearize and merge PDFs using QPDF.exe

3

2

I am trying to create a batch OR .bat file that will linearize MULTIPLE PDF files. I already have a script I commandeered that uses PDFTK that sends PDFs to the 'sendto' folder where they are combined. I would like to do the same with qpdf but use the --linearize command instead.

The main problem is, I cannot combine the files first into one PDF using PDFTK and then linearize the one file - the files still turn out too big. What works the best is if I linearize each INDIVIDUAL file, and then combine them. It would probably be best if I could also combine them using qpdf, that way I wouldn't have to use two different programs.

IN SUMMATION:

This is the current bat file I have that works perfectly (below) -> I can select multiple PDF files, drop them into the bat file and out pops a new PDF file named Combined_PDFs.pdf. I want to do the SAME with qpdf, except linearize, THEN combine the files. I cannot get the syntax right, I simply do not know enough about bat files.

CURRENT WORKING SCRIPT:

@echo off
PUSHD "%~dp0"
setlocal enabledelayedexpansion
FOR %%A IN (%*) DO (set command=!command! %%A)
pdftk.exe %command% cat output "%~dp1Combined_PDFs.pdf" compress

This script works, but it will ONLY work for one file at a time:

@echo off
PUSHD "%~dp0"
setlocal enabledelayedexpansion
FOR %%A IN (%*) DO (set command=!command! %%A)
qpdf.exe --linearize %command% "%~dpn1_Reduced%~x1"

I need the script above to work for multiple inputs.

matt m

Posted 2014-05-21T19:03:52.650

Reputation: 33

Answers

2

I believe what you need to do is change the FOR loop in the second batch file. The first batch file creates a local variable called "command" that holds all of the files passed to the batch file in a string, separated by spaces.

However, in your second batch file, you need to actually run the qpdf.exe program separately for each file. To do this, change the FOR loop to execute the qpdf.exe program on each loop:

@echo off
PUSHD "%~dp0"
setlocal enabledelayedexpansion
FOR %%A IN (%*) DO qpdf.exe --linearize %%A "%%~dpnA_Reduced%%~xA"

In short, this will run whatever is after the DO one time for every string passed to the batch file.


Also, as a tip for future debugging, try putting REM in front of the @echo off statement temporarily. REM is used for commenting code and can improve readability. That will display each line in the batch file as it executes, so you get a much better idea of what is happening when you run.

REM @echo off

Kevin Bennett

Posted 2014-05-21T19:03:52.650

Reputation: 36

Hmm, this seems to work good for one file. However, when I select more than one file it looks like it processes all the files correctly but names them all the same name (first file selected_Reduced.pdf). This of course will write over the previous files, leaving only one file produced. – matt m – 2014-05-22T17:05:39.933

My apologies! I've updated the FOR loop. Previously it was coded to only use the first filename. Now it should use the name of the file currently being processed in addition to the extension of the file being processed (in case they are different).
The difference is the double percent sign, and the use of the variable "A".
– Kevin Bennett – 2014-05-23T03:39:37.807

Works like a charm! Thank you so very much! I hate to push my luck...but is there anyway I could batch combine the _Reduced files from there (either with PDFtk or qpdf)? – matt m – 2014-05-23T15:54:15.680

If I understand what you are asking, it seems like you could use the batch file I showed in the answer, and then add the portion of your first batch file that combines all of the Reduced PDF files. So at the end of the batch file add: FOR %%A IN (%*) DO (set command=!command! "%%~dpnA_Reduced%%~xA") pdftk.exe %command% cat output "%~dp0Combined_PDFs.pdf" compress This should put all of the Reduced files into the variable "command", and then pass that to the pdftk.exe application. Note that, in this example, the final file will end up in the directory where the batch file is located. – Kevin Bennett – 2014-05-23T21:35:12.150