Solved

batch script to merge pdf files

Posted on 2013-11-04
16
6,311 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
  • 8
  • 4
  • 3
  • +1
16 Comments
 
LVL 14

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 51

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 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 39622141
Sometimes timing is everything in life. :)
0
 

Author Comment

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

thanks,
0
 
LVL 51

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 51

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Closing Comment

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

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 51

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 51

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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Have you ever come up with a need of emailing only few pages of PDF file to one of yourfriend or colleague, instead of whole Adobe file? If yes, then surely you have face problems in doing that! Read this section as I have suggested multiple solutio…
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now