Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 12-22-2013, 06:48 AM   #1
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Converting to PDF with Prince

I am considering porting my Prince conversion utility to a calibre plugin. My problem is I'm very much a python (and calibre) newbie, so this will not be a trivial work for me.

I've had a look at the plugin documentation, but there are some details about what I would need that are not available. For example, how can I obtain a list of the filenames in a book and in the spine? And is there some precooked solution to have the files in a named temporary location so they can be fed to Prince?

My original code is actually very simple: just uncompress an epub, add some custom CSS files and issue a command for Prince containing all filenames from the spine.

Another question is whether this should be a "general" plugin or an "output" plugin, given that it is using an external non-free tool and it would replace the already existing PDF conversion.
Jellby is offline   Reply With Quote
Old 12-22-2013, 06:55 AM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,344
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Is there something that prince does that calibre's own conversion to pdf does not do?
kovidgoyal is offline   Reply With Quote
Advert
Old 12-22-2013, 07:38 AM   #3
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Quote:
Originally Posted by kovidgoyal View Post
Is there something that prince does that calibre's own conversion to pdf does not do?
I haven't tested calibre's conversion much, but Prince is supposed to have better typesetting than normal HTML renderers (in particular better paragraph-breaking algorithm). It also supports a number of CSS3 and custom CSS properties allowing some interesting things like real floating images (no white space when an image is moved to the next page), real footnotes (although most of the styling is missing), special page styles (headers/footers) for the first page of a chapter, etc.

But probably the main reason for my tool is that it allows to use specific CSS stylesheets only for the PDF conversion. One additional stylesheet may contain user preferences, like page size, fonts (already available in the calibre configuration), but also margins, justification, hyphenation limits, etc. (that's a suggestion for calibre). Another stylesheet may be included in the book itself (in a <meta> tag), and it's intended to have some some styles to work specifically with Prince, using the above features for example.

I've just compared both PDF conversions, and it looks like the kerning from calibre is not that good either. I've attached the results, you can compare yourself. There are also differences in the title page picture (a problem of default resolution probably), in the TOC (Prince added the page numbers to the HTML TOC, I guess calibre can only do that if it generates the TOC itself), in the PDF bookmarks (calibre has only one level), in the <hr> (compare page 41)... The comparison is not 100% fair, because this book is already tweaked for Prince (with the special style I mentioned above).
Attached Files
File Type: pdf prince.pdf (782.5 KB, 314 views)
File Type: pdf calibre.pdf (1.01 MB, 311 views)
File Type: epub A se tordre.epub (392.9 KB, 274 views)
Jellby is offline   Reply With Quote
Old 12-22-2013, 08:41 AM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,344
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
The kerning is indeed off in the calibre pdf, I suspect that has to do with the font used, Qt is rather finicky about what fonts it renders well. calibre is perfectly capable of generating multi-level tocs, you just need to set the appropriate conversion options. You can also control margins, justification and set a special stylesheet. And calibre uses the Qt WebKit rendering engine, which means it's support for CSS is likely to be pretty good

Anyway, I was just curious. Regarding your original questions, if you make it a standalone plugin you will need to do more work --- get the file from the database, explode the epub yourself and so on. SO make it a conversionplugin.

The conversion plugin is called witht eh book already exploded into its individual files, in an "oeb" object. Just have a look at one of the simpler conversion output plugins in calibre to see how to use the oeb object. For example, the rtf or txt output plugins. Or the htmlz output plugin. And feel free to ask if you need clarification.
kovidgoyal is offline   Reply With Quote
Old 12-22-2013, 09:39 AM   #5
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Quote:
Originally Posted by kovidgoyal View Post
Just have a look at one of the simpler conversion output plugins in calibre to see how to use the oeb object. For example, the rtf or txt output plugins. Or the htmlz output plugin.
Where can I find the source for those plugins?

By running a dummy plugin with debugging, I see this in the terminal:

Code:
Trimming unused files from manifest...
Is it possible to avoid that? I'd have to use one of these files for the conversion...
Jellby is offline   Reply With Quote
Advert
Old 12-22-2013, 11:32 AM   #6
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,344
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Looking in ebooks/conversion/plugins. I dont see how you could avoid the trimming of unused files. If you want to pass in some special css to prince, you can make an option in the output plugin and pass in the css that way, in any case the conversion pipeline will remove any css that is not used by some html.
kovidgoyal is offline   Reply With Quote
Old 12-22-2013, 12:15 PM   #7
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Quote:
Originally Posted by kovidgoyal View Post
Looking in ebooks/conversion/plugins.
I was just going to write that I found them

Quote:
I dont see how you could avoid the trimming of unused files. If you want to pass in some special css to prince, you can make an option in the output plugin and pass in the css that way, in any case the conversion pipeline will remove any css that is not used by some html.
The problem here is that this would be a different (optional) CSS file per book. I include such a file with my ePubs. I could maybe use @media rules, but still, this is a stylesheet that's only intended to be used with Prince...

I guess I'll have to go the long way, explode the file and parse the opf.
Jellby is offline   Reply With Quote
Old 12-22-2013, 12:53 PM   #8
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
I tried this:

Code:
tdir = PersistentTemporaryDirectory('_PrincePDF')
exploder = get_tools(format)[0]
opf = exploder(book, tdir)
#os.chdir(os.path.dirname(opf))
oeb = OEBBook(default_log, HTMLPreProcessor, CSSPreProcessor)
OEBReader()(oeb, opf)
(where "format" and "book" are arguments passed to the function".) The exploding seems to go fine, if I print the "tdir" value I can see that the directory is created and all the ePub files are there. I have tried with and without the chdir, but I always get:

Code:
Failed to parse content in Chapitre-36.xhtml
Failed to parse content in Chapitre-14.xhtml
Failed to parse content in Chapitre-21.xhtml
[...]
Referenced file u'Chapitre-24.xhtml' not in manifest
Referenced file u'Chapitre-12.xhtml' not in manifest
Referenced file u'Chapitre-10.xhtml' not in manifest
[...]
OEBError: Spine is empty
What am I doing wrong so far?

EDIT: Solved, I had to use preprocessor instances:

Code:
html_preprocessor = HTMLPreProcessor()
css_preprocessor = CSSPreProcessor()
oeb = OEBBook(default_log, html_preprocessor, css_preprocessor)
Now the unused files are not removed from the manifest, but the metadata does not correspond to the OPF (illustrator or translator do not appear, neither do <meta> tags). In my tool I was using "import xml.dom.minidom", is that safe to use within a calibre plugin? Is there any alternative?

Last edited by Jellby; 12-22-2013 at 03:25 PM.
Jellby is offline   Reply With Quote
Old 12-22-2013, 10:00 PM   #9
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,344
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
If you wan to parse the OPF use lxml. You can use xml.dom.minidom, but lxml is better. calibre will only read metadata it supports itself when parsing the opf.
kovidgoyal is offline   Reply With Quote
Old 12-23-2013, 05:38 AM   #10
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Thank you. First version is working, now I have to add all the options, config and GUI.
Jellby is offline   Reply With Quote
Old 12-23-2013, 02:50 PM   #11
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
OK, here is a more or less working version (no help message yet, and default demo icon).

Some issues I have:

- The "View log" does not look disabled, although I use setEnabled(False). It's not clickable by default, but at least in my computer it's not greyed-out or anything.

- I don't know what to do with the print statements. I'd like them to be off by default, and only appear with some minimal debug level. Better yet, I'd like to have some status indication... can I use some calibre function for that or should I have it inside the plugin?

- In the configuration dialog, is there any way to have the "Restore defaults" button together with OK and Cancel?

- The output PDF is still created in a temp dir, I'd like to add an option to choose whether it should be added to the library or saved to disk elsewhere. Can I use any calibre functions for that? In one case, I'd add a file to the existing book record, and I guess calibre would take care of everything. In the other case I'd probably like to use the "save to disk" template at least.
Attached Files
File Type: zip prince_pdf.zip (8.1 KB, 231 views)

Last edited by Jellby; 12-24-2013 at 03:35 AM.
Jellby is offline   Reply With Quote
Old 12-23-2013, 05:44 PM   #12
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,344
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
from calibre.constants import DEBUG
if DEBUG:
print (whatever)

then prints will only happen when running calibre in debug mode.

See the documentation for QDialogButtonBox, you can add as many types of buttons as you like.


db = self.gui.current_db
db.new_api.add_format(book_id, 'pdf', path)

If you want to use save to disk templates, use the get_components() function from library/save_to_disk.py
kovidgoyal is offline   Reply With Quote
Old 12-24-2013, 03:51 AM   #13
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Quote:
Originally Posted by kovidgoyal View Post
from calibre.constants import DEBUG
if DEBUG:
print (whatever)
Great! Is there something that will work with 'calibre -v' too? What about some GUI feedback?

Quote:
See the documentation for QDialogButtonBox, you can add as many types of buttons as you like.
But I didn't define the QDialogButtonBox for the config dialog. As far as I can see that is defined in calibre/customize/__init__.py:

Code:
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
Can I access from my plugin the QDialogButtonBox that is defined in the standard do_user_config procedure?

Last edited by Jellby; 12-24-2013 at 04:03 AM.
Jellby is offline   Reply With Quote
Old 12-24-2013, 04:20 AM   #14
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,344
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
There is no calibre -v, it's calibre-debug -g

If you want to implement anything beyond the most basic customization you have to implement your own config widget, http://manual.calibre-ebook.com/crea...of-your-plugin
kovidgoyal is offline   Reply With Quote
Old 12-24-2013, 05:02 AM   #15
Jellby
frumious Bandersnatch
Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.Jellby ought to be getting tired of karma fortunes by now.
 
Jellby's Avatar
 
Posts: 7,548
Karma: 19500001
Join Date: Jan 2008
Location: Spaniard in Sweden
Device: Cybook Orizon, Kobo Aura
Quote:
Originally Posted by kovidgoyal View Post
There is no calibre -v, it's calibre-debug -g[/url]
I mean 'calibre --verbose', as explained in http://manual.calibre-ebook.com/cli/calibre.html

Quote:
If you want to implement anything beyond the most basic customization you have to implement your own config widget, http://manual.calibre-ebook.com/crea...of-your-plugin
That's what I did, but the config dialog is built by the do_user_config procedure. It puts together my widget and some QDialogButtonBox of its own. I can do anything with my widget, but I don't know how to add buttons to the box that I did not create. I'm guessing the answer is that I can't do that anyway (and it's not a big deal).
Jellby is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script for converting ePUB to PDF using Prince Jellby ePub 44 03-03-2015 03:21 PM
HRH Prince ????? Prince of Cambridge wodin Lounge 8 07-26-2013 04:30 PM
Converting PDF dan1chris2 Sony Reader 1 12-08-2010 05:44 AM
Harry Potter and the Half-Blood Prince - the Prince? doctorow Reading Recommendations 11 05-12-2007 01:53 PM
Using prince to convert html to pdf? Antartica iRex 1 12-20-2006 08:49 AM


All times are GMT -4. The time now is 02:51 AM.


MobileRead.com is a privately owned, operated and funded community.