View Single Post
Old 04-21-2023, 10:52 PM   #8
Geremia
Addict
Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!Geremia rocks like Gibraltar!
 
Posts: 258
Karma: 100000
Join Date: Oct 2012
Device: Calibre
ChatGPT answer to kovidgoyal's instructions:
Quote:
To create an input plugin for Calibre that calls the built-in PDF input plugin under the hood, you can follow these steps:
  1. Create a new folder in your Calibre plugins directory (usually located in the Calibre configuration folder) and name it something like “mypdfplugin”.
  2. Inside the “mypdfplugin” folder, create a new file named “__init__.py” (note the double underscores before and after “init”). This file will contain the code for your plugin.
  3. In the "__init__.py” file, you can import the built-in PDF input plugin like this:
    Code:
    from calibre_plugins.pdf_input import PDFInput
  4. You can then define a new class that inherits from the PDFInput class and overrides any methods that you need to modify. For example, if you want to change the way the plugin handles the “description” metadata field, you could define a new “get_metadata” method like this:
    Code:
    class MyPDFInput(PDFInput):
        def get_metadata(self, stream, filename=None, title=None, author=None, description=None):
            # Call the original get_metadata method from the PDFInput class
            metadata = super().get_metadata(stream, filename, title, author, description)
            
            # Modify the description field
            metadata['description'] = "My custom description"
            
            # Return the modified metadata
            return metadata
  5. Finally, you can register your plugin by creating a new file named “plugin.py” (again, in the “mypdfplugin” folder) and adding the following code:
    Code:
    from calibre.customize import FileTypePlugin
    
    class MyPDFPlugin(FileTypePlugin):
        name = 'My PDF Plugin'
        description = 'My custom PDF plugin'
        file_types = set(['pdf'])
        can_split = False
        can_convert = False
        input_plugin = 'mypdfplugin.MyPDFInput'
    
    # Register the plugin
    FileTypePlugin.register_plugin('pdf', MyPDFPlugin)
    This code defines a new FileTypePlugin that registers your PDF input plugin under the name “My PDF Plugin”. The “file_types” set specifies which file types your plugin can handle (in this case, just PDF files), and the “input_plugin” parameter specifies the name of your custom PDFInput subclass.
And that’s it! After restarting Calibre, your plugin should be available to use for any PDF files you add to your library.

Last edited by Geremia; 04-21-2023 at 11:41 PM.
Geremia is offline   Reply With Quote