Skip to content

Python libraries to handle PDF files

License

Notifications You must be signed in to change notification settings

0x007E/pypdftool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version: 0.0.1 License: GPL v3

Small Python PDF creation/manipulation tool

This python scripts can be used with command line or as import from another script.

PDFpage

Create a PDF from a JSON config file. For additional information call:

python ./pdfpage.py -h
Short Expand Description
-h --help Help menu
-v --verbose Enable verbosity
-c --config Configuration file
-o --output Output PDF file
-f --force Overwrite output file

CLI-Usage

# Do not overwrite Test.pdf
python ./pdfpage.py -c ./pdf.page.json -o ./Test.pdf

# Overwrite Test.pdf
python ./pdfpage.py -c ./pdf.page.json -o ./Test.pdf -f

Python usage

# Create a blank page with size
page = PDFPage(Size(200, 200))

# Or create a blank page with format
page = PDFPage(Format(PageType.A4, Orientation.P))

# Add a text
text: Text = Text(Font("Times", 10), "Text")
text.position = Position(10, 10)
text.font = Font("Times", 10)
text.text = "Test"

# Add a link 
link: Link
link.position = Position(10,20)
link.size = Size(10, 10)
link.link = "https://github.com/0x007e"
link.text = "0x007e GitHub account"

# Add an image
image: Image
image.position = Position(10,20)
image.size = Size(10, 10)
image.path = "./Test.png"
image.text = "Test image"

# Add a rectangle
rectangle: Rectangle
rectangle.style = RectangleStyle(Rendering.D)
rectangle.position = Position(10,20)
rectangle.size = Size(10, 10)

# Add a circle
circle: Circle
circle.style = BaseStyle(Rendering.D)
circle.position = Position(10,20)
circle.radius = 10

# Get output as byte array
arr: bytearray = page.output()

# Or write pdf page
page.write("Output.pdf")

Create from JSON file

{
    "size": {
        "width": 100.22,
        "height": 150.22
    },
    "format": {
        "pagetype": "A4",
        "orientation": "L"
    },
    "content": {
        "text": [
            {
                "font": {
                    "family": "Times",
                    "size": 10
                },
                "position": {
                    "X": 10,
                    "Y": 9
                },
                "text": "Test"
            }
        ],
        "link": [
            {
                "link": "https://github.com/sunriax",
                "text": "Test",
                "position": {
                    "X": 10,
                    "Y": 10
                },
                "size": {
                    "width": 10,
                    "height": 10
                }
            }
        ],
        "image": [
            {
                "path": "./Test.png",
                "position": {
                    "X": 10,
                    "Y": 10
                },
                "size": {
                    "width": 50,
                    "height": 50
                }
            }
        ],
        "rectangle": [
            {
                "position": {
                    "X": 10,
                    "Y": 10
                },
                "size": {
                    "width": 50,
                    "height": 50
                },
                "style": {
                    "rendering": "D",
                    "corner": false,
                    "radius": 0
                }
            }
        ],
        "circle": [
            {
                "position": {
                    "X": 10,
                    "Y": 10
                },
                "radius": 10,
                "style": {
                    "rendering": "D"
                }
            }
        ]
    }
}

Use size or format

with open("data.json", "rb") as fp:
    config = json.loads(fp.read(), object_hook=lambda d: SimpleNamespace(**d)) 

    if(hasattr(config, f"{Size.__name__.lower()}")):
        size: Size = Size(config.size.width, config.size.height)
        page = PDFPage(size)
    elif(hasattr(config, f"{Format.__name__.lower()}")):
        format: Format = Format(config.format.pagetype, config.format.orientation)
        page = PDFPage(format)
    else:
        raise NameError(args.config)

    page.create(config.content)
    page.write("Test.pdf", False)

PDFoverlay

Overlay a PDF page in a PDF document

python ./pdfoverlay.py -h
Short Expand Description
-h --help Help menu
-v --verbose Enable verbosity
-i --input PDF document
-p --page Overlay PDF page
-o --output Output PDF file
-n --numbers Pages where overlay page should be layed on empty or 0=all pages
-w --watermark Add page as watermark

CLI-Usage

# Overlay page on all document pages
python ./pdfoverlay.py -i ./Document.pdf -p Overlay.pdf -o ./DO.pdf

# Overlay page on selected document pages
python ./pdfoverlay.py -i ./Document.pdf -p Overlay.pdf -o ./DO.pdf -n 1 3 4 8

# Underlay page as watermark
python ./pdfoverlay.py -i ./Document.pdf -p Overlay.pdf -o ./DO.pdf -w

Python usage

pdf: PDFOverlay=PDFOverlay("Document.pdf")

# All pages
pdf.add_overlay("Overlay.pdf")
pdf.add_overlay("Overlay.pdf", [ 0 ])

# Selected pages
pdf.add_overlay("Overlay.pdf", [ 1, 3, 6, 8 ])

# Add watermark to all pages
pdf.add_watermark("Overlay.pdf")

# Write to Document
pdf.write(args.output)

PDFmerge

Merge PDF pages into one document

python ./pdfmerge.py -h
Short Expand Description
-h --help Help menu
-i --input PDF documents
-o --output Output PDF file

CLI-Usage

python ./pdfmerge.py -i ./Document1.pdf ./Document2.pdf -o ./Document.pdf

Python usage

pdf: PDFMerge=PDFMerge("Document.pdf")

pdf.add_document("Document1.pdf")
pdf.add_document("Document2.pdf")
# ...
pdf.add_document("Documentn.pdf")

pdf.write()
pdf.close()

About

Python libraries to handle PDF files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages