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
- Duplicate Book.epub to BookCopy.epub
- Rename BookCopy.epub to BookCopy.zip
- 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)
- Figure out what is causing the weird aspect ratios
- Find a page in BookCopy.pdf that has an image with an incorrect aspect ratio
- Note the header that precedes this image. In my case this matches the title of that section in the PDF outline.
- 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.
- Open this randomHtmlName.xhtml in Chrome
- For me Chrome would only open these files if I dragged them from Finder into the Chrome window
- In Chrome, right click on the image and select Inspect
- Play around with the HTML until the image has the correct aspect ratio
- 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
- 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;"
- Use a tool such as TextMate to run a regex find and replace across all the randomHtmlName.xhtml files
- 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
- Click Find All to make sure it finds the patterns
- Click Replace All to replace the patterns
- 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
- Open Keka and set the compression method to Store / No compression.
- Not sure if this is necessary but it’s what I did
- Rename folder BookCopy/ to BookCopyFixed/
- Using Keka compress BookCopyFixed/ to BookCopyFixed.zip
- Rename BookCopyFixed.zip to BookCopyFixed.epub
- Fix metadata
- In Calibre, right click on Book and select Open containing folder
- In the folder, select cover.jpg and metadata.opf and copy them
- Drag BookCopyFixed.epub into Calibre
- In Calibre, right click on BookCopyFixed and select Open containing folder
- 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
- In Calibre, right click on Book and select Edit meta > Copy metadata
- 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!
- In Calibre, select the new Book and convert to PDF
- 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!