Solved

batch script to merge pdf files

Posted on 2013-11-04
16
8,830 Views
Last Modified: 2016-10-05
I have a folder with 2 files, file1.pdf and file2.pdf

is it possible to have a batch script, that takes all the files in the folder, prompts for a file name and merges both files into the new file with the new name? (the one we entered in the prompt?

also delete the old files

anybody has done anything like this?

thanks,
Vinnie
0
Comment
Question by:damixa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 15

Assisted Solution

by:Giovanni Heward
Giovanni Heward earned 250 total points
ID: 39622135
You can do this using PDFtk.

pdftk *.pdf cat output combined.pdf

Open in new window


This script will get the job done, with some safety checks built in.

@echo off
setlocal enabledelayedexpansion
set pdftk=c:\path\to\pdftk.exe
set /p t=PDF output name: 
if [!t!]==[] set t=combined.pdf
if exist temp ( rd /s/q temp ) else ( md temp )
if exist !pdftk! (
   if exist temp (
      !pdftk! *.pdf cat output temp\!t!
      if exist temp\!t! (
         del *.pdf /f/q
         move temp\!t! .
         rd temp /s/q
      )
   )
)

Open in new window

0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 39622140
Hi Vinnie,
I recommend using the the PDF Toolkit (PDFtk), an excellent (free!) product that has numerous features to manipulate PDFs. It comes in both command line and GUI versions. The command line version is called PDFtk Server and may be downloaded here:
http://www.pdflabs.com/tools/pdftk-server/

Don't be misled by "Server" in the name. I don't know why they called it that, but it's just an EXE with a DLL that runs on XP, Vista, W7, and W8 (it does not have to run on a "server" OS...it also runs on Mac, but I've never used it on that).

Here's the one-line solution using PDFtk:

pdftk file1.pdf file2.pdf cat output outputfile.pdf

The rest of the batch file would be getting the output file name and deleting the source files. You can make it as fancy or as simple as you want. Here's the simple approach:

@echo off
set /p fn=Enter the output filename:
pdftk file1.pdf file2.pdf cat output %fn%.pdf
del file1.pdf
del file2.pdf

Open in new window


You'll need to handle paths and the location of PDFtk, but other than that, the above works. I just tested it with the input files and the two required PDFtk files (pdftk.exe and libiconv2.dll) residing in the same folder and it worked perfectly. Regards, Joe
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 39622141
Sometimes timing is everything in life. :)
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:damixa
ID: 39622226
This worked great. Is there a way to delete all files except %fn% ?

thanks,
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 39622240
I won't speak to x66_x72_x65_x65's script. Mine deletes just file1.pdf and file2.pdf. Regards, Joe
0
 

Author Comment

by:damixa
ID: 39622274
So I had to use


@echo off
set /p fn=Enter the output filename:
pdftk *.pdf cat output %fn%.pdf

Open in new window

because I dont know the names and how many files there would be. Just an extra step to delete * except for %fn%.pdf.

thanks again
0
 
LVL 54

Accepted Solution

by:
Joe Winograd, EE MVE earned 250 total points
ID: 39622359
Ah, I misunderstood you...I thought it was just those two files...but you figured it out with *.pdf – perfect!

Btw, I should have mentioned this before, but I'd be tempted not to delete the source PDFs in the same batch file, in case the merge (cat) does not work successfully (although I've never seen PDFtk fail on it). You could build a separate batch file to delete the source files, but run it only after you reviewed the output to make sure that the operation worked.

That said, one simple trick is to rename the output PDF file to a different file type, do the delete of *.pdf, then rename the output file back to a PDF. Something like this:

@echo off
set /p fn=Enter the output filename:
pdftk *.pdf cat output %fn%.pdf
ren %fn%.pdf %fn%.xxx
del *.pdf
ren %fn%.xxx %fn%.pdf

Simple, but effective. Regards, Joe
0
 

Author Comment

by:damixa
ID: 39622457
genius Joe,
thanks
0
 

Author Closing Comment

by:damixa
ID: 39622460
Thanks both of you guys
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 39622476
You're welcome. And thanks to you for the kind words - much appreciated! Regards, Joe
0
 

Expert Comment

by:Sazan Abd
ID: 41828847
Hi Joe, I came across this site when I was looking for batch pdf file merge. I have a folder with man pdf files, and want to merge the files with the same id in to one file, for example:

111_1.pdf
111_2.pdf
222_1.pdf
222_2.pdf
I want to merge 111_1.pdf and 111_2.pdf into one file and merge 222_1.pdf and 222_2.pdf into another.

How can I do this.
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 41828900
> How can I do this.

Write a program that looks for a match on the first three characters of the file names — or on all the characters in the file name before an underscore (needed if the file names can be a varying number of characters before the underscore). Build up a list of file names with the matching first characters and then feed the list to PDFtk with the cat command, naming the output file with the characters before the underscore. In other words, in your example above, it would combine/merge:

111_1.pdf
111_2.pdf

into:

111.pdf

and:

222_1.pdf
222_2.pdf

into:

222.pdf

and, extending your example:

33333_1.pdf
33333_2.pdf
33333_3.pdf

into:

33333.pdf

I've written several programs for clients to do this, but each one is slightly different based on each customer's unique requirements. One of the simpler programs is called Combine PDFs Based On Filenames, but it requires a fixed number of leading characters. Other programs allow multiple segments in the file names (i.e., more than just the two as in your case) and varying separator characters (underscore, hyphen, period, etc.). In any case, if you are a programmer, you shouldn't have much trouble writing a program to figure out which PDFs to combine/merge — the tough stuff is combining/merging them, but that's where PDFtk comes in — making it easy! Regards, Joe
1
 

Expert Comment

by:Sazan Abd
ID: 41829010
Thanks Joe for your prompt reply, I have some programming back ground, I do understand your concept, but I would appreciate if you can share any scripts that do this task.
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 41830003
Do all of your files have just three characters before the underscore? If not, do they all have the same number characters before the underscore?
1
 

Expert Comment

by:Sazan Abd
ID: 41830918
Let me explain the exact scenario, I am generating customer statement using SSRS, each statement has 3 or more page, the first two pages needs to be in portrait orientation, and the rest in landscape. SSRS doesn't have the ability to set orientation per page, it is set on the report level only. To get around this issue, I am generating a report with the first two pages in portrait layout, and another report contains the rest of the pages in landscape layout, then export them in PDF format and finally merge both report for each customer into one pdf file. All the files will start with 9 character (which is the account number and will be followed by '-' and statement number and page number, as below:

123456789-23-page12.pdf
123456789-23-page3.pdf

123334455-4-page12.pdf
123334455-4-page3.pdf
0
 
LVL 54

Expert Comment

by:Joe Winograd, EE MVE
ID: 41830965
Thanks for the explanation — very helpful! I suggest we continue this discussion via the message system, since it has veered off-course from the original, already-closed question. I'll send you a message in a moment.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question