2021-08-10 14:40:37 +02:00
|
|
|
def fromFile(filename, userpw):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Load a PDF file from a given file. Supply a user password (possibly
|
|
|
|
blank) in case the file is encrypted. It won't be decrypted, but sometimes
|
|
|
|
the password is needed just to load the file."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def fromFileLazy(filename, userpw):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Load a PDF from a file, doing only minimal parsing. The objects will be
|
|
|
|
read and parsed when they are actually needed. Use this when the whole
|
|
|
|
file won't be required. Also supply a user password (possibly blank) in
|
|
|
|
case the file is encrypted. It won't be decrypted, but sometimes the
|
|
|
|
password is needed just to load the file."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def fromMemory(data, userpw):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Load a file from a byte array and the user password (blank if none)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def fromMemoryLazy(data, userpw):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Load a file from from a byte array and the user password (blank if
|
2021-09-01 19:41:10 +02:00
|
|
|
none), but lazily like fromFileLazy."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def ptOfCm(i):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Convert a figure in centimetres to points (72 points to 1 inch)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def ptOfMm(i):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Convert a figure in millimetres to points (72 points to 1 inch)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def ptOfIn(i):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Convert a figure in inches to points (72 points to 1 inch)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def cmOfPt(i):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Convert a figure in points to centimetres (72 points to 1 inch)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def mmOfPt(i):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Convert a figure in points to millimetres (72 points to 1 inch)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def inOfPt(i):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Convert a figure in points to inches (72 points to 1 inch)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def parsePagespec(pdf, pagespec):
|
2022-01-23 14:06:53 +01:00
|
|
|
"""Parse a page specification such as "1-3,8-end" to a range with reference
|
|
|
|
to a given PDF (the PDF is supplied so that page ranges which reference
|
|
|
|
pages which do not exist are rejected)."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def validatePagespec(pagespec):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Validate a page specification, returning True or False, so far as is
|
2021-08-10 14:40:37 +02:00
|
|
|
possible in the absence of the actual document."""
|
|
|
|
|
|
|
|
def stringOfPagespec(pdf, r):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Build a page specification from a page range. For example, the range
|
|
|
|
containing 1,2,3,6,7,8 in a document of 8 pages might yield "1-3,6-end"."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def blankRange():
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Create a range with no pages in."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def pageRange(f, t):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Build a range from one page to another inclusive. For example,
|
|
|
|
pageRange(3,7) gives the range 3,4,5,6,7."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def all(pdf):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""The range containing all the pages in a given document."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def even(r):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""A range which contains just the even pages of another range."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def odd(r):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""A range which contains just the odd pages of another range."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def rangeUnion(a, b):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""The union of two ranges giving a range containing
|
2021-08-10 14:40:37 +02:00
|
|
|
the pages in range a and range b."""
|
|
|
|
|
|
|
|
def difference(a, b):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""The difference of two ranges, giving a range
|
2021-08-10 14:40:37 +02:00
|
|
|
containing all the pages in a except for those which are also in b."""
|
|
|
|
|
|
|
|
def removeDuplicates(r):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Deduplicates a range, returning a new one."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def rangeLength(r):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""The number of pages in a range."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def rangeGet(r, n):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Get the page number at position n in a range, where
|
2022-01-23 14:06:53 +01:00
|
|
|
n runs from 0 to rangeLength - 1."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def rangeAdd(r, p):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Add the page to a range, if it is not already
|
2021-08-10 14:40:37 +02:00
|
|
|
there."""
|
|
|
|
|
|
|
|
def isInRange(r, p):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Returns True if the page p is in the range r, False otherwise."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def pages(pdf):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Return the number of pages in a PDF."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def pagesFast(userpw, filename):
|
2024-04-17 01:59:48 +02:00
|
|
|
"""Return the number of pages in a given PDF, with given user password. It
|
|
|
|
tries to do this as fast as possible, without loading the whole file."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def toFile(pdf, filename, linearize, make_id):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Write the file to a given filename. If linearize is True, it will be
|
|
|
|
linearized, if supported by libcpdf. If make_id is True, it will be given a
|
2024-04-17 01:59:48 +02:00
|
|
|
new ID. NB: Unlike with the command line tool, cpdf, streams decompressed
|
|
|
|
during processing will not automatically be compressed when writing. Call
|
|
|
|
compress() first."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def toFileExt(pdf, filename, linearize, make_id, preserve_objstm,
|
|
|
|
generate_objstm, compress_objstm):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Write the file to a given filename. If linearize is True, it will be
|
|
|
|
linearized, if supported by libcpdf. If make_id is True, it will be given a
|
|
|
|
new ID. If preserve_objstm is True, existing object streams will be
|
|
|
|
preserved. If generate_objstm is True, object streams will be generated
|
|
|
|
even if not originally present. If compress_objstm is True, object streams
|
|
|
|
will be compressed (what we usually want). WARNING: the pdf argument will
|
|
|
|
be invalid after this call and should not be used again."""
|
|
|
|
|
2021-08-10 14:40:37 +02:00
|
|
|
def toMemory(pdf, linearize, make_id):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Write a file to memory, returning the buffer as a byte array of type
|
2024-04-17 01:59:48 +02:00
|
|
|
bytes. NB: Unlike with the command line tool, cpdf, streams decompressed
|
|
|
|
during processing will not automatically be compressed when writing. Call
|
|
|
|
compress() first."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def isEncrypted(pdf):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Returns True if a documented is encrypted, False otherwise."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def toFileEncrypted(pdf, method, permissions, ownerpw, userpw, linearize,
|
|
|
|
makeid, filename):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Write the file to a given filename encrypted with the given encryption
|
|
|
|
method, permissions list, and owener and user passwords. If linearize is
|
|
|
|
True, it will be linearized, if supported by libcpdf. If make_id is True,
|
|
|
|
it will be given a new ID."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def toFileEncryptedExt(pdf, method, permissions, ownerpw, userpw, linearize,
|
|
|
|
makeid, preserve_objstm, generate_objstm,
|
|
|
|
compress_objstm, filename):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Write the file to a given filename encrypted with the given encryption
|
|
|
|
method, permissions list, and owener and user passwords. If linearize is
|
|
|
|
True, it will be linearized, if supported by libcpdf. If make_id is True,
|
|
|
|
it will be given a new ID. If preserve_objstm is True, existing object
|
|
|
|
streams will be preserved. If generate_objstm is True, object streams will
|
|
|
|
be generated even if not originally present. If compress_objstm is True,
|
|
|
|
object streams will be compressed (what we usually want). WARNING: the pdf
|
|
|
|
argument will be invalid after this call and should not be used again."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def decryptPdf(pdf, userpw):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Attempts to decrypt a PDF using the given user password. An exception is
|
|
|
|
raised in the event of a bad password."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def decryptPdfOwner(pdf, ownerpw):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Attempts to decrypt a PDF using the given owner password. An exception
|
|
|
|
is raised in the event of a bad password."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def hasPermission(pdf, perm):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Returns True if the given permission (restriction) is present."""
|
2021-08-10 14:40:37 +02:00
|
|
|
|
|
|
|
def encryptionKind(pdf):
|
2021-09-01 19:41:10 +02:00
|
|
|
"""Return the encryption method currently in use on a document."""
|
2024-04-17 01:59:48 +02:00
|
|
|
|
|
|
|
def loadFont(name, filename):
|
|
|
|
"""Loads a TrueType font from the given file name, and names it. It may
|
|
|
|
then be used when adding text or drawing, using the name in place of a
|
|
|
|
standard font name. """
|