Wednesday, December 5, 2007

Merging PDF file with gs

Merging PDF files together can be very useful.
I use it to collect all the tutorials for a course into
one file and all the solutions into another. 
This makes it easier for students to print all the
tutorials for a few weeks in one go and has other practical benefits.

There are lots of programs available on the web which merge PDFs. 
One which has been specifically brought to my attention is pdftk
the pdf tool kit, which is free software made available under the GPL. 
It is also possible to merge PDFs on the command-line 
if you have GhostScript installed.

To merge three PDF files entitled '1.pdf','2.pdf' and '3.pdf' into one file 
called 'all.pdf', the command is:

For Windows PCs:
gswin32 -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Merged.pdf -dBATCH 1.pdf 2.pdf 3.pdf

For Unix PCs:
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=Merged.pdf -dBATCH 1.pdf 2.pdf 3.pdf

I use 2 Windows batch files to work through a directory of PDFs. 
It assumes the existence of a first file 1.pdf and joins all other 
PDFs alphabetically, outputting the file merged.pdf.

The batch files can be found here and the source is below. 
Execute Merge1 in the directory where the files to be joined 
are located. These programs have not been thoroughly 
tested and are used at your own risk. 
They were thrown together but they seem to work!

Merge1:
@echo off
gswin32 -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged.pdf -dBATCH 1.pdf
FOR %%Z IN (*.pdf) DO IF NOT %%Z==1.pdf IF NOT %%Z==merged.pdf IF NOT %%Z==merged2.pdf call merge2.bat %%Z

Merge2:
@echo off
gswin32 -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=merged2.pdf -dBATCH merged.pdf %1
del merged.pdf
ren merged2.pdf merged.pdf


For Windows users:
If you receive the error

"'gswin32' is not recognized as an internal or external command, 
operable program or batch file",
then you need to add GhostScript to your path.
To do this, go to My Computer->Properties->Advanced tab.
Click on Environment Variables and edit the System Variable called 'Path'.
Add "c:\Program Files\gs\gs8.00\bin;" (or wherever your installation of GhostScript resides)
to the end of the path string.

Centos 7 reset root/ any user lost password / lockout due to cant remember password

1. Need to be in front of the terminal. (Physically if not vm). 2. Reboot the server 3. Press 'e' in the GRUB2 boot screen. 3. bunch...