How to print Github wiki pages

I have no idea how it's supposed to work (or why the don't have a print CSS), but this works for pages written with Markdown:

  1. "Edit" the wiki page
  2. Copy all text
  3. Run a Markdown interpreter and pipe its result, e.g.: kramdown > /tmp/github.html
  4. Paste your markdown
  5. Press Ctrl-D to finalize your input
  6. Open the generated HTML file and print it.

O_o

How to embed images in higher resolutions for printing

When you print out a HTML pages, all raster images (like PNGs) will appear aliased. This is because a printer's resolution is usually much higher than that of a computer screen.

If an image absolutely must look awesome when printed, a solution is to embed the image in much higher solution than needed (e.g. four times the horizontal resolution), then scale it down to the desired width using CSS.

Note that this will slightly alter the image's appearance on the screen because browsers will scale down the image [using an anti-aliasing method](...

Use a Ruby method like a block or lambda

Sometimes you want to use a vanilla Ruby method like a block. You can use Object#method to obtain a method reference that responds to #call:

foo_plus = "foo".method(:+)
foo_plus.call("bar") # => "foobar"

The method reference also understands #to_proc so you can feed it to block-taking methods by prefixing it with &:

printer = method(:puts)
[1, 2, 3].each(&printer) # will print one line per number

Fix: "undefined method `bytesize' for #<Array>"

I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:

"undefined method `bytesize' for #<Array>"

Starting the application in Passenger gave me a stacktrace in log/development.log that pointed to the actual problem.

Possible causes discovered by looking at the logs
-----------------------------------------------------...

Invoices: How to properly round and calculate totals

While it might seem trivial to implement an invoice that sums up items and shows net, gross and vat totals, it actually involves a lot of rules and caveats. It is very easy to create invoices where numbers don't add up and a few cents are missing. A missing cent is a big deal for an accountant, so it is important for your invoices to list correct numbers.

Note that this is not legal advice. Also note that while this note has a number of code examples in Ruby and MySQL, the concepts apply to all programming languages and data stores.

When ...

Convert the colorspace of a PDF from RGB to CMYK under Ubuntu Linux

Note that converting from RGB to CMYK will usually degrade your colors because no exact mapping is possible. Anyway, this Stackoverflow post worked for me:

gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite \
-sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK \
-sOutputFile=output.pdf input.pdf

Rotate a PDF under Ubuntu Linux

Use the PDF toolkit:

sudo apt-get install pdftk

To rotate page 1 by 90 degrees clockwise:

pdftk in.pdf cat 1E output out.pdf    # old pdftk
pdftk in.pdf cat 1east output out.pdf # new pdftk

To rotate all pages clockwise:

pdftk in.pdf cat 1-endE output out.pdf    # old pdftk
pdftk in.pdf cat 1-endeast output out.pdf # new pdftk

The E (old pdftk) or east (new pdftk) is meaningful if you want other rotations. From the man page:

The page rotation setting...

Unpack a .tar.gz archive

You can unpack a .tar.gz file into the current directory like this:

tar -xzvf archive.tar.gz

The options used are

-x
: Extract

-z
: Unzip

-f
: Process a file input

-v
: Be verbose, i.e. print each extracted file

Side notes:

  • The dash is optional (tar xzvf ... works as well).
  • For .tar.bz2 archives, use j instead of z (tar xjvf ...`)
  • In order to create a *.tar.gz archive, use -c flag instead of -x flag.
  • The order of parameters matters.

Printing to PDF files on Ubuntu

This will install a printer called "PDF":

sudo apt-get install cups-pdf

Files will be put into ~/PDF/ without any questions asked.

Fix randomly failing PDF cucumber tests

Sometimes PDF cucumber tests fail at the first test run and succeed at the second run. You can fix this with this step:

When PDF generation is ready
And I follow "Save as PDF"
# Checks for PDF contents go here

Here is the step definition:

When /^PDF generation is ready$/ do
  Then 'I should see ""'
end

Install LaTeX on Ubuntu 10.10

The Ubuntu Documentation on LaTeX says, that the packages tetex are no longer supported. You can install the alternative texlive (380 MB) via apt-get.

sudo apt-get install texlive

Fix errors when rendering PDF output

If you run specs or your application and get an error like:
ActionController::MissingFile in 'ProductsController#show, should render PDF'
Cannot read file /some/file.pdf

You may be missing the HTMLDOC binary on your system. Install it like this on Debian/Ubuntu:
sudo apt-get install htmldoc