PDF Output with even/odd page margins

Bug #1853155 reported by jimbojw on 2019-11-19
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
calibre
Undecided
Unassigned

Bug Description

I'm using ebook-convert (3.47.1 on Ubuntu) to turn an epub into a pdf intended for print. I would like even numbered pages and odd numbered pages to have different margins so that on left-hand pages, there's a greater right-hand margin, and vice versa. This is to accommodate the spine between printed pages.

I tried using the "@page :left" and "@page :right" CSS pseudo selectors, but theses do not seem to be treated differently from naked "@page". It also appears that "@page" directives are treated differently in the main epub styles and the --extra-css file. The latter appears to stretch the rendered page content after the fact. Even so, I could work around the stretching if the PDFWriter would respect the :left and :right @page pseudo selectors.

How can I specify different margins for even/odd pages in pdf output from epub?

I'm afraid there is no such functionality in the PDF Output plugin,
because this functionality is missing from the library calibre uses
to render HTML to PDF.

 status wontfix

Changed in calibre:
status: New → Won't Fix
jimbojw (jimbojw) wrote :

Thanks for the quick response, Kovid. If I wanted to hack this myself just to try to get a proof of concept, where would I start? Thanks for any pointers!

Kovid Goyal (kovid) wrote :

First of all you would need to update to calibre 4.x, since the PDF
Output plugin is completely re-written for that. Then look in
pdf/html_writer.py

This uses Qt WebEngine to do the actual rendering, and as far as I know
WebEngine simply ignores @page, you can test that by adding it in
resources/pdf-preprint.js which is run just before printToPDF() is
called.

jimbojw (jimbojw) wrote :

Ok, thanks for the tip. I'll dig in there. I upgraded to v4.3.0 this morning. I'm not wedded to the idea of using the @page selectors. It would be enough to flip the left and right margins for even/odd pages.

Also, while we're chatting, would it be better for me to use HTML rather than an epub to create the PDF? I went with epub so that ebook-convert would detect and render the PDF table of contents. Do you think HTML->PDF would be better than ePub->PDF?

jimbojw (jimbojw) wrote :

Update: Yes, there appears to be no way of informing QWebEnginePage that it should apply different margins, and it does not support '@page' directives as far as I can tell.

But while I wasn't able to get ebook-convert to do what I wanted, I was able to use ghostscript (gs) to perform the necessary steps afterwards based on the guidance in this SuperUser post: https://superuser.com/questions/904332/add-gutter-binding-margin-to-existing-pdf-file

Kovid Goyal (kovid) wrote :

HTML to pdf or EPUB to pdf makes no difference as far as calibre is
concerned. You can use the ToC detection conversion options to
auto-generate tocs from html files.

Kovid Goyal (kovid) wrote :

As for post processing the PDF that is not really an ideal solution, as
it means the PDF will be scaled when printed, potentially leading to
quality issues.

jimbojw (jimbojw) wrote :

Ya, that was a concern. To avoid the scaling, I used a ---custom-size with a width reduced by the gutter size. Then the ghostscript call increases the width of all pages and offsets every other page's content using the postscript code described in the previous link. That author's scaling problem stemmed from the fact that the author had no control over the size of the incoming PDF, whereas I do because I'm using ebook-convert to generate it.

Kovid Goyal (kovid) wrote :

Not sure I follow. Doesn't that mean on the on-offset pages there will
be an extra large right margin? Basically what you appear to have done,
if I am understanding you correctly, is make the left margin wider on odd
page nd the right margin wider on even pages.

jimbojw (jimbojw) wrote :

> Basically what you appear to have done, if I am understanding you correctly, is make the left margin wider on odd page nd the right margin wider on even pages.

Correct, that's my intended outcome. For printing, I need an extra large margin on the "inside" of each page to account for the binding. So left-handed pages need an extra gutter margin on the right, and right-handed pages need an extra gutter margin on the left.

I'm currently using lulu xpress to print, and for them, the first interior page is a right-side page. So in my case, odd pages (counting from 1) need extra left margin, even pages need extra right margin. But that's a detail of the printer. I could imagine another printer who treats the first interior page as a left-side page (not sure if any do this, just speculating).

Kovid Goyal (kovid) wrote :

Well if all you want to do is shift the contents of even and odd pages,
this should be perfectly possible with calibre. calibre includes the PDF
library PoDoFo, using that one can simply adjust the cropbox of
the pages. Look in the PoDoFo directory in the calibre source code for
examples of doing various PDF manipulation. The PDF output plugin uses
it for headers/footers, links, font deduplication, etc.

You can see an example of doing this here:
https://stackoverflow.com/questions/7973823/how-do-you-shift-all-pages-of-a-pdf-document-right-by-one-inch

albeit using the command line tools not the library directly.

jimbojw (jimbojw) wrote :

> this should be perfectly possible with calibre ... albeit using the command line tools not the library directly

I'm sorry, I don't think I understand. Are you saying that the command line tools already have this capability? Or that one could implement them in the calibre source code to add a new feature? I admit that I haven't searched the code or docs for the word "cropbox" yet.

Eli Schwartz (eschwartz) wrote :

I think Kovid is suggesting that you could use calibre's podofo bindings rather than gs to do the PDF post-processing.

The logical next step is to try hacking the Calibre source code to expose this as a conversion option rather than a do-it-yourself python script run with calibre-debug ./convert-and-postprocess.py or some such.

Kovid Goyal (kovid) wrote :

Yeah basically use the calibre podofo bindings to adjust the PDF cropbox
in the output PDF file.

Fixed in branch master. The fix will be in the next release. calibre is usually released every alternate Friday.

 status fixreleased

Changed in calibre:
status: Won't Fix → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers