How to convert PDF to image

You may sometimes want to convert a PDF file into an image format like PNG or JPG. Doing this is easy using the convert application from ImageMagick.

If you do not have it, first install ImageMagick:

$ sudo apt install imagemagick

To convert a PDF with a single page into a JPG:

$ convert foo.pdf foo.jpg
$ ls
foo.pdf
foo.jpg

If the PDF has multiple pages, one image file is produced per page:

$ convert foo.pdf foo.png
$ ls
foo.pdf
foo-1.png
foo-2.png
foo-3.png

By default, the image file is produced at 96 DPI resolution of the PDF. If you need higher DPI, use the density option. For example, to generate at 300 DPI:

$ convert -density 300 foo.pdf foo.png

Tried with: ImageMagick 6.7.7-10 and Ubuntu 14.04

wxWidgets Examples

Starting off with a GUI library can be quite daunting. Lots of new code and classes need to be written to get even the most basic program working. I have found that the best way to get started with wxWidgets is to look at its examples. Pick the example that implements most of what you need and start off from there.

Doing this is easy:

  • Install the wxWidgets examples:
$ sudo apt-get install wx3.0-examples
  • The examples are zipped up and need to be uncompressed before using them:
$ cd /usr/share/doc/wx3.0-examples/examples
$ ./unpack_examples.sh ~/wx-examples
  • You can build all the examples in one command:
$ cd ~/wx-examples/samples
$ make
  • Or you can go to a specific directory and build a specific example:
$ cd ~/wx-examples/samples/opengl/penguin
$ make

Tried with: wxWidgets 3.0 and Ubuntu 14.04

How to get mouse position in wxWidgets

To get mouse position relative to top-left corner of display, add this in a mouse event handler:

const wxPoint pt = wxGetMousePosition();
int mouseX = pt.x;
int mouseY = pt.y;

To get mouse position relative to top-left corner of current window or canvas, add this in a mouse event handler:

const wxPoint pt = wxGetMousePosition();
int mouseX = pt.x - this->GetScreenPosition().x;
int mouseY = pt.y - this->GetScreenPosition().y;

Tried with: wxWidgets 3.0 and Ubuntu 14.04

Segmentation fault at glCreateShader in wxWidgets

Problem

I had a simple graphics program written using OpenGL and using GLUT for windowing. To get more windowing functionality, I wanted to move it to wxWidgets. I picked an example program that uses OpenGL in wxWidgets. It compiled and worked correctly. When I placed my OpenGL code into this example, I got a segmentation fault at the glCreateShader call in my code.

Solution

I had a glewInit in my original code. I had put this in the application class constructor. Apparently, wxWidgets requires that the first OpenGL or GLEW call be placed inside the OnPaint event handler function. Look for EVT_PAINT to find this function. Once I did that, the error disappeared and OpenGL worked.

How to add a docset to Zeal

Zeal can add docsets shared by Dash automatically over the internet. You can do this using Edit > Options > Docsets > Download.

Sometimes, you may need a docset for a library that has not yet been added by Dash. Typically, you can find docsets created by others and shared on the internet. These can also be loaded into Zeal easily:

  1. Download the docset zip file. Unzip it to obtain a docset directory.

  2. Copy this directory to ~/local/share/zeal/docsets

  3. Restart Zeal. You should be able to see your docset loaded.

Tried with: Zeal 20140215 and Ubuntu 14.04

How to build wxWidgets program using CMake

Configuring a project that uses wxWidgets to build on Windows is quite simple. However, on Linux the same task is a bit more involved and requires using the wx-config program.

CMake has support for wxWidgets, but requires knowing the right incantations to build. A minimal CMakeLists.txt for building a wxWidgets program is given below:

This includes the core and base wxWidgets libraries. If you use any other wxWidgets modules, like wxGLCanvas for example, then include the relevant modules, like gl in the find_package command.

Tried with: wxWidgets 3.0, CMake 2.8.12.2 and Ubuntu 14.04