Get Updates by Email

Wednesday, 14 September 2016

Some useful hacks I learned recently

This is mostly for my own consumption, but you might find it useful too.

This single image might make more sense to you than the rest of this post combined.

The disappearing mouse cursor in GNOME - how to make it reappear.

This problem tends to happen with my Elementary OS system, which is based on Ubuntu. I leave the computer for a while, and then the computer logs out. I come back, and I log in. The mouse cursor would have disappeared.

Quick solution from Stack Exchange: Press Ctrl-Alt-F1, then Ctrl-Alt-F7.

Ctrl-Alt-F1 brings you to the text terminal. Ctrl-Alt-F7 brings you back to the graphical environment. This resets the mouse daemon and you mouse cursor is back.


Removing carriage returns on LibreOffice

I sometimes copy a bunch of text, and the paragraph's been broken up with carriage returns. Ugly carriage returns that smashes the justified alignment of the paragraph and makes it appear jaggedly.

Or sometimes I download a PDF and then I convert it to TXT like this:

pdftotext THEPDF.pdf THEPDF.txt

And of course the text file gets a carriage return at the end of every line. It won't look nice when you want to convert the TXT file into a EPUB for fun reading and sharing.

The long way which I used to do: Copy the paragraph, paste it on a website that removes carriage returns, and click "Remove". Then copy the resulting text and paste back into the original file. Very tedious. But I did that for a couple of favourite blog posts that I wanted to put into an EPUB for personal reading. Like a personal edition of "Top 100 #longread #tldr blog articles from 2015". 

So I found an easier way. Apparently you can do the "Search and Replace" for carriage returns in LibreOffice. It helps to swap carriage returns for spaces.

Here are the steps:
  1. In LibreOffice, call up the Search and Replace dialog by pressing Ctrl-H.
  2. At Find, type "$" (without quotation marks)
  3. At Replace, type " " (without quotation marks)
  4. Click on "Other options" (little plus sign)
  5. Tick on "Regular expression"
  6. Choose "Replace" or "Find next" until the whole text file is done.

A popular saying that may or may not be correct. Who knows? You might have to lose it all to find out. 


How to convert a PDF generated by LibreOffice into a PDF that doesn't have editable text.

You know some clients want you to send over the PDF file, just so that they can take your hard work and tell you two hours later that it's not required. I've had that experience so often that I got fed up. They always say, "Send me the file, it's urgent!" And then I send it along with my bill. And then they're quiet as crickets.

LibreOffice is great because it can export the text file into PDF format. Sometimes the clients aren't so tech-savvy so they say, "Send me the DOC file so that I can edit it! I can't edit your PDF." But there are tools out there like "PDFTOTEXT" or online converters. Once they get the PDF they disappear because the text is good to go.

So to save me some heartache I created a Bash script that will do this:
  1. Generate PNG files from every page of your PDF (density: 100 dpi)
  2. Convert every PNG image into PDF. Same image density.
  3. Delete every PNG.
  4. Combine all the newly generated PDF pages into a new PDF.
The resulting PDF cannot be edited. It cannot be copied and pasted. But it's not immune against people who want to print out your PDF, as is. I know that my work is good enough for many clients to print out. Even my first draft. And that's why I get upset when they say, "We are still studying your draft agreement." Especially when they say it after a few months.

So here's my Bash script.
if [[ $# -eq 0 ]] ; then
    echo 'Please specify a PDF file'
    exit 1
fi
echo Converting this file = "$1"
echo This works with PDFs up to 999 pages long.
echo To use it on PDFs 1,000+ pages, edit this script.
convert -density 100 "$1" foo-%03d.png
echo PNGs for individual pages created.
echo Now converting PNGs to PDFs.
for a in {000..999}
 do
  if [ -f 'foo-'$a'.png' ]
   then
    convert 'foo-'$a'.png' 'foo-'$a'.pdf'
    rm 'foo-'$a'.png'
  fi
done
echo PNGs converted to PDFs. PNGs erased.
echo Using PDFTK to join the individual pages into a combined PDF.
echo Please wait.
mv foo-000.pdf foostart.pdf
for a in {001..999}
 do
  if [ -f 'foo-'$a'.pdf' ]
   then
    pdftk foostart.pdf 'foo-'$a'.pdf' cat output fooresult.pdf
    mv fooresult.pdf foostart.pdf
    rm 'foo-'$a'.pdf'
  fi
done
mv foostart.pdf unclick."$1"
echo Individual PDF pages joined into unclick."$1"
echo Individual PDF pages erased.

What you need to do:
  1. Copy my script, and save it under a name like "unclickable.sh"
  2. At the Bash prompt, run this: chmod +x unclickable.sh
  3. Make sure that you have ImageMagick and PDFTK installed. Otherwise...
  4. Install ImageMagick by running: sudo apt-get install imagemagick
  5. Install PDFTK by running: sudo apt-get install pdftk
  6. You're good to go.

Let's say you have a file called "MYPDFDOCUMENT.pdf" generated by LibreOffice. You can process it like this:

./unclickable.sh MYPDFDOCUMENT.pdf

It will generate a file called "unclick.MYPDFDOCUMENT.pdf". Or something like that.

I wouldn't mind giving the source files to the client if they paid my bill. But if they don't intend to pay the bill, they can retype my PDF file, which would have taken me hours and hours to draft out. Sometimes even days. But everybody thinks that they overpay for "simple work". It's usually easy for anyone to say that something is "simple" when they have the PDF file.

When I was younger, I thought of becoming a programmer. I started programming at the age of 11. I taught myself BASIC from a library book. I guess this is how I compensate for that lost childhood dream.

Thanks for reading.

Would you like to read some of my other posts.

0 comments: