Built in PDF Magic in Debian

Debian has some awesome PDF tools built right in via the poppler-utils package that I never knew about. In my previous post I talked about how to make beautiful documents with code snippets using various Sublime Text extensions to convert markdown into PDF. One issue that I ran into was getting a cover page created. As far as I know There is really no easy way to make a nice cover page in markdown. Specifically in Github Flavored Markdown (which is what I am using) there is not a good way to make a page break.

The easy solution would be to simple write up your entire document in Markdown, and then make a separate cover page. The problem is how to merge these two files together to make one document.

Thanks to the built in PDF tools in debian this becomes very simple! The poppler-utils package has the following utilities built in:

  • pdfdetach — lists or extracts embedded files (attachments)
  • pdffonts — font analyzer
  • pdfimages — image extractor
  • pdfinfo — document information
  • pdfseparate — page extraction tool
  • pdftocairo — PDF to PNG/JPEG/PDF/PS/EPS/SVG converter using Cairo
  • pdftohtml — PDF to HTML converter
  • pdftoppm — PDF to PPM/PNG/JPEG image converter
  • pdftops — PDF to PostScript (PS) converter
  • pdftotext — text extraction
  • pdfunite — document merging tool

In order to combine a coverpage with another document we can simple run the following command inside a terminal.

pdfunite coverpage.pdf content.pdf final.pdf

This will create a document called final.pdf (of course you should change coverpage.pdf and content.pdf to match your actual files).

Warning: be sure to include the final output file or pdfunite will replace the last file that you type will all over the previous files!

Convert Markdown to PDF in Sublime Text

Sublime Text is an awesome text editor that has a ton of useful extensions that can be installed through the Package Manager that make it even better. One of my favorite parts of emacs is org-mode, this allows you to organize your life, make awesome notes, and even perform spreadsheet calculations all from with in the emacs text editor. The best part about org-mode is that you can export your documents to HTML, PDF, and LaTex. In my opinion, sublime text is almost a modern successor to emacs because a lot of the extensions that have been developed for Sublime Text allow you to do fancy things like this as well.

I am a huge fan of Markdown, It makes writing structured documents super simple, and is especially useful when your documents contain code snippets. As a CS student, I am often writing papers that include code snippets and this is not handled very well in most word processors. Org-mode does a great job of creating documents with code snippets, and I was excited to find out that with a few extensions to Sublime text you can make beautiful documents including code with little to no hassle.

The following steps are being done with Sublime Text 3 running on Debian Testing, the steps may be a bit different for other Operating Systems. In order to make beautiful documents with code snippets in Sublime Text you will need to do the following.

  1. If you have not already install Package Control
  2. Install MarkdownEditing in Sublime Text
  3. Install Pandoc in Sublime Text
  4. Install Pandoc in Debian
  5. Install TexLive in Debian (this is to convert things to PDF)

sudo apt-get install pandoc texlive

We are going to be leveraging the awesome tools that are provided by pandoc in order to make really cool things happen with our text editor! This even supports syntax highlighting! Once you have installed all of those pre requisites you can save a text file as markdown using the .md extension and then convert it to HTML, PDF, or other formats using pandoc. Simply open the command launcher with Control+Shift+P type in pandoc and select your output format.

Now you can turn this:

Sample Python Code
Sample Python Code

Into this:

Sample Python Code Pdf
Sample Python Code Pdf

Awesome! Next steps are to level up your Sublime Text skills with Mastering Sublime Text.

Bash Alias for emacs -nw

Emacs is an awesome text editor with a slightly steep learning curve. However, once you have mastered the basics of emacs your life will change forever.

One little annoying thing that I have found is that when you install emacs in Ubuntu, OS X, and probably a couple other distributions typing in emacs in the terminal will open the emacs GUI. This seems silly to me because the best part about emacs is that it does not require a GUI and runs natively in the terminal. In addition, running emacs in the terminal will force you to learn how to use it, and these skills will come in handy when working on different types of operating systems or in systems that do not have a GUI at all.

In order to get emacs to run in the terminal you must invoke it using the -nw argument which stands for “no window system”. If you wish to automatically open emacs in a terminal rather than in the GUI when you type the emacs command you can simply make a bash alias that changes the meaning of the command.

These are the steps to create a bash alias for emacs -nw:

  1. Open up the .bashrc file from the terminal with your favourite text editor.
    emacs ~/.bashrc</pre>
  2. Add the following line to the bottom of the file
    alias emacs='emacs -nw'</pre>
  3. Save this file, and then reload your bash profile
    source ~/.bashrc</pre>

You should now be able to type in emacs into your terminal and see emacs open inside of the terminal. Yay! Happy Hacking.

Connect Libreoffice Base to MySQL

I think LibreOffice Base has so much underutilized potential as a rapid application development platform, business intelligence platform and just a general reporting platform. Not to mention the fact that registered data objects can be used in all of the other LibreOffice applications to make really amazing documents and improve the work-flow of any office. Anyone who has actually used MS Access knows how powerful it can be used to be for these types of purposes. The most recent version of Access seems to have lost a lot of the features that made it useful. This is okay since most power-users are still using Access 2003.

LibreOffice Base is not nearly as powerful as Access, specifically from a usability perspective. My biggest frustration with getting started with LibreOffice Base is the obscure and somewhat cryptic documentation around the platform that makes way too many assumptions about what someone new to LibreOffice actually knows. My hope is to provide some practical tutorials with real world use cases. So let’s get started by connecting Base to an existing MySQL database.

In my opinion, the built in HSQL engine has a somewhat weird syntax and is generally not worth learning unless you are not planning on ever actually writing any SQL and only using the built in wizards. I would prefer to work with MySQL databases because they are ubiquitous, use a “standard” syntax and very powerful. In addition most practical office use cases will involve a central database and not a local database.

Preparing Your MySQL Server

This is the part of the documentation that I find most obscure and confusing, so here is how to do it using LibreOffice 4.3 running on Ubuntu 14.04 LTS. The steps here will be slightly different depending on if you are developing on a local database or a remote database. If you are using a local MySQL database please feel free to skip this section.

I do most of my database development inside of Linux Containers which essentially makes my databases “remote”. In order to allow remote connections we need to make a few changes to the default MySQL configuration. Please note that if you are doing these steps on a live production system you will need to be extra careful with users, permissions, ports that are opened, etc.. This falls outside of the scope of this tutorial but the rule of thumb is that if your database accepts connections from the outside world you should white list each IP address that will be connecting to it and block all others. The easiest way to do this in my opinion is with your firewall.

By default MySQL only runs on the local host and is not accessible from remote hosts. To change this setting you need to edit the my.cnf file.

1) Open up my.cnf which is found in /etc/mysql/my.cnf

2) Find the bind-address and change if from the local host to the IP address of the server.


3) Restart MySQL

sudo service mysql restart

Install the MySQL JDBC Driver

On Ubuntu 14.04 this is very easy and can be done by running the following command:

sudo apt-get install libmysql-java

Configure the Class Path in LibreOffice

Open up any LibreOffice App and go to Tools -> Options

On the right hand side navigate to LibreOffice -> Advanced

Select on the Class Path… button and load the new driver that was installed in the previous step. In order to do this you will need to select **Add Archive… **and select /usr/share/java/mysql.jar

Once this has been loaded restart LibreOffice

Connect to your Database

Now comes the fun part. Now that we have taken care of all of the previous steps the rest is easy. To connect to your database open up LibreOffice Base.

  1. In the Database Wizard select Connect an existing database and chose the MySQL option from the dropdown menu.
  2. Select Connect using JDBC and hit next
  3. Enter the database name, server IP, port number and select next
  4. Enter the username for an existing user in your database and select next
  5. If you wish to use this database in other LibreOffice applications you should select the **Yes, register the database for me **radio button.
  6. Select Finish

Congratulations! Now you can rock some custom queries, fancy forms, and TSP reports using Base. We will go through how to do all of that an more in future posts.