Using Kindlegen with PHP on Linux to Create Kindle Files

I’m working on a side project at the moment that requires the conversion programatically of a page of html to something that can be consumed by an Amazon Kindle. I did a quick search to see if anything existed as a PHP class library that I could use and while there was they were either hugely bloated or too alpha for my needs.

I then stumbled upon Amazon’s command line tool KindleGen which allows conversion of HTML and ePub docs to the MOBI format that the Kindle requires. This is a multi-platform too and, crucially, a version for Linux is available.

Installation is a simple case of copying the single file to an appropriate place on your server, such as /usr/local/bin. Then create a new folder somewhere and make sure that the web process has write access to it. On Ubuntu this would be, for example, by :

sudo chown www-data:www-data /var/www/kindle

In my case I needed to convert some HTML to MOBI format and while experimenting found that it was very important to have well formed code, particularly with html and body tags. The other thing that you might like to consider is including a title tag as this is what is used as the name in the Kindle library and if this is omitted Amazon will use the name of the attached file instead.

To convert the file you simply need to pass to kindlegen the name of the html file and the output filename – note that you don’t need to give the path for the output file as it is created in the same place as the source. In PHP you can use “exec” to call a Linux command:

exec('kindlegen ' . '/var/www/kindle/input.html' . ' -c0 -o ' 
. 'output.mobi' );

If you were allowing a user to enter their own HTML that you were going to process this way I would highly recommend sanitizing the input first!

As a full example of this the following code stub will convert html in the $content variable and then send the resulting file to your Kindle email.

<?php

$content = "<html><head><title>Your title</title></head><body>
            <p>Your Content</p></body></html>";

// create the input file
$filename = date('Ymd_His');
$body = file_put_contents($filename.'.html',$content);

// convert to mobi format
exec('kindlegen ' . '/var/www/kindle/'.$filename.'.html' . 
     ' -c0 -o ' . $filename.'.mobi' );

// send the file as an attachment to your Kindle
$mail = new PHPMailer();
$mail->IsSendmail(); 
$mail->AddReplyTo('registered @ domain.com'
 ,'First Last');

// this address must be registered with your Amazon account
$mail->SetFrom("registered @ domain","First Last");

// this is the email address of your Kindle
$mail->AddAddress("your_address @ kindle.com", "First Last");

// the next two are required by PHPMailer but not by Amazon
$mail->Subject  = "";
$mail->MsgHTML(" ");

// add the mobi file
$mail->AddAttachment('/var/www/kindle/'.$filename.'.mobi'); 

// send the file
if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
          
// delete the files created
unlink('/var/www/kindle/'.$filename.'.mobi');
unlink('/var/www/kindle/'.$filename.'.html');

?>

One comment on “Using Kindlegen with PHP on Linux to Create Kindle Files

  1. Hi!

    What version of kindlegen do You use?
    I downloaded newwest version from Amazon and it does not work, while windows version (even with Wine) works fine.

    Regards!

Leave a Reply

Your email address will not be published. Required fields are marked *