View Single Post
Old 02-09-2008, 10:11 AM   #1
myelinator
Junior Member
myelinator began at the beginning.
 
Posts: 6
Karma: 10
Join Date: Feb 2008
Device: kindle
Lightbulb All on the Mac: PDFs with Formatting!

Hello, all.

I noticed that there were many people who were struggling to find a way to make formatted PDFs for their Kindle with their Mac without resorting to MobiPocket creator. I needed to do the same thing and was getting frustrated with being forced to use an emulator. Here is the method I came up with. It is rather technical and requires some UNIX terminal knowledge and is just my quick and dirty homebrew method. However, it took a signficant amount of time to develop and I wanted to make sure to share it with others.

Please understand that I am extremely busy and don't get to spend as much time doing this as I might like. (I've been using this for over a month and just now have had a moment to sit down and type this.) Therefore, I may or may not have time to answer questions, but I've tried to be as clear as I can.

This program relies on two other pieces of software:
  1. Mobiperl (http://www.mobileread.com/forums/sho...&highlight=prc) and its dependencies (perl, etc.)
  2. ImageMagick (http://www.imagemagick.org/)

After you have downloaded Mobiperl you need to make modifications to the source perl to optimize it for use with the Kindle. In the file [...your perl libraries...]/MobiPerl/Util.pm in the subroutine get_image_data() change the lines that read:
Code:
my $maxwidth = 480;
my $maxheight = 640;
to:
Code:
my $maxwidth = 525;
my $maxheight = 700;
Now you are ready for the shell script that puts it all together (I named it pdf2mobi):
Code:
#!/bin/bash

for FILE in "$@"; do
FILEPATH=${FILE%/*}
FILENAME=${FILE##*/}
FILEBASE=${FILENAME%%.*}
FILEEXT=${FILENAME#*.}
IMGEXT=jpg

# if my experiments are right then the size of the Kindle screen is 525x700
# (a 4:3 aspect ratio) as one might expect

# use imagemagick to do all the needed conversions:
# -density              166x166 to get the right dpi
# -trim                 off the margins (do it before you resize!)
# -resize 700           to get the width of the pages maximized
# -rotate -90           to maximize the use of the screen
# -colorspace gray      self explanatory
# -type Grayscale       self explanatory
# -level                darken the image
# +dither               turn off dither
# -level 34%,100%,1.2   darken the image
# -map kindle.gif       a four color gif with the allowed shades of gray
echo "**********************************************************************"
echo "* Converting to $FILE to $IMGEXT files... please be patient..."
echo "**********************************************************************"
convert -density 166x166  "${FILEBASE}.pdf" -trim +repage -resize 700 -rotate -90 -colorspace gray -type Grayscale -level 34%,100%,1.2 +dither -map kindle.gif "${FILEBASE}.${IMGEXT}"

echo "Dividing the $IMGEXT page images..."
for PAGE in "${FILEBASE}"*.${IMGEXT}
  do
    PAGEBASE=${PAGE%%.*}
    echo $PAGEBASE
    convert "${PAGEBASE}.${IMGEXT}" -crop 525x700+0 +repage "${PAGEBASE}-01.${IMGEXT}"
    convert "${PAGEBASE}.${IMGEXT}" -crop 525x700+450 +repage "${PAGEBASE}-02.${IMGEXT}"
    convert "${PAGEBASE}.${IMGEXT}" -crop 525x700+900 +repage "${PAGEBASE}-03.${IMGEXT}"
    rm "${PAGE}"
done

echo "Writing the HTML file..."
cat > "${FILEBASE}.html" <<HEAD
<html><head><guide></guide></head><body>
HEAD
for IMG in "${FILEBASE}"*.${IMGEXT}
  do
    echo "<img src='${IMG}'/><mbp:pagebreak/>" >> "${FILEBASE}.html"
  done
echo "</body></html>" >> "${FILEBASE}.html"

echo "Converting HTML to MOBI..."
html2mobi --title "${FILEBASE}" --author "ME" "${FILEBASE}.html"

#echo "Cleaning up..."
rm "${FILEBASE}.html" "${FILEBASE}"*.${IMGEXT}
done
Please share any further optimizations and thoughts... The main current issue with this program is that it can not handle very long PDF documents because ImageMagick chokes on 100s of pages to convert at once. I have not had time to make it read and convert to jpg one page at a time, but it should be relatively trivial.

Good luck and enjoy!
myelinator is offline   Reply With Quote