View

Bootstrap: How to avoid printing link URLs

By default, Twitter Bootstrap's print styles include printing links.

/* Bootstrap's way of printing URLs */
@media print {
  a[href]:after {
    content: " (" attr(href) ")";
  }
}

If you want to turn that off, you can do

/* Don't print link hrefs */
@media print {
  a[href]:after {
    content: none
  }
}
Linked content

Fixing tlmgr cannot setup TLPDB

tlmgr is the TeX Live Manager and responsible for the TeX installation on your (Linux) machine.

If you're getting the message:

(running on Debian, switching to user mode!)
cannot setup TLPDB in /home/dominik/texmf at /usr/bin/tlmgr line 5336.

tlmgr has not been initialized. Run this to initialize it:

tlmgr init-usertree

Then, you may set e.g. a global paper size standard: tlmgr paper a4.

How to install the `xelatex` binary on Ubuntu 14.04

Just install the texlive-xetex package:

sudo apt-get install texlive-xetex

Running integration tests without texlive-xetex will produce an error during xelatex execution:

RTeX::Document::ExecutableNotFoundError

When Sass-generated stylesheets print a Encoding::CompatibilityError

We upgraded a Rails 2 application to Rails 3.2 and Ruby 2.1, changed the mysql adapter from mysql to mysql2, but did not activitate the asset pipeline. Instead we used Sass the old-school way (stylesheets in public/sass/*.sass) and relied on stylesheet_link_tag to activate the Sass compiler.

Now all Sass-generated stylesheets inserted the following text into body:before:

Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT

I could get rid of this by removing all generated .css files in `…

pdfkit/wkhtmltopdf: When a header is invisible

If you're using the :header_html option in PDFKit (or the corresponding --header-html option in wkhtmltopdf), and the header remains invisible, you need to add this to your header HTML:

<!doctype html>

The same applies to footers via footer_html

I'm sorry.

Linux: Running a program with a different locale than your default

When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.

I will use the date command and print the current weekday, just for the sake of an example.

Changing the locale using environment variables

Most often, setting LC_ALL for your command should be enough. The following was run on a system using a German locale.

$ date +%...
Linked content

What we know about PDFKit

What PDFKit is

  • PDFKit converts a web page to a PDF document. It uses a Webkit engine under the hood.
  • For you as a web developer this means you can keep using the technology you are familar with and don't need to learn LaTeX. All you need is a pretty print-stylesheet.

How to use it from your Rails application

  • You can have PDFKit render a website by simply calling PDFKit.new('http://google.com').to_file('google.pdf'). You can then send the…
Linked content

Lightweight PDF viewer: MuPDF

MuPDF is a PDF reader that renders very quickly, yet still correctly.

It supports PDF 1.7 and all the fancy shenanigans that evince (Ubuntu's default PDF reader) fails to render properly.

On Ubuntu, MuPDF is available in the Universe sources. Simply install via APT:

sudo apt-get install mupdf

Interaction primarily happens via keyboard, but there is basic mouse support.\
See the manpage for more details on navigating.

One downside: There is no printing support, so if…

A Ruby script that installs all gems it is missing

So you want your Ruby script to install missing gems instead of dying? Take this method:

def installing_missing_gems(&block)
  yield
rescue LoadError => e
  gem_name = e.message.split('--').last.strip
  install_command = 'gem install ' + gem_name
  
  # install missing gem
  puts 'Probably missing gem: ' + gem_name
  print 'Auto-install it? [yN] '
  gets.strip =~ /y/i or exit(1)
  system(install_command) or exit(1)
  
  # retry
  Gem.clear_paths
  puts 'Trying again ...'
  require gem_name
  retry
end

Use it like this:

```rb
insta…

Rails 2: Refuse response formats application-wide

If you regularly get ActionView::MissingTemplate exceptions, maybe some bot visits your site requesting silly formats like:

http://www.rails-app.com/makandra.html-username-2000 # => Rails tries to retrieve 'makandra' with format 'html-username-2000'

Just restrict accepted format parameters for the whole application like this:

class ApplicationController < ActionController::Base
  
  before_filter :refuse_silly_formats

  private

  def refuse_silly_formats
    acceptable_formats = %w[html xml pdf]

    if par...
Linked content

Linux: How to print PDF files from the command line

Sometimes you may want to print files from the command line, especially when you have lots of them.
You can use lp for that.

To print a single example.pdf file on your default printer, simply say:

lp example.pdf

lp accepts multiple filenames, so to print all PDF files in the current directory:

lp *.pdf

You can specify a printer via the -d switch:

lp -d YOUR_PRINTER_NAME *.pdf

Your printer's name is then one you defined on your system. You can check with your CUPS configuration by visiting <http://localhost:631/…

Linked content

PDF Generation in Rails

A look at Prawn, PDFKit, and Wicked PDF

Bash: Heavy headings for CLI

To print a colored full-width bar on the bash, use this bash script expression:

echo -e '\033[37;44m\nHEADING\033[0m\nLorem ipsum ...'

In Ruby:

  puts "\033[37;44m\n #{text}\033[0m" # blue bar

Notes: -e turns on escape character interpretation for echo. See this card for details on bash formatting.

The line above will print:

How to load only a subset of a massive MySQL dump

I had a huge MySQL dump that took forever (as in: days) to import, while I actually just wanted to have the full database structure with some data to use on my development machine.

After trying several suggestions on how to speed up slow MySQL dump imports (which did not result in any significant improvement), I chose to import just some rows per table to suffice my needs. Since editing the file was not an option, I used a short Ruby script to manage that.

Here is how:

pv huge.dump | ruby -e 'ARGF.each_line { |l| m = l.match(/^INSERT ...

Bash: How to only do things in interactive shells

When you print something from within your .bashrc file you will run into trouble when copying something onto your machine using scp for example.

This is because the output from your .bashrc interferes with scp. The solution is to decide whether the bash shell is started interactively (you start a terminal on your screen) or not (scp).

if [ ! -z "$PS1" ]; then
  # This happens in interactive shells only and does not interfere with scp.
  echo "Learn!"
fi

Howto provide a single page preview for PDF & TXT with carrierwave

Assert rmagick provision …

Gemfile

gem 'rmagick', '2.13.2' # at this moment the latest stable version

config/initializer/carrierwave.rb

require 'carrierwave/processing/rmagick'

… and define a custom processor

MyUploader.rb

```
class MyUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick

def cover
manipulate! do |frame, index|
frame if index.zero? # take only the first page of the file
end
end

version :preview do
process :cover
process :resize_to_fit => [310,…

Don't use PDFKit v0.5.4 – it breaks on dead urls

When I upgraded from 0.5.4 PDFKit deadlocked or crashed when an ApplicationController::RouteError occured. If this error happens only because of a broken image link you probably still wan't the page to render and get a PDF with a "missing image placeholder" instead.

I don't know yet in which version (0.5.2, 0.5.3 or 0.5.4) the bug was built in. At the moment I'm doing fine with 0.5.1 and I didn't evaluate any other versions yet.

I will update this card as soon as I know further information (State 4th Nov. 2013).

Fix „command failed: /usr/bin/wkhtmltopdf ...“ using PDFKit middleware

Ubuntu 12.04 LTS x64, Ruby 1.8.7, Rails 2.13, PDFKit 0.5.4, Phusion Passenger Apache 2

I ran into this, when I started using passenger to deal with the Single Thread Issue which caused my webrick to deadlock when an ActionController::RoutingError (No route matches "...") occurred.

These steps brought me a little further

(1) assert dependencies are installed sudo aptitude install openssl build-essential xorg libssl-dev
(2) only for 64bits OS Run one by one the follo…

This website uses cookies to improve usability and analyze traffic.
Accept or learn more