Showing posts with label publishing. Show all posts
Showing posts with label publishing. Show all posts

Thursday, December 5, 2013

Added a Table of Contents feature to XMLtoPDFBook

By Vasudev Ram

XMLtoPDFBook is a publishing tool I created, that allows you to create simple PDF ebooks from text content in XML files.

I had blogged about XMLtoPDFBook earlier, here:

Create PDF books with XMLtoPDFBook

and here:

XMLtoPDFBook now supports chapter numbers and names

Today I added some support for a Table of Contents feature to XMLtoPDFBook. Here is the updated program:
# XMLtoPDFBook2.py

# A program to convert a book in XML text format to a PDF book.
# Uses xtopdf and ReportLab.

# Author: Vasudev Ram - http://www.dancingbison.com
# Version: v0.2

#--------------------------------------------------------------------

# imports

import sys
import os
import string
import time

from PDFWriter import PDFWriter

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

#--------------------------------------------------------------------

# global variables

sysargv = None

#--------------------------------------------------------------------

def debug(message):
    sys.stderr.write(message + "\n")

#--------------------------------------------------------------------

def get_xml_filename(sysargv):
    return sysargv[1]

#--------------------------------------------------------------------

def get_pdf_filename(sysargv):
    return sysargv[2]

#--------------------------------------------------------------------

def XMLtoPDFBook():

    debug("Entered XMLtoPDFBook()")

    global sysargv

    # Get command-line arguments.
    xml_filename = get_xml_filename(sysargv)
    debug("xml_filename: " + xml_filename)
    pdf_filename = get_pdf_filename(sysargv)
    debug("pdf_filename: " + pdf_filename)

    # Parse the XML file.
    try:
        tree = ET.ElementTree(file=xml_filename)
        debug("tree = " + repr(tree))
    except Exception:
        sys.stderr.write("Error: caught exception in ET.ElementTree(file)")
        sys.exit(1)

    # Get the tree root.
    root = tree.getroot()
    debug("root.tag = " + root.tag)
    if root.tag != "book":
        debug("Error: Root tag is not 'book'")
        sys.exit(1)

    # Initialize the table of contents list.
    toc = []
    # Initialize the chapters list.
    chapters = []

    # Traverse the tree, extracting needed data into variables.
    debug("-" * 60)
    for root_child in root:
        if root_child.tag != "chapter":
            debug("Error: root_child tag is not 'chapter'")
            sys.exit(1)
        chapter = root_child
        #debug(chapter.text)
        chapters.append(chapter.text)
        try:
            chapter_name = chapter.attrib['name']
        except KeyError:
            chapter_name = ""
        toc.append(chapter_name)
        debug("-" * 60)

    # Create and set some fields of a PDFWriter.
    pw = PDFWriter(pdf_filename)
    pw.setFont("Courier", 12)
    pw.setFooter("Generated by XMLtoPDFBook. Copyright 2013 Vasudev Ram")

    # Write the TOC.
    pw.setHeader("Table of Contents")
    chapter_num = 0
    debug("Chapter names")
    for chapter_name in toc:
        debug(chapter_name)
        chapter_num += 1
        pw.writeLine(str(chapter_num) + ": " + chapter_name)
    pw.savePage()

    # Write the chapters.
    chapter_num = 0
    for chapter in chapters:
        chapter_num += 1
        pw.setHeader("Chapter " + str(chapter_num) + ": " + toc[chapter_num - 1])
        lines = chapter.split("\n")
        for line in lines:
            pw.writeLine(line)
        pw.savePage()

    pw.close()

    debug("Exiting XMLtoPDFBook()")

def main():

    debug("Entered main()")

    global sysargv
    sysargv = sys.argv

    # Check for right number of arguments.
    if len(sysargv) != 3:
        sys.exit(1)

    XMLtoPDFBook()

    debug("Exiting main()")

#--------------------------------------------------------------------

if __name__ == "__main__":
    try:
        main()
    except Exception, e:
        sys.stderr.write("Error: caught Exception" + str(e))
        sys.exit(1)

#--------------------------------------------------------------------

You can run it as follows:
python XMLtoPDFBook2.py vi_quickstart2.xml vi_quickstart2.pdf 
where I've used my vi quickstart tutorial, first written for Linux For You magazine, as the input XML file.

Here is a screenshot of the first page of the resulting PDF ebook - the Table of Contents:


And here is a screenshot Chapter 3 of the book:


I've pushed the code (as file XMLtoPDFBook2.py) to my xtopdf project on Bitbucket.

Enjoy.

- Vasudev Ram - Dancing Bison Enterprises

Contact Page




Saturday, June 15, 2013

Create PDF books with XMLtoPDFBook

By Vasudev Ram


XMLtoPDFBook is a program that lets you create simple PDF books from XML text content. It requires Python, ReportLab and my xtopdf toolkit for PDF creation.

(Use ReportLab v1.21, not the 2.x series; though 2.x has more features, xtopdf has not been tested with it; also, those additional features are not required for xtopdf.)

XMLtoPDFBook.py is released as open source software under the BSD license, and I'll be adding it to the tools in my xtopdf toolkit.

Here's how to use XMLtoPDFBook:

In a text editor, create a simple XML template for the book, like this:
<?xml version="1.0"?>
<book>
        <chapter>
        Chapter 1 content here.
        </chapter>

        <chapter>
        Chapter 2 content here.
        </chapter>
</book>
Add as many chapter elements as you need.

Then write or paste the text of one chapter inside each chapter element, in sequence.

Now you can convert the book content to PDF using this program, XMLtoPDFBook:
#--------------------------------------------------
# XMLtoPDFBook.py

# A program to convert a book in XML text format to a PDF book.
# Uses xtopdf and ReportLab.

# Author: Vasudev Ram - http://www.dancingbison.com
# Version: v0.1

#--------------------------------------------------

# imports

import sys
import os
import string
import time

from PDFWriter import PDFWriter

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET

#--------------------------------------------------

# global variables

sysargv = None

#--------------------------------------------------

def debug(message):
    sys.stderr.write(message + "\n")

#--------------------------------------------------

def get_xml_filename(sysargv):
    return sysargv[1]

#--------------------------------------------------

def get_pdf_filename(sysargv):
    return sysargv[2]

#--------------------------------------------------

def XMLtoPDFBook():

    debug("Entered XMLtoPDFBook()")

    global sysargv

    xml_filename = get_xml_filename(sysargv)
    debug("xml_filename: " + xml_filename)
    pdf_filename = get_pdf_filename(sysargv)
    debug("pdf_filename: " + pdf_filename)

    pw = PDFWriter(pdf_filename)
    pw.setFont("Courier", 12)
    pw.setHeader(xml_filename + " to " + pdf_filename)
    pw.setFooter("Generated by ElementTree and xtopdf")

    tree = ET.ElementTree(file=xml_filename)
    debug("tree = " + repr(tree))

    root = tree.getroot()
    debug("root.tag = " + root.tag)
    if root.tag != "book":
        debug("Error: Root tag is not 'book'")
        sys.exit(2)

    debug("=" * 60)
    for root_child in root:
        if root_child.tag != "chapter":
            debug("Error: root_child tag is not 'chapter'")
            sys.exit(3)
        debug(root_child.text)
        lines = root_child.text.split("\n")
        for line in lines:
            pw.writeLine(line)
        pw.savePage()
        debug("-" * 60)
    debug("=" * 60)
    pw.close()

    debug("Exiting XMLtoPDFBook()")

#--------------------------------------------------

def main():

    debug("Entered main()")

    global sysargv
    sysargv = sys.argv

    # Check for right number of arguments.
    if len(sysargv) != 3:
        sys.exit(1)

    XMLtoPDFBook()

    debug("Exiting main()")

#--------------------------------------------------

if __name__ == "__main__":
    main()

#--------------------------------------------------

Here is an example run of XMLtoPDFBook, using my vi quickstart article earlier published in Linux For You magazine:

python XMLtoPDFBook.py vi_quickstart.xml vi_quickstart.pdf

This results in the contents of the article being published to PDF in the file vi_quickstart.pdf.

- Vasudev Ram - Dancing Bison Enterprises

Contact me

Wednesday, November 28, 2012

PyBooklet, to create PDFs with 2 pages per sheet for printing

yedderson/PyBooklet · GitHub

Interesting idea.

PyBooklet may be a useful complement to my PDFBook utility (a part of my xtopdf toolkit), which can create PDF books from a set of text file chapters, and other such tools, to create printed books from PDF files, with less use of paper, which is better for the environment.

- Vasudev Ram
www.dancingbison.com

Thursday, November 22, 2012

The Readium project: open source EPUB 3 reference implementation

Readium | Digital Publishing meets Open Web

One of their goals is to provide an open source reference implementation of an EPUB3 reader in the browser. They have a Chrome extension which does that.

Excerpt from the site:


Readium Open Source Initiative Launched to Accelerate
Adoption of EPUB 3: ACCESS, Adobe, Barnes & Noble, Copia,
Google, Kobo/Rakuten, O’Reilly, Samsung, Sony, others
support project to advance universal digital publishing
format
New York, NY, February 13, 2012 -The International Digital
Publishing Forum (IDPF) today announced the Readium Project,
a new open source initiative to develop a comprehensive
reference implementation of the IDPF EPUB® 3 standard. This
vision will be achieved by building on WebKit, the widely
adopted open source HTML5 rendering engine. A quote sheet
with quotes from the following is available at http://idpf.org/
readium-support : ACCESS, Adobe, Anobii, Apex CoVantage,
Assoc. American Publishers (AAP), Barnes & Noble, Bluefire
Productions, BISG, Copia, DAISY, EAST, EDItEUR, Evident Point,
Google, Incube Tech, Kobo/Rakuten, Monotype, O’Reilly,
Rakuten, Safari Books Online, Samsung, Sony, VitalSource,
Voyager Japan. For more information about the project,
including how to participate and links to downloads and source
code, visit http://readium.org .
)

Saturday, September 22, 2012

How to create an ebook with Pandoc, the swiss-army-knife conversion tool

By Vasudev Ram


Pandoc is a tool that lets you convert many document formats to many other document formats.

I had blogged or tweeted about Pandoc some time ago, but saw this feature only today:

Pandoc can be used to easily create EPUB format ebooks.

I was interested to see that the process of creating an EPUB ebook using Pandoc is similar (in one respect only, that is one book chapter per file or directory *) to the process of creating PDF ebooks with xtopdf, my PDF creation toolkit.

Of course, the Pandoc method supports many more features (including markup, metadata, etc.) than xtopdf does. But xtopdf is very easy if you just want a simple set of text chapters converted to a single PDF ebook.

* One chapter per file for xtopdf, one chapter per directory for Pandoc.

- Vasudev Ram - Dancing Bison Enterprises



Sunday, September 9, 2012

Leanpub, publish ebooks early/often, higher royalties

By Vasudev Ram


LeanPub.com lets you publish ebooks early and often (i.e. versions of an evolving book) and earn higher royalties.

Their tagline:

"Publish Early, Publish Often

Self-publish your book as you write it, and earn great royalties."

Interesting idea.

They give 90% royalties. They take a 10% cut plus 50 cents.

- Vasudev Ram - Dancing Bison Enterprises

Monday, March 26, 2012

Rich-layout ebooks with EPUB3, HTML5 and CSS3


http://www.ibm.com/developerworks/library/x-richlayoutepub/index.html

The article is by Liza Daly, VP Engineering, Safari Books Online. It also links to an earlier EPUB tutorial by Liza, which uses Java and Python.

- Vasudev Ram
www.dancingbison.com
twitter.com/vasudevram

Thursday, February 16, 2012

txt2epub - create an epub from text files


As the title says:

https://github.com/mfrasca/txt2epub

Came across this recently. It reminded me of the PDFBook.py program in my xtopdf toolkit, which does roughly the same thing, but with PDF as the target:

http://www.dancingbison.com/products.html#xtopdf

Wednesday, August 24, 2011

Google eBooks and eBookStore

By Vasudev Ram

Google had launched an eBookStore which seems roughly similar to the Amazon ebooks service - it both allows you to buy ebooks and download free ebooks, and also syncs your reading across devices and computers. It seems to have launched sometime last year. As of now it is only for the US, but they have plans to extend it to other countries. Many links about it below.

Seen via this tweet from @markupforum:

First Google eBooks Device To Go on Sale at Target This Week

Google Launches Long-Awaited E-Bookstore

Google ebookstore

Google Books Help

Google eBooks: Overview

Google Books: Information for Publishers and Authors

Google Books Tour - Promote Your Books On Google - for free

- Vasudev Ram @ Dancing Bison

Share |

Wednesday, August 10, 2011

Amazon announces HTML5-based Kindle Cloud Reader

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

Amazon has released a Kindle Cloud Reader "written from the ground up in HTML5", according to PCMag.com. It can automatically sync with other Kindle apps, allowing you to start reading on the Web and continue on other devices. You can also read offline.

See here:

http://goo.gl/HP47w

or here:

http://www.pcmag.com/article2/0,2817,2390784,00.asp

(Both above links go to the same page at PCMag.com)

You can access Amazon Cloud Reader here: https://read.amazon.com/about

Posted via email
- Vasudev Ram


Saturday, August 6, 2011

Read Kindle ebooks without a Kindle

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

Yes, you read that right - you don't need a Kindle device to read Kindle ebooks. You can read them on your PC (Windows), Mac, iPhone, iPad, BlackBerry, or Android Phone. Amazon has free Kindle reader software for all those computer and mobile platforms.

Get it from here (Amazon site): http://goo.gl/i7hO5

UPDATE: I downloaded and tried out the free Kindle reader app for Windows. It seems to work well (on a brief test, of course.). I like the default font and the uncluttered appearance of the screen, and the similarity to a physical book - clear black text on a white background, with enough spacing between words, and on the sides of the text.

Here is a screenshot of the Kindle Reader for Windows, showing the book "Treasure Island":



(The book looks better in the Kindle reader than in the image above.)

Excerpts from the site:
[

- You can read thousands of free Kindle ebooks.

- Get free book samples - read the first chapter for free before you decide to buy.
]

Posted via email
- Vasudev Ram @ Dancing Bison

Thursday, July 7, 2011

The Startup Magazine, Magcloud-powered, launching August 2011

By Vasudev Ram - dancingbison.com | @vasudevram | jugad2.blogspot.com

Saw this on Hacker News (http://news.ycombinator.com)

The Startup Magazine - http://thestartupmagazine.com/ - is to launch in early August 2011. As the name says, it is a magazine about startups; it will consist of interesting startup stories. It will be on sale in both digital and print format. The print format will be created using the Magcloud ( http://www.magcloud.com/ ) magazine print-on-demand service by Hewlett-Packard that I had blogged about earlier, here:

Start your own magazine with MagCloud, a cool new Hewlett-Packard startup:
http://jugad2.blogspot.com/2008/08/start-your-own-magazine-with-magcloud.html

Excerpt from the Startup Magazine front page:

[ The special issue of Hacker Monthly, "Startup Stories," was a smash hit. You, our readers, loved it and now you want more.

So we figured, why not make a new magazine out of it? Think of it as a monthly dose of "Founders At Work" or "Do More Faster." Wouldn't that be something?

We are hard at work making this happen, with a set deadline of August 8th, 2011. If you run a startup and have a story to tell, drop us a line. Otherwise, enter your email below so we can tell you when it has launched. ]

Hacker News thread about The Startup Magazine:
http://news.ycombinator.com/item?id=2733797

Posted via email
- Vasudev Ram - dancingbison.com

Saturday, August 2, 2008

Start your own magazine with MagCloud, a cool new Hewlett-Packard startup

Did you ever wish you could start a magazine of your own? on any topic - in some area where you have skills or knowledge, on a personal hobby area? Or on any topic at all?

Now you can - all you need to be able to do is to create a PDF of the content.

MagCloud.com is a cool new startup by Hewlett-Packard that enables you to publish your own magazine.

Anyone can create a MagCloud reader/subscriber account. This type of account only allows you to view previews of magazines on the site, and to subscribe to magazines (which are delivered to you by post).

You need a MagCloud publisher account to create magazines. Publisher accounts are by invitation as of now, but anyone can apply. All you have to do is upload a PDF of your magazine to their site. The rest - printing, mailing, subscription management - is taken care of by them. (At present they only deliver to U.S. locations, but are planning to ship to other areas too.)

Magazines are printed and posted to you by MagCloud. They use their own HP  Indigo printers to Print On Demand (POD).

They have some stipulations about the way your PDF should be created:

- One is using high resolution images, of 300 dpi (dots per inch).

- Another is that you have to use a specific size of "bleed" - a printing technical term - its something like margins around the edges of the content - in your PDF.

At the least, Adobe InDesign and Scribus (a free and open source desktop publishing software that can output to PDF) are supported as software to create PDFs. Other software may work too.

And, if your magazine is text-only, you may find my xtopdf toolkit for PDF creation, which is free software, to be one of the quickest and easiest ways of creating a PDF for your magazine (at least if you are a little computer-savvy and can use a command-line - you don't have to be a programmer).

All you need in order to use xtopdf is Python and the open source version of the ReportLab toolkit (version 1.17 or higher, but use the 1.x series, as xtopdf is not yet tested with the 2.x series of ReportLab).

Download Python from here (version 2.2 or higher).

Here is a guide to installing and using xtopdf on Windows. xtopdf works well on Linux too (in fact it was developed on Linux, and only later tested on Windows). Basically, all you have to do, on either Windows or Linux, is these steps (in brief):

- Install Python if not installed already
- Install the ReportLab toolkit; follow the instructions given in its README or INSTALL or similarly-named file to see how to install it, and to make it visible to Python.
- Install xtopdf (see its README file for how).

Then you can try out the individual programs that come with xtopdf (such as PDFBook.py mentioned below).

xtopdf even comes with a program (written using its own API), to create simple PDF books - look for the program called PDFBook.py in the xtopdf download .zip or .tar.gz file. PDFBook.py lets you create a PDF book from a set of text files, each of which could represent one chapter of the book (though it is not a restriction that each separate file should represent one chapter). So you could use it to create a PDF for your magazine for MagCloud, that consists of a set of text files, where each file contains an article created by one contributor. As long as you can ensure that all their articles follow the same layout decided by you, the end result will look consistent.

Based on my initial interaction with MagCloud, they seem to be quite enthusiastic and responsive to potential issues or questions of users. For example, some initial questions that I had were answered quite quickly.

Vasudev Ram - Dancing Bison Enterprises