Prepare documents for distribution
APACHE-2.0 License
Prepare documents for distribution.
A Pure-Python library built as a PDF toolkit.
Features:
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
In order to use this application you will need to have a Python 3 interpreter installed on your machine. A limited functionality executable application has been developed for Windows 10 to bypass Python as a system dependency.
Upgrade to the latest version of pip.
pip install --upgrade pip
Install the latest version from the PyPi distribution. Run pip install pdfconduit on the command line of your interpreter (virtual environment not required but recommended).
PyPi install
pip install pdfconduit
PyPi update (no cache dir to force install of newest version)
pip install --no-cache-dir --upgrade pdfconduit
PyPi install (with GUI)
pip install pdfconduit-gui
pdf
├── conduit
│ ├── __init__.py
│ ├── _version.py
│ ├── encrypt.py
│ ├── flatten.py
│ ├── merge.py
│ ├── rotate.py
│ ├── slice.py
│ ├── upscale.py
│ ├── utils
│ │ ├── __init__.py
│ │ ├── extract.py
│ │ ├── info.py
│ │ ├── lib
│ │ │ └── font
│ │ │ └── Vera.ttf
│ │ ├── lib.py
│ │ ├── path.py
│ │ ├── receipt.py
│ │ ├── samples.py
│ │ ├── view.py
│ │ └── write.py
│ └── watermark
│ ├── __init__.py
│ ├── add.py
│ ├── canvas
│ │ ├── __init__.py
│ │ ├── constructor.py
│ │ └── objects.py
│ ├── draw
│ │ ├── __init__.py
│ │ ├── image.py
│ │ └── pdf.py
│ ├── label.py
│ └── watermark.py
└── gui
├── __init__.py
├── _version.py
├── gui.py
└── lib
├── icon
│ ├── lock.ico
│ └── stamp.ico
└── img
├── Standard\ (no\ blocks).png
├── Standard.png
├── Wide\ (no\ blocks).png
└── Wide.png
pdfconduit
├── __init__.py
pdfconduit was developed to streamline the redundant process of creating watermarks, overlaying them on PDF files and adding security parameters before distribution to clients.
By removing the steps of launching Photoshop and Acrobat to perform a number of tasks process efficiency is dramatically increaded.
Outlined below are basic uses of the main classes and functions of the pdfconduit python package.
Generate watermark, add watermark to file and encrypt file
from pdfconduit import Watermark
# Set document and watermark params
pdf = 'mypdfdoc.pdf'
address = '2000 Main Street'
town = 'Boston'
state = 'MA'
# Initialize with PDF document
w = Watermark(pdf)
# Generate watermark file
w.draw(text1=address, text2=town + ', ' + state, include_copyright=True, rotate=30, opacity=0.08
# Add watermark file to PDF document
w.add()
>> > mypdfdoc_watermarked.pdf
# Encypt PDF document
w.encrypt(user_pw='foo', owner_pw='baz')
>> > mypdfdoc_secured.pdf
# Remove temp files and save receipt to disk
w.cleanup()
from pdfconduit import GUI
GUI.watermark()
Watermark.draw(image='Wide.png')
Watermark.draw(compress=0) # Uncompressed
Watermark.draw(compress=1) # Compressed
Layered | Flattened | |
---|---|---|
Finer parameter tuning with more options | Watermark harder to remove by merging img layers | |
Construction | * Creates a CanvasStr object for each text layer * Create CanvasImg object for watermark logo image file | * Draw each text layer to PIL image file * Draw PIL image with text to PIL image with logo to create one image file |
CanvasObjects | Initiate CanvasObjects() and use CanvasObjects().add() to add each string and image | Initiate CanvasObjects() and use CanvasObjects().add() to one CanvasImg instance |
Draw | Iterate CanvasObjects and draw each to canvas | Draw CanvasImg to canvas |
Save | Save canvas with text objects to layered PDF document | Save canvas with single image layer |
Watermark.draw(flatten=False) # Layered
Watermark.draw(flatten=True) # Flattened
Watermark.add(underneath=False) # Overlay
Watermark.add(underneath=True) # Underneath
Watermark.draw(opacity=0.09) # Set opacity to 9%
Encrypt a PDF file to add passwords and restrict permissions.
from pdfconduit import protect
# Required parameters
pdf = 'mypdfdoc.pdf'
user_pw = 'baz' # Password to open and view PDF
owner_pw = 'foo' # Password to change security settings
# Optional parameters
encrypt_128 = True # Encrypt using 128 bits (40 bits when False)
restrict_permission = True # Restrict permissions to print only (all allowed when false)
# Encrypt PDF document
encrypted = encrypt(pdf, user_pw, owner_pw, encrypt_128, restrict_permission)
>>> mypdfdoc_secured.pdf
Merge multiple PDF files into one concatenated PDF file.
from pdfconduit import Merge
# List of PDF paths
pdfs = ['doc1.pdf', 'doc2.pdf', 'doc3.pdf']
# Merge PDF files
merged = Merge(pdfs)
>>> merged.pdf
or
from pdfconduit import Merge
# List of PDF paths
pdfs = ['doc1.pdf', 'doc2.pdf', 'doc3.pdf']
# Specify output file name
output = 'combined doc'
# Merge PDF files
merged = Merge(pdfs, output_name=output)
>>> combined doc.pdf
Rotate a PDF document by increments of 90 degrees.
from pdfconduit import rotate
pdf = 'mypdfdoc.pdf' # PDF to-be rotated
rotate = 90 # Degress of rotation (clockwise)
# Rotate PDF file
rotated = rotate(pdf, rotate)
>>> mypdfdoc_rotated.pdf
Slice a PDF document to extract a range of page.
from pdfconduit import slicer
# Parameters
pdf = 'mypdfdoc.pdf'
first_page = 4
last_page = 17
# Slice PDF file
sliced = slicer(pdf, first_page, last_page)
>>> mypdfdoc_sliced.pdf
Add a text label to the bottom left corner of each page of PDF file.
from pdfconduit import Label
# Parameters
pdf = 'mypdfdoc.pdf'
label = 'Document updated 7/10/18'
# Label PDF file
labeled = Label(pdf, label)
>>> mypdfdoc_labeled.pdf
Watermark(document, remove_temps=True, open_file=True, tempdir=mkdtemp(), receipt=None, use_receipt=True)
Parameters | Type | Description |
---|---|---|
document | str |
PDF document full path |
remove_temps | bool |
Remove temporary files after completion |
open_file | bool |
Open file after completion |
tempdir | str or function |
Temporary directory for file writing |
receipt | cls |
Use existing Receipt object if already initiated |
use_receipt | bool |
Print receipt information to console and write to file |
def draw(self, text1, text2=None, copyright=True, image=default_image, rotate=30,
opacity=0.08, compress=0, flatten=False, add=False):
Parameters | Type | Description |
---|---|---|
text1 | str |
Text line 1 |
text2 | str |
Text line 2 |
copyright | bool |
Draw copyright and year to canvas |
image | str |
Logo image to be used as base watermark |
rotate | int |
Degrees to rotate canvas by |
opacity | float |
Watermark opacity`` |
compress | bool |
Compress watermark contents (not entire PDF) |
flatten | bool |
Draw watermark with multiple layers or a single flattened layer |
add | bool |
Add watermark to original document`` |
Return: Watermark file full path
def add(self, document=None, watermark=None, underneath=False, output=None, suffix='watermarked'):
Parameters | Type | Description |
---|---|---|
document | str |
PDF document full path |
watermark | str |
Watermark PDF full path |
underneath | bool |
Place watermark either under or over existing PDF document |
output | str |
Output file path |
suffix | str |
Suffix to append to existing PDF document file name |
Return: Watermarked PDF document full path
def encrypt(self, user_pw='', owner_pw=None, encrypt_128=True, restrict_permission=True):
Parameters | Type | Description |
---|---|---|
user_pw | str |
User password required to open and view PDF document |
owner_pw | str |
Owner password required to alter security settings and permissions |
encrypt_128 | bool |
Encrypt PDF document using 128 bit keys |
restrict_permission | str |
Restrict permissions to print only |
Return: Encrypted PDF document full path
Please read CONTRIBUTING.md for details on our code conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.