Try to display PDF File with KivyMD and Python

Viewed 657

I'm trying to build a quite simple app Python/KivyMD to get some documents to be organised. Is there any solution to display PDF documents ? I am looking in many places but at this stage, didn't find anything convincing... Does any one got an idea (certainly better than mine?) Many thanks

1 Answers

Yes there are a few, but most of the times the real answer is not in the same place but among different places, I will give you some details about how you can be able to do that: jbsidis

  1. Add Pyjnius to your spec requierements, this helps to access java classes:

requirements = kivy==2.0.0,kivymd==0.104.1,python3,pyjnius,plyer,requests,urllib3,chardet,idna,pip,Image,PIL

  1. Download the .jar plugin from https://pdfbox.apache.org/download.html this will give you easy access to functions that will help you to:

Extract Text Extract Unicode text from PDF files. Split & Merge Split a single PDF into many files or merge multiple PDF files. Fill Forms Extract data from PDF forms or fill a PDF form. Preflight Validate PDF files against the PDF/A-1b standard. Print Print a PDF file using the standard Java printing API. Save as Image Save PDFs as image files, such as PNG or JPEG. Create PDFs Create a PDF from scratch, with embedded fonts and images. Signing Digitally sign PDF files.

  1. You must add that plugin into your app package.

  2. If everything is setup correctly the easiest part comes now (instead of the Apache plugin you can also use native classes from the NDK of the device you will be compiling the app for: android, ios, etc)

  3. Study the API from the plugins, if you will use the Apache plugin, read: https://pdfbox.apache.org/docs/2.0.13/javadocs/

  4. Program the Graphical User Interface using KivyMD and Python3, also use pyjnius: https://pyjnius.readthedocs.io/en/stable/

  5. My recomendation for organizing PDFs is to use a GridLayout, like having pictures but putting MDCards plus text labels:


from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout

Window.size = (330, 500)


kv = '''
#:import MDTextField kivymd.uix.textfield.MDTextField
<MyTile@SmartTileWithLabel>
    size_hint_y: None
    height: "240dp"

<S>:
    MDTextFieldRound:
        pos_hint: {"center_x": .5, "center_y": .95}
        normal_color : [1,1,1,.1]
        color_active : [1,1,1,1]
        size_hint: .8, .07
        hint_text : 'Search a product...'
        icon_left : 'magnify'

Screen:
    FloatLayout:
        BoxLayout:
            id: m5
            pos_hint: {"center_x": .5, "center_y": .371} #this will change if you change this Window.size = (330, 500)
            orientation: "vertical"

            ScrollView:
                GridLayout:
                    
                    cols: 2
                    adaptive_height: True
                    padding: dp(4), dp(4)
                    spacing: dp(4)
                    MDCard:
                        size: "150dp","200dp"
                        MDLabel:
                            text: "    PDF Book"
                        MDIconButton:
                            icon: "file"
                    MDCard:
                        size: "150dp","200dp"
                        MDLabel:
                            text: "    PDF Book"
                        MDIconButton:
                            icon: "file"
                    MDCard:
                        size: "150dp","200dp"
                        MDLabel:
                            text: "    PDF Book"
                        MDIconButton:
                            icon: "file"
                    MDCard:
                        size: "150dp","200dp"
                        MDLabel:
                            text: "    PDF Book"
                        MDIconButton:
                            icon: "file"
                    MDCard:
                        size: "150dp","200dp"
                        MDLabel:
                            text: ""
                        MDIconButton:
                            icon: "file"

##                    MyTile:
##                        source: "Photos/pro.jpg"
##                        text: "[size=26]jbsidis[/size]\\n[size=14]cat-1.jpg[/size]"
##
##                    MyTile:
##                        source: "Photos/pro.jpg"
##                        text: "[size=26]jbsidis[/size]\\n[size=14]cat-2.jpg[/size]"
##                        tile_text_color: app.theme_cls.accent_color
##
##                    MyTile:
##                        source: "Photos/pro.jpg"
##                        text: "[size=26][color=#ffffff]jbsidis[/color][/size]\\n[size=14]cat-3.jpg[/size]"
##                        tile_text_color: app.theme_cls.accent_color


                    MyTile:
                        source: "a11.png"
                        text: ""
                        tile_text_color: [0,0,0,0]
                        FloatLayout:
                            AnchorLayout:
                                pos_hint: {"center_x": .5, "center_y": .9}
                                MDTextButton:
                                    halign: "center"
                                    text: "\\n\\n\\n\\n\\n\\n\\nLoading more...\\n\\n"
                                MDSpinner:
                                    size_hint: .1,.1

    MDToolbar:
        id: success_screen_toolbar
        title: "jbsidis"
        pos_hint: {"top": 1}
        right_action_items: [["progress-check", lambda: print(6)]]


    MDBottomAppBar:
        MDToolbar:
            id: success_screen_bottomappbar
            icon: "magnify"
            on_action_button: root.add_widget(app.sbar())
            type: 'bottom'
            mode: 'center'
            #elevation: '8dp'
            left_action_items: [["calendar-text", lambda: print(6)], ["account-group", lambda: print(6)]]
            right_action_items: [["magnify", lambda: print(6)], ["menu", lambda: print(6)]]

'''

class blanks1(BoxLayout):
    pass
class S(FloatLayout):
    pass


class Main(MDApp):
    def build(self):
        return Builder.load_string(kv)
    def sbar(self):
        self.root.ids.success_screen_toolbar.md_bg_color=[1,1,1,1]
        return S()


Main().run()

Then, in the python code we should define the functions with pyjnius that will get the java plugin classes to do operations with PDF files and we must combine that with KivyMD GUI to make it operational, is never impossible, what we need is creativity:

enter image description here

Related