Bash is a very flexible cross-platform scripting language with multiple uses and can integrate with lots of other tools (ie DIff). It is built into OS X and Linux and you can download it for Windows. This post is about some uses of Bash and how you can use it with Java. We will show you how we run our Java program from Bash and hopefully give you some ideas for how you can use Bash in your own work.
At IDRSolutions, we specialize in converting PDF files to HTML5. This needs a LOT of regression testing, and we use Bash to control all this. We also use it to automate tasks as much as possible. Recently I wrote a Bash Script (So we can run on both Linux & Windows) to automatically pull the required files from our FTP, convert the PDF files used on our examples page, run PNG-Quant on them and re-upload the converted HTML5 output to the FTP. This way our examples page will always show a true representation of what our software can currently achieve (and I can get a coffee while the computer does all the hard work!)
In this tutorial, I’ll be using our Java PDF to HTML5 jar which converts files automatically with a Bash Script as an example of running a Java program from Bash. I will also cover how to run a really useful free image compression tool (PNGQuant) on the converted files in Bash.
Getting Started
General Setup :
- First things first, you’ll need to download our PDF to HTML5 trial jar.
- The next step is to create a folder anywhere you like and move the PDF to HTML5 jar into that folder, we’ll call it “ExampleFile”.
- Next, get any PDF you wish to convert and move it into the ExampleFile folder you created, for this tutorial ours will be called examplePDF.pdf.
- Next, create a .txt file and name it something like “ConvertPDF”, this will be where we write the BashScript.
Your ExampleFile directory should now look something like this :
- Ensure you have Java and PngQuant installed using terminal write :
Coding the BashScript
Ok so let’s get coding!
Open up the text file you created with an app such as gedit.
First, we will add these two lines to the top of the text file which will tell the console that its a bash file and to clear the console screen :
#!/bin/bash
clear
Now we’re going to add a variable that will hold the location where we want our output directory (where we want the converted files to end up) and one more variable to hold the PDF file name.
Notice the “echo” words? They just print text to the console, so they’re useful for documentation, giving instructions and placing line-breaks.
Like so :
echo "---------Starting Script---------"
echo
echo "---------Generating Vars---------"
OUTPUT="OutputFile/"
FILE="examplePDF.pdf"
Now we will write some code that will automatically create our OutputFile directory for us (makes our life easier and cuts down the requirements), this is where our converted content will end up.
Like so :
echo "---------Making Directories---------"
mkdir -p OutputFile/
And finally, the code will run the PDFtoHTML5 jar with the input PDF file name and the output location.
Like so :
echo "---------Converting---------"
java -Xmx512M -jar jpdf2html.jar $FILE $OUTPUT
echo
echo "---------Finished Script---------"
Your code should now look something like this :
#!/bin/bash
clear
echo "---------Starting Script---------"
echo
echo "---------Generating Vars---------"
OUTPUT="OutputFile/"
FILE="examplePDF.pdf"
echo "---------Making Directories---------"
mkdir -p OutputFile/
echo "---------Converting---------"
java -Xmx512M -jar jpdf2html.jar $FILE $OUTPUT
echo
echo "---------Finished Script---------"
Giving Script Permission & Running :
Now we have written our BashScript to convert a PDF file to HTML5 we need to give the file permission to run, so first thing we do is navigate to the directory where our .txt file is located using terminal write:
And now give our BashScript permission to run by writing in terminal :
And finally, we run it,
If we now check our OutputFile directory we should see the following file which contains our html5 files.
Congratulations!! In just a few lines of code, you have just converted your first PDF file to HTML5 using just a java .jar file and some home-brewed BashScript!
As a bonus and for a bit of fun we’ll now run .png quant over all the .png files in the converted output folder which will reduce the file size dramatically and you can then even host the content on your website if you wanted to!
Running PNGQuant
Ok so once again open up the BashScript we were editing
Now add the following lines of code to the bottom of the BashScript (assuming you installed PNGQuant correctly) :
echo "-----Starting PNGQuant Script-----"
find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;
echo "-----Finished PNGQuant Script-----"
Your BashScript file should finally look like this :
#!/bin/bash
clear
echo "---------Starting Script---------"
echo
echo "---------Generating Vars---------"
OUTPUT="OutputFile/"
FILE="examplePDF.pdf"
echo "---------Making Directories---------"
mkdir -p OutputFile/
echo "---------Converting---------"
java -Xmx512M -jar jpdf2html.jar $FILE $OUTPUT
echo "-----Starting PNGQuant Script-----"
find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;
echo "-----Finished PNGQuant Script-----"
echo
echo "---------Finished Script---------"
Now PNGQuant will run over our converted files once they are converted.
So finally, cd to where our BashScript is located :
And run the BashScript again :
You should now see this :
And that’s it! We have now not only written a BashScript that will convert a PDF file to HTML5 using Java but now it will also compress any .png images in the converted output, thus reducing file-size!!
Our software libraries allow you to
Convert PDF to HTML in Java |
Convert PDF Forms to HTML5 in Java |
Convert PDF Documents to an image in Java |
Work with PDF Documents in Java |
Read and Write AVIF, HEIC, WEBP and other image formats |