Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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
share|improve this answer
    
Thank you so much for the advice! I will try this and repost. –  matt m May 22 at 3:50
    
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 May 22 at 17:05
    
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 May 23 at 3:39
    
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 May 23 at 15:54
    
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 May 23 at 21:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.