Bulk .docx to .pdf Conversion

Sometimes you just need some fast PDF file conversion action satisfaction! Fortunately LibraOffice which is installed by default on most GUI driven Linux distro’s can run headless operations for converting between file formats via the command line. In my case I needed to convert some Windows Word files to PDFs. This can easily be done with the following command below.

loffice --convert-to pdf *.docx

As a Perl One-Liner

To add to this syntax above lets prefix an epoch time stamp and make it better formatted for web (a.k.a an excuse to create a perl one liner) The following shenanigans below will work nicely πŸ˜‰

ls *.doc* | perl -MFile::Copy -snle '$o=$_; s~(.*)\.doc.*$~${1}~; system qq~$lo "${o}"~; $o=$_; s~\s+(?{$m=q`-`})|[[:punct:]]+(?{$m=q``})~$m~ge; $pdf = time."-${_}.pdf"; move "${o}.pdf", "$pdf"; print "> $pdf - Created."' -- -lo='libreoffice --headless --invisible --convert-to pdf'

To make this re-useable you can create a function to your .bashrc file. This way you can invoke it as needed. Put in the following at the end of the file.

Regex Explanation

This regex s~\s+(?{$m=q-})|[[:punct:]]+(?{$m=q“})~$m~ge; replaces whitespace with the dash character and strips out any punctuation. This makes it more ‘web friendly’.

.bashrc

function doc2pdf
{
	
    ls *.doc* | perl -MFile::Copy -snle '$o=$_; s~(.*)\.doc.*$~${1}~; system qq~$lo "${o}"~; $o=$_; s~\s+(?{$m=q`-`})|[[:punct:]]+(?{$m=q``})~$m~ge; $pdf = time."-${_}.pdf"; move "${o}.pdf", "$pdf"; print "> $pdf - Created."' -- -lo='libreoffice --headless --invisible --convert-to pdf'

}

Usage

doc2pdf

πŸ‘ Get Blog Updates

πŸ“ Latest Posts

0Shares
0

Leave a Reply