02-02-2023, 04:11 AM | #1 |
Member
Posts: 15
Karma: 10
Join Date: Jan 2023
Device: Kindle Paperwhite (10th Generation)
|
Plugin that edits a PDF in the library
Hi,
I have an idea for a plugin but I'm not sure I know which type of plugin this should be. The problem: I have many pdf books which have no cover. I would like to add a cover image to the pdf file itself. The cover could be taken from Calibre's metadata for that book. Would it be possible to write some plugin that would do all this? I was not able to find a way for a plugin to alter the book file itself. |
02-02-2023, 04:34 AM | #2 |
creator of calibre
Posts: 43,866
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Sure you make an interface plugin. Then when you click it the plugin can iterate over all the selected books, find the PDF files and do whatever it likes with them.
|
02-02-2023, 11:24 AM | #3 |
Member
Posts: 15
Karma: 10
Join Date: Jan 2023
Device: Kindle Paperwhite (10th Generation)
|
Great, thanks.
|
02-04-2023, 03:04 PM | #4 |
Member
Posts: 15
Karma: 10
Join Date: Jan 2023
Device: Kindle Paperwhite (10th Generation)
|
Another question:
I have working python code for inserting a cover page from an image to a pdf. However it uses 3rd-party libraries, namely pypdf and reportlab. I know I can add them to my plugin, but if Calibre's existing code could handle this already, I'd rather make my plugin leaner and use it instead. So can someone here guide me on if and how to use Calibre's pdf modules? Here's my existing code for reference: Code:
import pypdf from reportlab.pdfgen import canvas from reportlab.lib.utils import ImageReader def generate_cover_pdf_stream( cover_url: str, main_pdf_reader: pypdf.PdfReader) -> io.BytesIO: cover_pdf_stream = io.BytesIO() sample_page = main_pdf_reader.pages[0] cover_image = ImageReader(cover_url) image_canvas = canvas.Canvas(cover_pdf_stream, pagesize=(sample_page.mediabox.width, sample_page.mediabox.height), cropBox=sample_page.cropbox, artBox=sample_page.artbox, trimBox=sample_page.trimbox) image_canvas.drawImage(cover_image, x=float(sample_page.trimbox.left), y=float(sample_page.trimbox.bottom), width=float(sample_page.trimbox.width), height=float(sample_page.trimbox.height)) image_canvas.showPage() image_canvas.save() return cover_pdf_stream def add_cover(filename_or_stream: str | IO | Path, cover_url: str, out_filename_or_stream: Path | str | IO): reader = pypdf.PdfReader(filename_or_stream) merger = pypdf.PdfMerger() cover_pdf_stream = generate_cover_pdf_stream(cover_url, reader) merger.append(cover_pdf_stream) merger.append(reader.stream) merger.write(out_filename_or_stream) |
02-04-2023, 10:46 PM | #5 |
creator of calibre
Posts: 43,866
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
The podofo module in calibre has the ability to insert a page from one PDF file into another. So you create your image page using the functions in pdf/image_writer.py and then do the insert by creating two podofo PDFDoc objects and using the insert_existing_page method. See the add_cover function in pdf/html_writer.py for an example.
|
06-17-2023, 06:33 PM | #6 |
Junior Member
Posts: 7
Karma: 64
Join Date: Apr 2014
Location: Homer, Illinois, US
Device: Kindle Paperwhite 2
|
I've got a shell script that I use in Linux to add the cover image as a page to the front of a PDF. Page numbering is a secondary issue that I've run into, but I'm learning more about PDF structure as I dive into it. I'll have to take a look at the podofo options to see what might work best.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How do I use Library Closed plugin? | tekwrite | Plugins | 1 | 08-11-2020 10:29 AM |
Looking for PDF plugin that removes PDF/A restrictrictions | lance777 | Plugins | 0 | 06-14-2018 03:07 AM |
Library modification plugin | anthony.burton4 | Plugins | 1 | 10-09-2012 10:58 PM |
New Plugin Type Idea: Library Plugin | cgranade | Plugins | 3 | 09-15-2010 12:11 PM |
Request Library - Plugin Architecture | admford | enTourage Archive | 0 | 04-28-2010 01:29 PM |