Tag Archives: Script

Nicer pdf2png, with poppler

Updated: so poppler now includes pdftocairo which does this. No need to do this anymore! Blog post here for reference.

I’ve been using convert from ImageMagick to convert PDF-files to png files. However, they’re butt ugly, or rather fugly. So I created a pdf2png script / python program to do it better.

Just look at the text here from convert:

Rather ugly. Look at the kerning. It’s truly horrible.

Not to say poppler doesn’t have its share of problems, but it looks rather much better, don’t you agree?

So, since I had to manually edit a presentation I had to use some time making a PDF-to-PNG converter since I couldn’t find another pdf2png.

So without further ado, here is pdf2png.py (updated to use cairo as Raimund posted code for in comments, also updated with width+height extra params):

#!/usr/bin/env python
 
import poppler
import cairo
import gtk
import urllib
import sys, os
 
width = height = 0
 
if len(sys.argv) != 2 and len(sys.argv) != 4:
    print("Usage: %s <filename> [width height]")
    sys.exit()
 
if len(sys.argv) == 4:
    width = sys.argv[2]
    height = sys.argv[3]
 
input_filename = os.path.abspath(sys.argv[1])
output_filename = os.path.splitext(os.path.basename(sys.argv[1]))[0] + '-%.2d.png'
 
doc = poppler.document_new_from_file('file://%s' % \
            urllib.pathname2url(input_filename), password=None)
 
for i in xrange(doc.get_n_pages()):
    page = doc.get_page(i)
 
    if width and height:
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
        ctx = cairo.Context(surface)
    else:
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                int(page.get_size()[0] * 2), int(page.get_size()[1] * 2))
        ctx = cairo.Context(surface)
        ctx.scale(2, 2)
 
    page.render(ctx)
    ctx.set_operator(cairo.OPERATOR_DEST_OVER)
    ctx.set_source_rgb(1, 1, 1)
    ctx.paint()
    surface.write_to_png(output_filename % i)

It’s very far from perfect. Note the hard coded height and width, all of these things are possible fixes. Not anymore! The default should be sensible now, or you can force it with arguments. I didn’t find any python-poppler documentation, but I used C++-docs instead, they were helpful enough.

If you do any improvements (thanks Raimund) or just use it, it’d make me happy if you told me in a comment. :-)

Send files via SSH/SCP with Nautilus

I wanted a quick and easy way to share files, images, scripts etc. I searched for this about two years ago, but couldn’t find it. So I wrote my own, and wrote about it on my norwegian blog.

Recently Omg! Ubuntu! wrote about a Nautilus Imgur-uploading script, and I wrote a small comment saying that I’ve used the same for some time, only to my own servers and using SCP. I said it also supported FTP, but I don’t know where I took that from, it doesn’t and I have no need for FTP :-)

Anyway, this is how it works:

  1. Right click file (or files!) in Nautilus
  2. Choose script -> Send to Thor (where Thor is the name of a server)
  3. Wait a bit, and you will get a small notification saying that the file is uploaded
  4. To make it easy to share, the script sets your selection clipboard to the url, so that you can just paste away

I use this directory structure, mostly:

example.com/rubbish/$name_of_computer/uploaded_file

You may download the send_via_scp script here.

I’m also pasting in this version of the script, just so that it doesn’t disappear.

[code lang=”bash”] #!/bin/bash # Send your files to another machine using SCP # REQUIRED # – Nautilus (doh!) # – scp # – notify-send (from package notify-bin, libnotify – for messages) # – xclip (to automaticaly set your clipboard to the URL of the file) # To use (for single server): # 1. Copy to ${HOME}/.gnome2/nautilus-scripts # 2. Rename to "Send to MYSERVER" and make executable (chmod a+x) # 3. Change the options below # # To use (for multiple servers): # 1. Copy to your ${HOME}/.gnome2/nautilus-scripts directory # 2. Make a file named "Send to MYSERVER" in the same directory # 3. Put this into it: # ————— # #!/bin/bash # # SCP_URL="odin@odin.s0.no:public_html/rot/`hostname`/" # HTTP_URL="http://odin.s0.no/rot/`hostname`/" # PROGNAME=`basename "$0"` # dir=`dirname "$0"` # # export SCP_URL HTTP_URL PROGNAME # source $dir/send_via_scp # ————— # # 4. Make it executable (you don’t have to make send_via_scp executable) # Author: Odin Hørthe Omdal < odin.omdal at gmail [dot] com > # Version: 1.1 # Based upon the script to make symbolic links by # Author: Jon Green < g-scripts [at] green-lines [dot] com > # Version: 1.0 if [ "x$SCP_URL" == "x" ]; then # OPTIONS, edit these SCP_URL="user@domain:public_html/folder/" HTTP_URL="http://domain/folder/" # This is the name it will use for notifications PROGNAME=`basename "$0"` fi #### You don’t have to edit below here ### warning() { notify-send -i gtk-dialog-error "$PROGNAME" "$*" } message() { notify-send -i gtk-dialog-info "$PROGNAME" "$*" } declare -a NAUTFILES export IX=0 while read FILE; do if [ "x${FILE}" != "x" -a "x${FILE}" != ‘x"’ ]; then NAUTFILES[${IX}]="${FILE}" IX=$[ ${IX} + 1 ] fi done <<EOF ${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS[@]} EOF if [ 0 -eq ${IX} ]; then URI="${NAUTILUS_SCRIPT_CURRENT_URI}" METHOD="${URI:0:7}" if [ "file://" == "${METHOD}" ]; then NAUTFILES[0]="${URI:7}" IX=1 fi fi if [ 0 == "${#NAUTFILES[@]}" ]; then warning "Nothing to do" exit fi for FILE in "${NAUTFILES[@]}"; do FILE_NAME=`basename "${FILE}"`; if ! scp "${FILE}" "$SCP_URL"; then warning "Couldn’t send ${FILE_NAME}" else message "Uploaded to <a href=’${HTTP_URL}${FILE_NAME}’>${HTTP_URL}${FILE_NAME}</a>" fi # Save the URL to the clip board echo "${HTTP_URL}${FILE_NAME}" | xclip done [/code]