Print the Document on Your Kindle
These days, electric devices for viewing documents have been common. Amazon Kindle and iPad are typical examples for the devices. We can transfer our PDFs or other documents from my computers to these devices using email or cables. This entry introduces a method to print documents on these devices in the same way as the case of printing on paper
紙に印刷するように,プリンタダイアログで「Kindleに印刷」ってのができるようになりました.これを選択すると数分で自動的にKindleにドキュメントが転送されます.
How it works
Printing documents on Kindle is quite simple. Let's see this screenshot of a printing disalog. You see an interesting option in PDF options.
By selecting the entry "Send AZW to Your Kindle", your kindle gets the document that is converted to Kindle format in few minutes.
Implementation
Normally there is no option "Send AZW to Your Kindle" in your printing dialog. I created the option. Mac OS X provides a interface to manipulate PDF in its printing dialog. Once you create a shell script on the directory "/Library/PDF Services", the name of the shell script appears in the menu of printing dialog.
Here, I put a following shell script to implement the menu in the above.
/Library/PDF Services $ cat Send\ AZW\ to\ Your\ Kindle #!/bin/sh # # /usr/bin/python /Users/suztomo/Documents/gmail_python/kindleSender.py "$1" "$3" \ >> /Users/suztomo/Documents/gmail_python/stdout \ 2> /Users/suztomo/Documents/gmail_python/stderr rm "$3"
When you select the option in printing dialog, a temporary file path of the document and the document's name are passed to the shell script, where they are sent to kindleSender.py again.
And kindleSender.py sends a mail to my Kindle's mail address given a path name of the temporary PDF file. Documents sent to the mail address are converted to Kindle format and automatically delivered to my Kindle.
#!/usr/bin/python2.6 # -*- coding: utf-8 -*- import smtplib from datetime import datetime from sys import argv from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.header import Header from email.utils import formatdate def create_message(from_addr, to_addr, subject, body, encoding, attachmentpath, attachmentname): msg = MIMEMultipart() msg['Subject'] = Header(subject) msg['From'] = from_addr msg['To'] = to_addr msg['Date'] = formatdate() related = MIMEMultipart('related') alt = MIMEMultipart('alternative') related.attach(alt) content = MIMEText(body, 'plain', encoding) alt.attach(content) fp = file('%s' % attachmentpath, 'rb') pdf = MIMEApplication(fp.read(), 'pdf', name=attachmentname) pdf.add_header('Content-Disposition', 'attachment', filename=attachmentname+".pdf") related.attach(pdf) msg.attach(related) return msg def send_via_gmail(from_addr, to_addr, msg): s = smtplib.SMTP('smtp.gmail.com', 587) s.ehlo() s.starttls() s.ehlo() s.login(from_addr, "password") s.sendmail(from_addr, [to_addr], msg.as_string()) s.close() d = datetime.today() print "sent a mail %s" % (d.strftime("%Y-%m-%d %H:%M:%S")) if __name__ == '__main__': name = argv[1] if len(argv) == 3 else None path = argv[2] if len(argv) == 3 else None if not name or not path: print "specify file path and name to convert" exit() print "Received: %s" % (name) from_addr = 'your mail@gmail.com' to_addr = 'your kindle mail@free.kindle.com' title = 'convert' body = '' msg = create_message(from_addr, to_addr, title, body, 'utf-8', path, name) send_via_gmail(from_addr, to_addr, msg)
It takes one or two minutes to get converted documents in my Kindle. Note that the account must be your registered email address to Amazon; otherwise, the email would be ignored.