2

I've used Calibre to convert Mobi/Epub to PDF. The overall output is good, however, the image is always not in good shape. For example,

How image shows in Kindle reader (Android app)

enter image description here

How image shows after conversion in PDF (w/ Calibre)

enter image description here

How can I keep the image not distorted when converted to PDF?

Thanks!

1

Converting from mobi to pdf is almost always going to be a problem. Both are rendered formats. The problem could come from anywhere. Two ideas:

First, try to obtain an epub copy instead of a mobi file. It's easier to convert from epub to pdf than from mobi to pdf.

Try decompiling the mobi file (go to mobileread to find a tool that can do so).

Possibly there is a setting in Calibre to preserve the image ratios. But really, this is asking calibre to do the impossible.

4
  • 1
    Thanks. Converting from EPUB to PDF give me the same result; and there is no exact settings in Calibre (I only find image cover ratio, but it can't handle images for every other images) – cqcn1991 Oct 30 '19 at 2:16
  • Rename the file extension of the epub file from .epub to .zip and then unzip the files. Find the image file and verify that it's dimensions are the correct ratios. Assuming that is so, then the problem is with the css. (I provide some mobi friendly CSS for images here: imaginaryplanet.net/weblogs/idiotprogrammer/2019/06/… – idiotprogrammer Nov 1 '19 at 12:22
  • Formatting images for Kindle is REALLY HARD! It sounds as though the original mobi used css which was suitable for one display width, but not for another. One of the problems is that mobi css does not support the max-width property.. – idiotprogrammer Nov 1 '19 at 12:24
  • This seems to me to be a common problem - have the same exact problem when converting from EPUB to PDF via Calibre. All images are stretched vertically and their proportion is never maintained, even if two settings are set: 1) PDF Output -> Preserve cover ratio (useless; only intended to keep same aspect ratio in the output file); 2) Page setup -> Output profile: "Tablet" (whose description states "does no resizing of images". Nothing works; images are always resized. – alexlomba87 Jan 4 at 16:32
1

I have achieved a workaround to make images have correct aspect ratio in the output pdf!! I did this on MacOS but I imagine it would work on other platforms. It also leaves extra blank space and sometimes whole blank pages where there used to be a stretched image. Here are my steps. YMMV


I have a file Book.epub I want to convert to pdf using Calibre, but no matter what I try in Calibre settings, some of the images in Book.pdf have super weird aspect ratios.

Note: The random file names are actually named along the lines of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.extension

  1. Duplicate Book.epub to BookCopy.epub
  2. Rename BookCopy.epub to BookCopy.zip
    • (NOT BookCopy.epub.zip)
  3. Unzip BookCopy.zip to folder BookCopy/
    • I use keka as my file archive tool but this probably isn’t important.
    • Folder structure of BookCopy/ follows:
        - BookCopy/
            - OEBPS/
                - Styles/
                    - Style00.css
                    - Style01.css
                    - etc
                - Images/
                    - randomImageName1.png
                    - randomImageName2.jpg
                    - etc
                - content.opf
                - cover.xhtml
                - toc.ncx
                - toc.xhtml
                - randomHtmlName1.xhtml
                - randomHtmlName2.xhtml
                - etc
            - META-INF/
                - container.xml
                - calibre_bookmarks.txt
            - mimetype (no extension)
  1. Figure out what is causing the weird aspect ratios
    1. Find a page in BookCopy.pdf that has an image with an incorrect aspect ratio
    2. Note the header that precedes this image. In my case this matches the title of that section in the PDF outline.
    3. Find the xhtml file within OEBPS/ that matches this section
      • I skimmed through the randomHtmlName.xhtml files in Finder’s Column view and checked the header in the previews as I keyed through the files. These previews do not show the images, only text.
    4. Open this randomHtmlName.xhtml in Chrome
      • For me Chrome would only open these files if I dragged them from Finder into the Chrome window
    5. In Chrome, right click on the image and select Inspect
    6. Play around with the HTML until the image has the correct aspect ratio
      1. For me, this was as simple as editing the style attribute.
        • I changed this from style="width:150.00em;height:98.00em;" to style="width:150.00em;"
        • I did not need to change the separate width and height attributes
        • I’m not sure if this is the same issue that is causing everyone else’s aspect ratio problems
      2. I checked another image and found that the style attribute had different width and height numbers
        • I need a way to find and replace all text that follows the same pattern as style="width:XX.XXem;height:XX.XXem;"
  2. Use a tool such as TextMate to run a regex find and replace across all the randomHtmlName.xhtml files
    1. In the Find window I set the following:
      • Find: (width:\d+\.\d+em;)(height:\d+\.\d+em;)
      • Replace: $1
      • Check the Regular Expression box
      • In Other Folder > Navegate to OEBPS
      • Matching *.xhtml
    2. Click Find All to make sure it finds the patterns
    3. Click Replace All to replace the patterns
  3. Drag a randomHtmlName.xhtml file to Chrome to see if it changed the aspect ratio of images to be correct
    • If you previously had that file open in Chrome you may need to close the tab before reopening the file
  4. Open Keka and set the compression method to Store / No compression.
    • Not sure if this is necessary but it’s what I did
  5. Rename folder BookCopy/ to BookCopyFixed/
  6. Using Keka compress BookCopyFixed/ to BookCopyFixed.zip
  7. Rename BookCopyFixed.zip to BookCopyFixed.epub
  8. Fix metadata
    1. In Calibre, right click on Book and select Open containing folder
    2. In the folder, select cover.jpg and metadata.opf and copy them
    3. Drag BookCopyFixed.epub into Calibre
    4. In Calibre, right click on BookCopyFixed and select Open containing folder
    5. In this new folder, delete metadata.opf and then paste
      • There should now be the correct cover.jpg and metadata.opf in this new folder
    6. In Calibre, right click on Book and select Edit meta > Copy metadata
    7. In Calibre, right click on BookCopyFixed and select Edit meta > Paste metadata
      • The title will now match the original so remember which is the new one!
  9. In Calibre, select the new Book and convert to PDF
  10. Enjoy!

Other notes I learned while working on this that others may find useful:

  • EpubToPdf is an alternative to using Calibre for EPUB > PDF conversion
  • If you get the error [Errno 24] Too many open files when converting you can increase the ulimit to resolve it. Remember to set it back when you’re done!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.