PDF Command Line Tools

Download Free Trial
Buy Now
Read Manual
Licensing
Blog

Coherent PDF Python API Usage Examples

Back to main page

Here are some examples of the Coherent PDF Python API in action. For more instructions, see the Full PDF Manual.

#Merge example
import sys
import pycpdflib

#DLL loading depends on your own platform. These are the author's settings.
if sys.platform.startswith('darwin'):
    pycpdflib.loadDLL("/Users/john/repos/python-libcpdf/libpycpdf.so")
elif sys.platform.startswith('linux'):
    pycpdflib.loadDLL("../libpycpdf.so")
elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
    os.add_dll_directory("C:\\\\OCaml64/home/JohnWhitington/python-libcpdf/")
    pycpdflib.loadDLL("libpycpdf.dll")

#We will take the input hello.pdf and repeat it three times
mergepdf = pycpdflib.fromFile('hello.pdf', '')

#The list of PDFs to merge
pdfs = [mergepdf, mergepdf, mergepdf]

#Merge them
merged = pycpdflib.mergeSimple(pdfs)

#Write output
pycpdflib.toFile(merged, 'merged.pdf', False, False)

Read the file hello.pdf and triplicate it, writing to merged.pdf

#Squeeze example
import sys
import pycpdflib

#DLL loading depends on your own platform. These are the author's settings.
if sys.platform.startswith('darwin'):
    pycpdflib.loadDLL("/Users/john/repos/python-libcpdf/libpycpdf.so")
elif sys.platform.startswith('linux'):
    pycpdflib.loadDLL("../libpycpdf.so")
elif sys.platform.startswith('win32') or sys.platform.startswith('cygwin'):
    os.add_dll_directory("C:\\\\OCaml64/home/JohnWhitington/python-libcpdf/")
    pycpdflib.loadDLL("libpycpdf.dll")

#Load file
pdf = pycpdflib.fromFile('pycpdflibmanual.pdf', '')

#Squeeze it
pycpdflib.squeezeInMemory(pdf)

#Write output. We make sure to use toFileExt, and make object streams.
pycpdflib.toFileExt(pdf, 'squeezed.pdf', False, False, True, True, True)

Read the file pycpdflibmanual.pdf and squeeze it, writing to squeezed.pdf