Macos – How to download wkhtmltopdf on a Mac OS X

installationmacosterminal

I need to be able to automatically generate pdf files which contain puzzles with random numbers. The best way I can think of do to this given my knowledge is to make it with Javascript/HTML/CSS and convert this to a PDF with a program, but other suggestions are welcome.

Anyway, so I tried downloading the program from this website but I ran into a few problems. First of all, from the list on the side, what should I download? I just see a bunch of random numbers and file extensions that don't mean anything to me. I tried the top one on the list and the one called "wkhtmltopdf.dmg".

Secondly, once I download the file, how do I install and use the program? It seems that on most files I have downloaded, it gives me a .dmg file. I don't know what a .dmg file is, but usually when I download stuff I just somehow know what to click on and suddenly the program is running. In this case, the .dmg file doesn't lead to anything, and I think I am expected to go into the terminal and work some magic. I tried using terminal but I just don't understand how it works. I was able to "cd" my way to the folder containing the file I downloaded. Then I tried running the commands that the link above told me to run: wkhtmltopdf www.myhomepage.com myhomepage.pdf. However, I keep getting wkhtmltopdf: command not found.

It's a common theme in my life that I can't figure out how to install anything unless it is wildly obvious how to install it (click icon, installer leads through steps, drag to application folder, run program). What is the piece of knowledge that I am missing? What is the book I haven't read yet that explains why I don't understand something so simple?

Thank you!

Best Answer

I just needed to install the same thing for a project that converts bunch of HTML pages to a PDF.

I had to do several things to get it working on my machine. First of all, I downloaded the DMG.

After download, just click on it, it should mount automatically (if not, check out the mount commando). Now it's like a "disk" attached to your machine, usually available on your desktop.

Open it in your Finder, and inside is a wkhtmltopdf.app. You need to copy this to your Applications where all your other programs (should) reside.

Inside your terminal you can then execute the command to use wkhtmltopdf

For example:

/Applications/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf "http://www.google.com" google.pdf

This will create a PDF called "google.pdf" from the provided URL. This is sort of annoying to type the whole path, so what I tend to do is create a symbolic link to it so I can shorten my type work (especially since I use it fairly often nowadays)

ln -s /Applications/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf wkhtmltopdf

This will create a symbolic link to wkhtmltopdf in my current directory and is called wkhtmltopdf.

The terminal input is now shortened to: wkhtmltopdf "http://www.google.com" google.pdf

There are also several params you can enter, for example when you want to convert a page that requires a user and password in order for you to access it. Note that the username & password is used to access the page, not to login in the page itself.

Your command would then look like this:

wkhtmltopdf --username "mydomain\myusername" --password "mypassword" "https://my-secure-url" ../Reports/my-pdf-name.pdf

This is basically the same as before. Just now I was required to enter my username and password for the secure site. I put the resulting PDF inside a Reports folder which is located a level above of my current location in the tree structure of folders.

Related Question