src/reportlab/pdfbase/pdfdoc.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4304 e7731618a9ee
child 4336 d39c63b164bc
permissions -rwxr-xr-x
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4304
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 484
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 2274
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfbase/pdfdoc.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 4233
diff changeset
     4
__version__='3.3.0'
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
     5
__doc__="""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     6
The module pdfdoc.py handles the 'outer structure' of PDF documents, ensuring that
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
     7
all objects are properly cross-referenced and indexed to the nearest byte.  The
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
     8
'inner structure' - the page descriptions - are presumed to be generated before
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
each page is saved.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    10
pdfgen.py calls this and provides a 'canvas' object to handle page marking operators.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    11
piddlePDF calls pdfgen and offers a high-level interface.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    12
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    13
The classes within this generally mirror structures in the PDF file
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    14
and are not part of any public interface.  Instead, canvas and font
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    15
classes are made available elsewhere for users to manipulate.
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    16
"""
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
    17
import types, binascii, codecs, time
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
    18
from collections import OrderedDict
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    19
from reportlab.pdfbase import pdfutils
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    20
from reportlab import rl_config
4195
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4188
diff changeset
    21
from reportlab.lib.utils import import_zlib, open_for_read, makeFileName, isSeq, isBytes, isUnicode, _digester, isStr, bytestr, isPy3, annotateException
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3747
diff changeset
    22
from reportlab.lib.rl_accel import escapePDF, fp_str, asciiBase85Encode, asciiBase85Decode
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
    23
from reportlab.pdfbase import pdfmetrics
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    24
from hashlib import md5
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    25
1653
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    26
from sys import platform
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    27
from sys import version_info
4116
cf49463fc067 Fix a bunch of undefined names. Mostly typos or missing imports.
Matthew Duggan <mgithub@guarana.org>
parents: 4067
diff changeset
    28
from sys import stderr
1653
87b869faf50f Fixed 1.5-breakage (we imported sys.version_info which did not exist then)
andy_robinson
parents: 1635
diff changeset
    29
1622
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    30
if platform[:4] == 'java' and version_info[:2] == (2, 1):
1677
1450177dd19e Exterminated all tab characters and added a test to make sure
andy_robinson
parents: 1653
diff changeset
    31
    # workaround for list()-bug in Jython 2.1 (should be fixed in 2.2)
1622
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    32
    def list(sequence):
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    33
        def f(x):
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    34
            return x
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
    35
        return list(map(f, sequence))
1622
dafbc4bbcb36 Added Dinu's Jython 2.1 workaround
rgbecker
parents: 1605
diff changeset
    36
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    37
class PDFError(Exception):
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    38
    pass
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
    39
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    40
# __InternalName__ is a special attribute that can only be set by the Document arbitrator
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    41
__InternalName__ = "__InternalName__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    42
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    43
# __RefOnly__ marks reference only elements that must be formatted on top level
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    44
__RefOnly__ = "__RefOnly__"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    45
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    46
# __Comment__ provides a (one line) comment to inline with an object ref, if present
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    47
#   if it is more than one line then percentize it...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    48
__Comment__ = "__Comment__"
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
    49
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    50
# name for standard font dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    51
BasicFonts = "BasicFonts"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    52
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    53
# name for the pages object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    54
Pages = "Pages"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    55
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    56
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
    57
PDF_VERSION_DEFAULT = (1, 3)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
    58
PDF_SUPPORT_VERSION = dict(     #map keyword to min version that supports it
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
    59
    transparency = (1, 4),
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
    60
    )
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    62
if isPy3:
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    63
    def pdfdocEnc(x):
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    64
        return x.encode('extpdfdoc') if isinstance(x,str) else x
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    65
else:
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    66
    def pdfdocEnc(x):
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    67
        return x.encode('extpdfdoc') if isinstance(x,unicode) else x
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    68
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
    69
def format(element, document, toplevel=0):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    70
    """Indirection step for formatting.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    71
       Ensures that document parameters alter behaviour
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    72
       of formatting for all elements.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    73
    """
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
    74
    if isinstance(element,PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    75
        if not toplevel and hasattr(element, __RefOnly__):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    76
            # the object cannot be a component at non top level.
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    77
            # make a reference to it and return it's format
2540
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
    78
            return document.Reference(element).format(document)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    79
        else:
2541
495aea8213fb pdfdoc.py: more optimisations
rgbecker
parents: 2540
diff changeset
    80
            f = element.format(document)
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    81
            if not rl_config.invariant and rl_config.pdfComments and hasattr(element, __Comment__):
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
    82
                f = pdfdocEnc("%% %s\n" % element.__Comment__)+f
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    83
            return f
3101
ec18b8a44cd2 reportlab:attempt to make PDF objects more explicit
rgbecker
parents: 3031
diff changeset
    84
    elif type(element) in (float, int):
2067
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
    85
        #use a controlled number formatting routine
ae4fe8b9c3f4 Now deterministically formats all numbers
andy_robinson
parents: 2045
diff changeset
    86
        #instead of str, so Jython/Python etc do not differ
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    87
        return pdfdocEnc(fp_str(element))
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    88
    elif isBytes(element):
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    89
        return element
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    90
    elif isUnicode(element):
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    91
        return pdfdocEnc(element)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    92
    else:
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
    93
        return pdfdocEnc(str(element))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
    94
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
    95
def xObjectName(externalname):
1560
382bbd1f20ae revert to older internal name for backward compatibility
rgbecker
parents: 1554
diff changeset
    96
    return "FormXob.%s" % externalname
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
    97
1605
fb7c31cc48d1 backwards compatible name
aaron_watters
parents: 1578
diff changeset
    98
# backwards compatibility
fb7c31cc48d1 backwards compatible name
aaron_watters
parents: 1578
diff changeset
    99
formName = xObjectName
fb7c31cc48d1 backwards compatible name
aaron_watters
parents: 1578
diff changeset
   100
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   101
# no encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   102
class NoEncryption:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   103
    def encode(self, t):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   104
        "encode a string, stream, text"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   105
        return t
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   106
    def prepare(self, document):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   107
        # get ready to do encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   108
        pass
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   109
    def register(self, objnum, version):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   110
        # enter a new direct object
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   111
        pass
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   112
    def info(self):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   113
        # the representation of self in file if any (should be None or PDFDict)
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   114
        return None
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   115
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   116
class PDFObject(object):
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   117
    pass
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   118
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   119
class DummyDoc(PDFObject):
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   120
    "used to bypass encryption when required"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   121
    encrypt = NoEncryption()
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   122
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   123
### the global document structure manager
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   124
class PDFDocument(PDFObject):
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   125
    # set this to define filters
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   126
    defaultStreamFilters = None
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   127
    encrypt = NoEncryption() # default no encryption
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   128
    def __init__(self,
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   129
                 dummyoutline=0,
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   130
                 compression=rl_config.pageCompression,
2476
19fb0900fba1 pdfdoc.py: minor changes to simplify PDFInfoL/U in other apps
rgbecker
parents: 2378
diff changeset
   131
                 invariant=rl_config.invariant,
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   132
                 filename=None,
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   133
                 pdfVersion=PDF_VERSION_DEFAULT,
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   134
                 ):
3570
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   135
        self._ID = None
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   136
        self.objectcounter = 0
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   137
        self.shadingCounter = 0
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   138
        self.inObject = None
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   139
        self.pageCounter = 1
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   140
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   141
        # allow None value to be passed in to mean 'give system defaults'
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   142
        if invariant is None:
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   143
            self.invariant = rl_config.invariant
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   144
        else:
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   145
            self.invariant = invariant
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   146
        self.setCompression(compression)
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   147
        self._pdfVersion = pdfVersion
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   148
        # signature for creating PDF ID
2993
033cbff23cb6 reportlab: fixes discovered during 2.6 testing
rgbecker
parents: 2964
diff changeset
   149
        sig = self.signature = md5()
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   150
        sig.update(b"a reportlab document")
1977
74967fd03ed4 Fix up invariance
rgbecker
parents: 1965
diff changeset
   151
        if not self.invariant:
1987
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   152
            cat = _getTimeStamp()
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   153
        else:
b2401bcddd6d Finally commit changes to allow invariant pdfencryption to work properly
rgbecker
parents: 1985
diff changeset
   154
            cat = 946684800.0
3808
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   155
        cat = ascii(cat)
3814
e207649ca52d pdfdoc.py: move utf8str -->utils.py renamed as bytestr
robin
parents: 3810
diff changeset
   156
        sig.update(bytestr(cat)) # initialize with timestamp digest
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   157
        # mapping of internal identifier ("Page001") to PDF objectnumber and generation number (34, 0)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   158
        self.idToObjectNumberAndVersion = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   159
        # mapping of internal identifier ("Page001") to PDF object (PDFPage instance)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   160
        self.idToObject = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   161
        # internal id to file location
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   162
        self.idToOffset = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   163
        # number to id
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   164
        self.numberToId = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   165
        cat = self.Catalog = self._catalog = PDFCatalog()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   166
        pages = self.Pages = PDFPages()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   167
        cat.Pages = pages
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   168
        if dummyoutline:
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   169
            outlines = PDFOutlines0()
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   170
        else:
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   171
            outlines = PDFOutlines()
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   172
        self.Outlines = self.outline = outlines
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   173
        cat.Outlines = outlines
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   174
        self.info = PDFInfo()
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   175
        self.info.invariant = self.invariant
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   176
        #self.Reference(self.Catalog)
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   177
        #self.Reference(self.Info)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   178
        self.fontMapping = {}
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   179
        #make an empty font dictionary
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   180
        DD = PDFDictionary({})
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   181
        DD.__Comment__ = "The standard fonts dictionary"
2944
72976f651dac pdfdoc.py: abandon unused var
rgbecker
parents: 2922
diff changeset
   182
        self.Reference(DD, BasicFonts)
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   183
        self.delayedFonts = []
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   184
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   185
    def setCompression(self, onoff):
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   186
        # XXX: maybe this should also set self.defaultStreamFilters?
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   187
        self.compression = onoff
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   188
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   189
    def ensureMinPdfVersion(self, *keys):
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   190
        "Ensure that the pdf version is greater than or equal to that specified by the keys"
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   191
        for k in keys:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   192
            self._pdfVersion = max(self._pdfVersion, PDF_SUPPORT_VERSION[k])
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   193
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   194
    def updateSignature(self, thing):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   195
        "add information to the signature"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   196
        if self._ID: return # but not if its used already!
3814
e207649ca52d pdfdoc.py: move utf8str -->utils.py renamed as bytestr
robin
parents: 3810
diff changeset
   197
        self.signature.update(bytestr(thing))
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   198
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   199
    def ID(self):
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   200
        "A unique fingerprint for the file (unless in invariant mode)"
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   201
        if self._ID:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   202
            return self._ID
1985
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   203
        digest = self.signature.digest()
20a5484dd812 Fix up for 1.1.4 PIL palette access
rgbecker
parents: 1977
diff changeset
   204
        doc = DummyDoc()
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   205
        IDs = PDFText(digest,enc='raw').format(doc)
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   206
        self._ID = (b'\n['+IDs+IDs+b']\n% ReportLab generated PDF document -- digest (http://www.reportlab.com)\n')
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   207
        return self._ID
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   208
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   209
    def SaveToFile(self, filename, canvas):
4284
73c620695dff try to prevent multiple saving contrib by Tim Meneely
robin
parents: 4281
diff changeset
   210
        if getattr(self,'_savedToFile',False):
73c620695dff try to prevent multiple saving contrib by Tim Meneely
robin
parents: 4281
diff changeset
   211
            raise RunTimeError("class %s instances can only be saved once" % self.__class__.__name__)
73c620695dff try to prevent multiple saving contrib by Tim Meneely
robin
parents: 4281
diff changeset
   212
        self._savedToFile = True
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   213
        if hasattr(getattr(filename, "write",None),'__call__'):
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   214
            myfile = 0
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   215
            f = filename
3687
1fead6079288 utils.py: add makeFileName and use in pdfdoc.py
robin
parents: 3630
diff changeset
   216
            filename = makeFileName(getattr(filename,'name',''))
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   217
        else :
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   218
            myfile = 1
3687
1fead6079288 utils.py: add makeFileName and use in pdfdoc.py
robin
parents: 3630
diff changeset
   219
            filename = makeFileName(filename)
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   220
            f = open(filename, "wb")
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   221
        data = self.GetPDFData(canvas)
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3723
diff changeset
   222
        if isUnicode(data):
3909
7e890b22bea3 pdfdoc.py: change possibly unwanted final encode
robin
parents: 3858
diff changeset
   223
            data = data.encode('latin1')
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   224
        f.write(data)
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   225
        if myfile:
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   226
            f.close()
2010
83e136234c0f Added import of os
rgbecker
parents: 2009
diff changeset
   227
            import os
2009
a6f1bd476301 Slight change to markfilename usage
rgbecker
parents: 1987
diff changeset
   228
            if os.name=='mac':
a6f1bd476301 Slight change to markfilename usage
rgbecker
parents: 1987
diff changeset
   229
                from reportlab.lib.utils import markfilename
a6f1bd476301 Slight change to markfilename usage
rgbecker
parents: 1987
diff changeset
   230
                markfilename(filename) # do platform specific file junk
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   231
        if getattr(canvas,'_verbosity',None): print('saved %s' % (filename,))
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   232
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   233
    def GetPDFData(self, canvas):
1635
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   234
        # realize delayed fonts
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   235
        for fnt in self.delayedFonts:
45fec4b75ae3 Added Marius Gedminas' TTF support
rgbecker
parents: 1634
diff changeset
   236
            fnt.addObjects(self)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   237
        # add info stuff to signature
1838
f7eeee67832c Changes to allow invariant documents when needed
andy_robinson
parents: 1799
diff changeset
   238
        self.info.invariant = self.invariant
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   239
        self.info.digest(self.signature)
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   240
        ### later: maybe add more info to sig?
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   241
        # prepare outline
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   242
        self.Reference(self.Catalog)
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   243
        self.Reference(self.info)
483
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   244
        outline = self.outline
c4c3969d1e30 undid last checkin and added an option for a default outline (different fix)
aaron_watters
parents: 482
diff changeset
   245
        outline.prepare(self, canvas)
1965
02ef6a007f0b David Fraser's GetPDFData patch (to avoid unwanted StringIOs
rgbecker
parents: 1903
diff changeset
   246
        return self.format()
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   247
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   248
    def inPage(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   249
        """specify the current object as a page (enables reference binding and other page features)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   250
        if self.inObject is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   251
            if self.inObject=="page": return
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   252
            raise ValueError("can't go in page already in object %s" % self.inObject)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   253
        self.inObject = "page"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   254
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   255
    def inForm(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   256
        """specify that we are in a form xobject (disable page features, etc)"""
742
df72775fc33d fixed minor bug in forms handling
aaron_watters
parents: 724
diff changeset
   257
        # don't need this check anymore since going in a form pushes old context at canvas level.
df72775fc33d fixed minor bug in forms handling
aaron_watters
parents: 724
diff changeset
   258
        #if self.inObject not in ["form", None]:
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   259
        #    raise ValueError("can't go in form already in object %s" % self.inObject)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   260
        self.inObject = "form"
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   261
        # don't need to do anything else, I think...
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   262
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   263
    def getInternalFontName(self, psfontname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   264
        fm = self.fontMapping
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   265
        if psfontname in fm:
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   266
            return fm[psfontname]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   267
        else:
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   268
            try:
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   269
                # does pdfmetrics know about it? if so, add
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   270
                fontObj = pdfmetrics.getFont(psfontname)
2678
38d18a697cd0 reportlab: Python2.5 changes + minor cosmetics and improvements
rgbecker
parents: 2666
diff changeset
   271
                if fontObj._dynamicFont:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2902
diff changeset
   272
                    raise PDFError("getInternalFontName(%s) called for a dynamic font" % repr(psfontname))
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   273
                fontObj.addObjects(self)
873
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   274
                return fm[psfontname]
62ba1388703d New font embedding stuff.
rgbecker
parents: 742
diff changeset
   275
            except KeyError:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2902
diff changeset
   276
                raise PDFError("Font %s not known!" % repr(psfontname))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   277
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   278
    def thisPageName(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   279
        return "Page"+repr(self.pageCounter)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   280
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   281
    def thisPageRef(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   282
        return PDFObjectReference(self.thisPageName())
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   283
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   284
    def addPage(self, page):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   285
        name = self.thisPageName()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   286
        self.Reference(page, name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   287
        self.Pages.addPage(page)
2919
1f7a61d79435 pdfdoc.py: some conveniences and a minor change
rgbecker
parents: 2915
diff changeset
   288
        self.pageCounter += 1
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   289
        self.inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   290
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   291
    def addForm(self, name, form):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   292
        """add a Form XObject."""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   293
        # XXX should check that name is a legal PDF name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   294
        if self.inObject != "form":
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   295
            self.inForm()
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   296
        self.Reference(form, xObjectName(name))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   297
        self.inObject = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   298
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   299
    def annotationName(self, externalname):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   300
        return "Annot.%s"%externalname
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   301
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   302
    def addAnnotation(self, name, annotation):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   303
        self.Reference(annotation, self.annotationName(name))
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   304
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   305
    def refAnnotation(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   306
        internalname = self.annotationName(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   307
        return PDFObjectReference(internalname)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   308
3570
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   309
    def addShading(self, shading):
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   310
         name = "Sh%d" % self.shadingCounter
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   311
         self.Reference(shading, name)
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   312
         self.shadingCounter += 1
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   313
         return name
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
   314
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   315
    def addColor(self,cmyk):
3226
cd06c1172678 pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents: 3198
diff changeset
   316
        sname = cmyk.spotName
3588
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   317
        if not sname:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   318
            if cmyk.cyan==0 and cmyk.magenta==0 and cmyk.yellow==0:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   319
                sname = 'BLACK'
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   320
            elif cmyk.black==0 and cmyk.magenta==0 and cmyk.yellow==0:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   321
                sname = 'CYAN'
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   322
            elif cmyk.cyan==0 and cmyk.black==0 and cmyk.yellow==0:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   323
                sname = 'MAGENTA'
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   324
            elif cmyk.cyan==0 and cmyk.magenta==0 and cmyk.black==0:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   325
                sname = 'YELLOW'
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   326
            if not sname:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   327
                raise ValueError("CMYK colour %r used without a spotName" % cmyk)
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   328
            else:
aed7c74d52a2 pdfdoc.py: synthesize a spot for simple CMYK or fail if no spotName in addColor
rgbecker
parents: 3570
diff changeset
   329
                cmyk = cmyk.clone(spotName = sname)
3226
cd06c1172678 pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents: 3198
diff changeset
   330
        name = PDFName(sname)[1:]
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   331
        if name not in self.idToObject:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   332
            sep = PDFSeparationCMYKColor(cmyk).value()  #PDFArray([/Separation /name /DeviceCMYK tint_tf])
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   333
            self.Reference(sep,name)
3226
cd06c1172678 pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents: 3198
diff changeset
   334
        return name,sname
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   335
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   336
    def setTitle(self, title):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   337
        "embeds in PDF file"
2666
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   338
        if title is None:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   339
            self.info.title = '(anonymous)'
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   340
        else:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   341
            self.info.title = title
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   342
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   343
    def setAuthor(self, author):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   344
        "embedded in PDF file"
2666
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   345
        #allow resetting to clear it
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   346
        if author is None:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   347
            self.info.author = '(anonymous)'
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   348
        else:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   349
            self.info.author = author
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   350
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   351
    def setSubject(self, subject):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   352
        "embeds in PDF file"
2666
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   353
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   354
        #allow resetting to clear it
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   355
        if subject is None:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   356
            self.info.subject = '(unspecified)'
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   357
        else:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   358
            self.info.subject = subject
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   359
3456
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   360
    def setCreator(self, creator):
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   361
        "embeds in PDF file"
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   362
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   363
        #allow resetting to clear it
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   364
        if creator is None:
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   365
            self.info.creator = '(unspecified)'
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   366
        else:
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   367
            self.info.creator = creator
3143bbebbefe added PDF Creator to document info
andy
parents: 3359
diff changeset
   368
2666
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   369
    def setKeywords(self, keywords):
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   370
        "embeds a string containing keywords in PDF file"
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   371
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   372
        #allow resetting to clear it but ensure it's a string
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   373
        if keywords is None:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   374
            self.info.keywords = ''
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   375
        else:
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   376
            self.info.keywords = keywords
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   377
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   378
    def setDateFormatter(self, dateFormatter):
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   379
        self.info._dateFormatter = dateFormatter
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   380
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   381
    def getAvailableFonts(self):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   382
        fontnames = list(self.fontMapping.keys())
1151
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   383
        # the standard 14 are also always available! (even if not initialized yet)
3975
4a3599863c11 eliminate from . imports in favour of absolutes to allow running modules
robin
parents: 3909
diff changeset
   384
        from reportlab.pdfbase import _fontdata
1151
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   385
        for name in _fontdata.standardFonts:
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   386
            if name not in fontnames:
431ef9e78aae "the 14 std fonts" should always be "available"
aaron_watters
parents: 1063
diff changeset
   387
                fontnames.append(name)
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   388
        fontnames.sort()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   389
        return fontnames
681
934a4f24ea5d Added explicit font and encoding support
andy_robinson
parents: 674
diff changeset
   390
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   391
    def format(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   392
        # register the Catalog/INfo and then format the objects one by one until exhausted
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   393
        # (possible infinite loop if there is a bug that continually makes new objects/refs...)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   394
        # Prepare encryption
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   395
        self.encrypt.prepare(self)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   396
        cat = self.Catalog
603
a6f460e53b00 forms in forms now work
aaron_watters
parents: 595
diff changeset
   397
        info = self.info
4281
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   398
        self.Reference(cat)
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   399
        self.Reference(info)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   400
        # register the encryption dictionary if present
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   401
        encryptref = None
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   402
        encryptinfo = self.encrypt.info()
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   403
        if encryptinfo:
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   404
            encryptref = self.Reference(encryptinfo)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   405
        # make std fonts (this could be made optional
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   406
        counter = 0 # start at first object (object 1 after preincrement)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   407
        ids = [] # the collection of object ids in object number order
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   408
        numbertoid = self.numberToId
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   409
        idToNV = self.idToObjectNumberAndVersion
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   410
        idToOb = self.idToObject
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   411
        idToOf = self.idToOffset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   412
        ### note that new entries may be "appended" DURING FORMATTING
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   413
        # __accum__ allows objects to know where they are in the file etc etc
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   414
        self.__accum__ = File = PDFFile(self._pdfVersion) # output collector
4281
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   415
        while True:
2575
0cba68b93555 reportlab-utf8 moved to trunk
rgbecker
parents: 2571
diff changeset
   416
            counter += 1 # do next object...
4281
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   417
            if counter not in numbertoid: break
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   418
            oid = numbertoid[counter]
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   419
            #printidToOb
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   420
            obj = idToOb[oid]
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   421
            IO = PDFIndirectObject(oid, obj)
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   422
            # register object number and version
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   423
            IOf = IO.format(self)
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   424
            # add a comment to the PDF output
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   425
            if not rl_config.invariant and rl_config.pdfComments:
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   426
                try:
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   427
                    classname = obj.__class__.__name__
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   428
                except:
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   429
                    classname = ascii(obj)
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   430
                File.add("%% %s: class %s \n" % (ascii(oid), classname[:50]))
4281
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   431
            offset = File.add(IOf)
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   432
            idToOf[oid] = offset
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
   433
            ids.append(oid)
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   434
        del self.__accum__
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   435
        # sanity checks (must happen AFTER formatting)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   436
        lno = len(numbertoid)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   437
        if counter-1!=lno:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   438
            raise ValueError("counter %s doesn't match number to id dictionary %s" %(counter, lno))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   439
        # now add the xref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   440
        xref = PDFCrossReferenceTable()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   441
        xref.addsection(0, ids)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   442
        xreff = xref.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   443
        xrefoffset = File.add(xreff)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   444
        # now add the trailer
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   445
        trailer = PDFTrailer(
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   446
            startxref = xrefoffset,
1309
2b674954263d /Size in Trailer off by one.
aaron_watters
parents: 1179
diff changeset
   447
            Size = lno+1,
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   448
            Root = self.Reference(cat),
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   449
            Info = self.Reference(info),
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   450
            Encrypt = encryptref,
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   451
            ID = self.ID(),
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   452
            )
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   453
        trailerf = trailer.format(self)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   454
        File.add(trailerf)
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   455
        for ds in getattr(self,'_digiSigs',[]):
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   456
            ds.sign(File)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   457
        # return string format for pdf file
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   458
        return File.format(self)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   459
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   460
    def hasForm(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   461
        """test for existence of named form"""
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   462
        internalname = xObjectName(name)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   463
        return internalname in self.idToObject
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   464
2679
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2678
diff changeset
   465
    def getFormBBox(self, name, boxType="MediaBox"):
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2678
diff changeset
   466
        """get the declared bounding box of the form as a list.
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2678
diff changeset
   467
        If you specify a different PDF box definition (e.g. the
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2678
diff changeset
   468
        ArtBox) and it has one, that's what you'll get."""
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   469
        internalname = xObjectName(name)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   470
        if internalname in self.idToObject:
1430
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   471
            theform = self.idToObject[internalname]
2798
40f791c3cbf8 pdfdoc.py: allow for proper external forms info
rgbecker
parents: 2778
diff changeset
   472
            if hasattr(theform,'_extra_pageCatcher_info'):
40f791c3cbf8 pdfdoc.py: allow for proper external forms info
rgbecker
parents: 2778
diff changeset
   473
                return theform._extra_pageCatcher_info[boxType]
1430
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   474
            if isinstance(theform, PDFFormXObject):
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   475
                # internally defined form
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   476
                return theform.BBoxList()
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   477
            elif isinstance(theform, PDFStream):
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   478
                # externally defined form
2679
b8464568f5e2 tweaks to support PDF art boxes
andy
parents: 2678
diff changeset
   479
                return list(theform.dictionary.dict[boxType].sequence)
1430
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   480
            else:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   481
                raise ValueError("I don't understand the form instance %s" % repr(name))
1430
1ba38d150a9e support for getting form bounding boxes
aaron_watters
parents: 1395
diff changeset
   482
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   483
    def getXObjectName(self, name):
1395
65c2c66a3a1b canvas.hasForm now gives honest answers.
andy_robinson
parents: 1363
diff changeset
   484
        """Lets canvas find out what form is called internally.
65c2c66a3a1b canvas.hasForm now gives honest answers.
andy_robinson
parents: 1363
diff changeset
   485
        Never mind whether it is defined yet or not."""
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   486
        return xObjectName(name)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   487
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   488
    def xobjDict(self, formnames):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   489
        """construct an xobject dict (for inclusion in a resource dict, usually)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   490
           from a list of form names (images not yet supported)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   491
        D = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   492
        for name in formnames:
1552
f83acda4ec71 expanded docco and fixed 'natural size'
andy_robinson
parents: 1510
diff changeset
   493
            internalname = xObjectName(name)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   494
            reference = PDFObjectReference(internalname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   495
            D[internalname] = reference
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   496
        #print "xobjDict D", D
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   497
        return PDFDictionary(D)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   498
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   499
    def Reference(self, obj, name=None):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   500
        ### note references may "grow" during the final formatting pass: don't use d.keys()!
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   501
        # don't make references to other references, or non instances, unless they are named!
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   502
        iob = isinstance(obj,PDFObject)
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   503
        idToObject = self.idToObject
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   504
        if name is None and (not iob or obj.__class__ is PDFObjectReference):
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   505
            return obj
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   506
        if hasattr(obj, __InternalName__):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   507
            # already registered
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   508
            intname = obj.__InternalName__
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   509
            if name is not None and name!=intname:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   510
                raise ValueError("attempt to reregister object %s with new name %s" % (
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   511
                    repr(intname), repr(name)))
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   512
            if intname not in idToObject:
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   513
                raise ValueError("object of type %s named as %s, but not registered" % (type(obj),ascii(intname)))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   514
            return PDFObjectReference(intname)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   515
        # otherwise register the new object
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   516
        objectcounter = self.objectcounter = self.objectcounter+1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   517
        if name is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   518
            name = "R"+repr(objectcounter)
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   519
        if name in idToObject:
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   520
            other = idToObject[name]
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   521
            if other!=obj:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   522
                raise ValueError("redefining named object: "+repr(name))
628
7f5cd20a02f1 very subtle bug that is only hit by pagecatcher: indirect references to integers now work.
aaron_watters
parents: 603
diff changeset
   523
            return PDFObjectReference(name)
3101
ec18b8a44cd2 reportlab:attempt to make PDF objects more explicit
rgbecker
parents: 3031
diff changeset
   524
        if iob:
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   525
            obj.__InternalName__ = name
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   526
        #print "name", name, "counter", objectcounter
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   527
        self.idToObjectNumberAndVersion[name] = (objectcounter, 0)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   528
        self.numberToId[objectcounter] = name
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   529
        idToObject[name] = obj
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   530
        return PDFObjectReference(name)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   531
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   532
### chapter 4 Objects
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   533
PDFtrue = "true"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   534
PDFfalse = "false"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   535
PDFnull = "null"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   536
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   537
class PDFText(PDFObject):
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   538
    def __init__(self, t, enc='utf-8'):
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   539
        self.t = t
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   540
        self.enc = enc
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   541
    def format(self, document):
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   542
        t = self.t
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3723
diff changeset
   543
        if isUnicode(t):
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   544
            t = t.encode(self.enc)
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   545
        result = binascii.hexlify(document.encrypt.encode(t))
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   546
        return b"<" + result + b">"
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   547
    def __str__(self):
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   548
        dummydoc = DummyDoc()
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   549
        return self.format(dummydoc)
1014
ba7f9c1294ea added missing PDFText() function for consistency (needed by pagecatcher)
aaron_watters
parents: 873
diff changeset
   550
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   551
def PDFnumber(n):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   552
    return n
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   553
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   554
import re
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   555
_re_cleanparens=re.compile('[^()]')
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   556
del re
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   557
def _isbalanced(s):
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   558
    '''test whether a string is balanced in parens'''
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   559
    s = _re_cleanparens.sub('',s)
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   560
    n = 0
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   561
    for c in s:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   562
        if c=='(': n+=1
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   563
        else:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   564
            n -= 1
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   565
            if n<0: return 0
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   566
    return not n and 1 or 0
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   567
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   568
def _checkPdfdoc(utext):
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   569
    '''return true if no Pdfdoc encoding errors'''
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   570
    try:
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   571
        utext.encode('pdfdoc')
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   572
        return 1
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   573
    except UnicodeEncodeError as e:
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   574
        return 0
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   575
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   576
class PDFString(PDFObject):
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   577
    def __init__(self, s, escape=1, enc='auto'):
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   578
        '''s can be unicode/utf8 or a PDFString
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   579
        if escape is true then the output will be passed through escape
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   580
        if enc is raw then the string will be left alone
4304
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   581
        if enc is auto we'll try and automatically adapt to utf_16_be/utf_16_le if the
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   582
        effective string is not entirely in pdfdoc
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   583
        '''
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   584
        if isinstance(s,PDFString):
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   585
            self.s = s.s
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   586
            self.escape = s.escape
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   587
            self.enc = s.enc
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   588
        else:
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   589
            self.s = s
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   590
            self.escape = escape
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   591
            self.enc = enc
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   592
    def format(self, document):
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   593
        s = self.s
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   594
        enc = getattr(self,'enc','auto')
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   595
        if isBytes(s):
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   596
            if enc is 'auto':
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   597
                try:
4304
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   598
                    if s.startswith(codecs.BOM_UTF16_BE):
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   599
                        u = s.decode('utf_16_be')
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   600
                    elif s.startswith(codecs.BOM_UTF16_LE):
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   601
                        u = s.decode('utf_16_le')
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   602
                    else:
e7731618a9ee allow for UTF_16_LE BOM, fix for bug contributed by Michael Poindexter mpoindexter@housecanary.com; version-->3.3.27
robin
parents: 4299
diff changeset
   603
                        u = s.decode('utf8')
2922
168b91cbfefc reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents: 2919
diff changeset
   604
                    if _checkPdfdoc(u):
168b91cbfefc reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents: 2919
diff changeset
   605
                        s = u.encode('pdfdoc')
168b91cbfefc reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents: 2919
diff changeset
   606
                    else:
168b91cbfefc reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents: 2919
diff changeset
   607
                        s = codecs.BOM_UTF16_BE+u.encode('utf_16_be')
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   608
                except:
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2902
diff changeset
   609
                    try:
2922
168b91cbfefc reportlab: attempt to make heuristic for PDFString formatting more sensible
rgbecker
parents: 2919
diff changeset
   610
                        s.decode('pdfdoc')
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2902
diff changeset
   611
                    except:
4116
cf49463fc067 Fix a bunch of undefined names. Mostly typos or missing imports.
Matthew Duggan <mgithub@guarana.org>
parents: 4067
diff changeset
   612
                        stderr.write('Error in %s' % (repr(s),))
2915
f2c1f8ef3b51 reportlab: obsoletisms removed
rgbecker
parents: 2902
diff changeset
   613
                        raise
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3723
diff changeset
   614
        elif isUnicode(s):
2595
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   615
            if enc is 'auto':
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   616
                if _checkPdfdoc(s):
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   617
                    s = s.encode('pdfdoc')
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   618
                else:
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   619
                    s = codecs.BOM_UTF16_BE+s.encode('utf_16_be')
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   620
            else:
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   621
                s = codecs.BOM_UTF16_BE+s.encode('utf_16_be')
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   622
        else:
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   623
            raise ValueError('PDFString argument must be str/unicode not %s' % type(s))
ffb020e8ab50 reportlab: PDFString vs Unicode changes
rgbecker
parents: 2575
diff changeset
   624
2524
7e6da7c7fe2c merge stable 2687:2688 pdfdoc.py: attempt to robustify escape attribute access
rgbecker
parents: 2517
diff changeset
   625
        escape = getattr(self,'escape',1)
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   626
        if not isinstance(document.encrypt,NoEncryption):
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   627
            s = document.encrypt.encode(s)
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   628
            escape = 1
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   629
        if escape:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   630
            try:
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3747
diff changeset
   631
                es = "(%s)" % escapePDF(s)
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   632
            except:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   633
                raise ValueError("cannot escape %s %s" % (s, repr(s)))
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   634
            if escape&2:
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   635
                es = es.replace('\\012','\n')
3995
c266a869165f pdfdoc.py: fix bug in PDFString.format
robin
parents: 3975
diff changeset
   636
            if escape&4 and _isbalanced(es):
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   637
                es = es.replace('\\(','(').replace('\\)',')')
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   638
            return pdfdocEnc(es)
2517
c8f07edb561d pdfdoc: allow special escaping in Annotations and PDFString
rgbecker
parents: 2487
diff changeset
   639
        else:
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   640
            return b'(' + s + b')'
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   641
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   642
    def __str__(self):
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3747
diff changeset
   643
        return "(%s)" % escapePDF(self.s)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   644
2540
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   645
def PDFName(data,lo=chr(0x21),hi=chr(0x7e)):
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   646
    # might need to change this to class for encryption
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   647
    #  NOTE: RESULT MUST ALWAYS SUPPORT MEANINGFUL COMPARISONS (EQUALITY) AND HASH
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   648
    # first convert the name
2540
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   649
    L = list(data)
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   650
    for i,c in enumerate(L):
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   651
        if c<lo or c>hi or c in "%()<>{}[]#":
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   652
            L[i] = "#"+hex(ord(c))[2:] # forget the 0x thing...
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   653
    return "/"+(''.join(L))
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   654
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   655
class PDFDictionary(PDFObject):
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   656
    multiline = True
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   657
    def __init__(self, dict=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   658
        """dict should be namestring to value eg "a": 122 NOT pdfname to value NOT "/a":122"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   659
        if dict is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   660
            self.dict = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   661
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   662
            self.dict = dict.copy()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   663
    def __setitem__(self, name, value):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   664
        self.dict[name] = value
2919
1f7a61d79435 pdfdoc.py: some conveniences and a minor change
rgbecker
parents: 2915
diff changeset
   665
    def __getitem__(self, a):
1f7a61d79435 pdfdoc.py: some conveniences and a minor change
rgbecker
parents: 2915
diff changeset
   666
        return self.dict[a]
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   667
    def __contains__(self,a):
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   668
        return a in self.dict
2730
b3fc23a74fd1 reportlab: Dirk Holtwick's editor's fixes :)
rgbecker
parents: 2679
diff changeset
   669
    def Reference(self, name, document):
2541
495aea8213fb pdfdoc.py: more optimisations
rgbecker
parents: 2540
diff changeset
   670
        self.dict[name] = document.Reference(self.dict[name])
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   671
    def format(self, document,IND=b'\n '):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   672
        dict = self.dict
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   673
        try:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   674
            keys = list(dict.keys())
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   675
        except:
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   676
            print(ascii(dict))
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   677
            raise
4147
7d81f51bb6c8 allow objects to know their place and prepare for possible signers
robin
parents: 4116
diff changeset
   678
        if not isinstance(dict,OrderedDict): keys.sort()
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   679
        L = [(format(PDFName(k),document)+b" "+format(dict[k],document)) for k in keys]
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   680
        if (self.multiline and rl_config.pdfMultiLine) or self.multiline=='forced':
2541
495aea8213fb pdfdoc.py: more optimisations
rgbecker
parents: 2540
diff changeset
   681
            L = IND.join(L)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   682
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   683
            # break up every 6 elements anyway
2540
fadac956b695 pdfdoc.py: speed improvements
rgbecker
parents: 2524
diff changeset
   684
            t=L.insert
3843
ee97902ffa44 pdfdoc.py, pdfform.py & pdfutils.py: eliminate LINEEND
robin
parents: 3840
diff changeset
   685
            for i in reversed(range(6, len(L), 6)):
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   686
                t(i,b'\n ')
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   687
            L = b" ".join(L)
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   688
        return b'<<\n'+L+b'\n>>'
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   689
2666
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   690
    def copy(self):
a72c25280be2 facilitating saving info dictionary
andy
parents: 2595
diff changeset
   691
        return PDFDictionary(self.dict)
2778
8cd23c6fbff3 reportlab: minor fix to linkRect and friends
rgbecker
parents: 2767
diff changeset
   692
3630
0302fc9eb428 added PDFDictionary normalize
rgbecker
parents: 3629
diff changeset
   693
    def normalize(self):
0302fc9eb428 added PDFDictionary normalize
rgbecker
parents: 3629
diff changeset
   694
        #normalize the names to use RL standard ie Name not /Name
0302fc9eb428 added PDFDictionary normalize
rgbecker
parents: 3629
diff changeset
   695
        D = self.dict
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   696
        K = [k for k in D.keys() if k.startswith('/')]
3630
0302fc9eb428 added PDFDictionary normalize
rgbecker
parents: 3629
diff changeset
   697
        for k in K:
0302fc9eb428 added PDFDictionary normalize
rgbecker
parents: 3629
diff changeset
   698
            D[k[1:]] = D.pop(k)
0302fc9eb428 added PDFDictionary normalize
rgbecker
parents: 3629
diff changeset
   699
2869
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   700
class checkPDFNames:
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   701
    def __init__(self,*names):
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   702
        self.names = list(map(PDFName,names))
2869
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   703
    def __call__(self,value):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   704
        if not value.startswith('/'):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   705
            value=PDFName(value)
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   706
        if value in self.names:
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   707
            return value
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   708
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   709
def checkPDFBoolean(value):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   710
    if value in ('true','false'): return value
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   711
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   712
class CheckedPDFDictionary(PDFDictionary):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   713
    validate = {}
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   714
    def __init__(self,dict=None,validate=None):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   715
        PDFDictionary.__init__(self,dict)
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   716
        if validate: self.validate = validate
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   717
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   718
    def __setitem__(self,name,value):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   719
        if name not in self.validate:
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   720
            raise ValueError('invalid key, %r' % name)
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   721
        cvalue = self.validate[name](value)
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   722
        if cvalue is None:
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   723
            raise ValueError('Bad value %r for key %r' % (value,name))
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   724
        PDFDictionary.__setitem__(self,name,cvalue)
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   725
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   726
class ViewerPreferencesPDFDictionary(CheckedPDFDictionary):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   727
    validate=dict(
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   728
                HideToolbar=checkPDFBoolean,
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   729
                HideMenubar=checkPDFBoolean,
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   730
                HideWindowUI=checkPDFBoolean,
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   731
                FitWindow=checkPDFBoolean,
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   732
                CenterWindow=checkPDFBoolean,
3460
bbfe1f84fc80 pdfdoc.py: added DisplayDocTitle to viewer settings, contributed by Mark Erbaugh
rgbecker
parents: 3456
diff changeset
   733
                DisplayDocTitle=checkPDFBoolean,    #contributed by mark Erbaugh
2869
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   734
                NonFullScreenPageMode=checkPDFNames(*'UseNone UseOutlines UseThumbs UseOC'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   735
                Direction=checkPDFNames(*'L2R R2L'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   736
                ViewArea=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   737
                ViewClip=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   738
                PrintArea=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   739
                PrintClip=checkPDFNames(*'MediaBox CropBox BleedBox TrimBox ArtBox'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   740
                PrintScaling=checkPDFNames(*'None AppDefault'.split()),
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
   741
                )
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   742
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   743
# stream filters are objects to support round trip and
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   744
# possibly in the future also support parameters
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   745
class PDFStreamFilterZCompress:
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   746
    pdfname = "FlateDecode"
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   747
    def encode(self, text):
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   748
        from reportlab.lib.utils import import_zlib
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   749
        zlib = import_zlib()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   750
        if not zlib: raise ImportError("cannot z-compress zlib unavailable")
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3723
diff changeset
   751
        if isUnicode(text):
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
   752
            text = text.encode('utf8')
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   753
        return zlib.compress(text)
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   754
    def decode(self, encoded):
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   755
        from reportlab.lib.utils import import_zlib
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   756
        zlib = import_zlib()
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   757
        if not zlib: raise ImportError("cannot z-decompress zlib unavailable")
674
c25a9dbfc27a Improved ImportError handling
rgbecker
parents: 628
diff changeset
   758
        return zlib.decompress(encoded)
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   759
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   760
# need only one of these, unless we implement parameters later
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   761
PDFZCompress = PDFStreamFilterZCompress()
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   762
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   763
class PDFStreamFilterBase85Encode:
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   764
    pdfname = "ASCII85Decode"
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   765
    def encode(self, text):
3975
4a3599863c11 eliminate from . imports in favour of absolutes to allow running modules
robin
parents: 3909
diff changeset
   766
        from reportlab.pdfbase.pdfutils import _wrap
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3747
diff changeset
   767
        text = asciiBase85Encode(text)
2767
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2740
diff changeset
   768
        if rl_config.wrapA85:
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2740
diff changeset
   769
            text = _wrap(text)
2ba5a1d26ad0 reportlab: make a85 wrap optional, add _reset to rl_config
rgbecker
parents: 2740
diff changeset
   770
        return text
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   771
    def decode(self, text):
3781
df8b57380768 import from reportlab.lib.lib.rl_accel
robin
parents: 3747
diff changeset
   772
        return asciiBase85Decode(text)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   773
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   774
# need only one of these too
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   775
PDFBase85Encode = PDFStreamFilterBase85Encode()
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   776
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   777
class PDFStream(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   778
    '''set dictionary elements explicitly stream.dictionary[name]=value'''
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   779
    ### compression stuff not implemented yet
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   780
    __RefOnly__ = 1 # must be at top level
3144
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   781
    def __init__(self, dictionary=None, content=None, filters=None):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   782
        if dictionary is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   783
            dictionary = PDFDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   784
        self.dictionary = dictionary
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   785
        self.content = content
3144
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   786
        self.filters = filters
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   787
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   788
        dictionary = self.dictionary
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   789
        # copy it for modification
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   790
        dictionary = PDFDictionary(dictionary.dict.copy())
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   791
        content = self.content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   792
        filters = self.filters
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   793
        if self.content is None:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   794
            raise ValueError("stream content not set")
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   795
        if filters is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   796
            filters = document.defaultStreamFilters
591
8ab476fd7d96 minor changes to support pdfparse
aaron_watters
parents: 504
diff changeset
   797
        # only apply filters if they haven't been applied elsewhere
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   798
        if filters is not None and "Filter" not in dictionary.dict:
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   799
            # apply filters in reverse order listed
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   800
            rf = list(filters)
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   801
            rf.reverse()
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   802
            fnames = []
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   803
            for f in rf:
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   804
                #print "*****************content:"; print repr(content[:200])
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   805
                #print "*****************filter", f.pdfname
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   806
                content = f.encode(content)
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   807
                fnames.insert(0, PDFName(f.pdfname))
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   808
            #print "*****************finally:"; print content[:200]
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   809
            #print "****** FILTERS", fnames
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   810
            #stop
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
   811
            dictionary["Filter"] = PDFArray(fnames)
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   812
        # "stream encoding is done after all filters have been applied"
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   813
        content = document.encrypt.encode(content)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   814
        fc = format(content, document)
3843
ee97902ffa44 pdfdoc.py, pdfform.py & pdfutils.py: eliminate LINEEND
robin
parents: 3840
diff changeset
   815
        dictionary["Length"] = len(content)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   816
        fd = format(dictionary, document)
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   817
        return fd+b'\nstream\n'+fc+b'endstream\n'
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   818
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   819
def teststream(content=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   820
    #content = "" # test
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   821
    if content is None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   822
        content = teststreamcontent
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   823
    content = content.strip() + '\n'
3359
171fa15695a8 reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents: 3327
diff changeset
   824
    S = PDFStream(content = content,
171fa15695a8 reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents: 3327
diff changeset
   825
                    filters=rl_config.useA85 and [PDFBase85Encode,PDFZCompress] or [PDFZCompress])
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   826
    # nothing else needed...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   827
    S.__Comment__ = "test stream"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   828
    return S
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   829
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   830
teststreamcontent = """
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   831
1 0 0 1 0 0 cm BT /F9 12 Tf 14.4 TL ET
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   832
1.00 0.00 1.00 rg
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   833
n 72.00 72.00 432.00 648.00 re B*
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
   834
"""
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   835
class PDFArray(PDFObject):
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   836
    multiline = True
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   837
    def __init__(self, sequence):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   838
        self.sequence = list(sequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   839
    def References(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   840
        """make all objects in sequence references"""
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   841
        self.sequence = list(map(document.Reference, self.sequence))
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   842
    def format(self, document, IND=b'\n '):
2541
495aea8213fb pdfdoc.py: more optimisations
rgbecker
parents: 2540
diff changeset
   843
        L = [format(e, document) for e in self.sequence]
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   844
        if (self.multiline and rl_config.pdfMultiLine) or self.multiline=='forced':
2541
495aea8213fb pdfdoc.py: more optimisations
rgbecker
parents: 2540
diff changeset
   845
            L = IND.join(L)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   846
        else:
3144
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   847
            n=len(L)
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   848
            if n>10:
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   849
                # break up every 10 elements anyway
3847
ca0ed3043694 pdfdoc.py: fix PDFArray.format
robin
parents: 3843
diff changeset
   850
                t=L.insert
ca0ed3043694 pdfdoc.py: fix PDFArray.format
robin
parents: 3843
diff changeset
   851
                for i in reversed(range(10, n, 10)):
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   852
                    t(i,b'\n ')
3847
ca0ed3043694 pdfdoc.py: fix PDFArray.format
robin
parents: 3843
diff changeset
   853
                L = b' '.join(L)
3144
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   854
            else:
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   855
                L = b' '.join(L)
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   856
        return b'[ ' + L + b' ]'
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   857
3144
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   858
class PDFArrayCompact(PDFArray):
a5d6bb711691 pdfdoc.py: added class PDFSeparationCMYKColorSpace
rgbecker
parents: 3112
diff changeset
   859
    multiline=False
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   860
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   861
class PDFIndirectObject(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   862
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   863
    def __init__(self, name, content):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   864
        self.name = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   865
        self.content = content
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   866
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   867
        name = self.name
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   868
        n, v = document.idToObjectNumberAndVersion[name]
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   869
        # set encryption parameters
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   870
        document.encrypt.register(n, v)
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   871
        fcontent = format(self.content, document, toplevel=1)   # yes this is at top level
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   872
        return (pdfdocEnc("%s %s obj\n"%(n,v))
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   873
                +fcontent+ (b'' if fcontent.endswith(b'\n') else b'\n')
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   874
                +b'endobj\n')
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   875
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   876
class PDFObjectReference(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   877
    def __init__(self, name):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   878
        self.name = name
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   879
    def format(self, document):
1317
2c0575047463 allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents: 1309
diff changeset
   880
        try:
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   881
            return pdfdocEnc("%s %s R" % document.idToObjectNumberAndVersion[self.name])
1317
2c0575047463 allow forms to be forward references (to allow table of contents, eg).
aaron_watters
parents: 1309
diff changeset
   882
        except:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   883
            raise KeyError("forward reference to %s not resolved upon final formatting" % repr(self.name))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   884
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   885
class PDFFile(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   886
    ### just accumulates strings: keeps track of current offset
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
   887
    def __init__(self,pdfVersion=PDF_VERSION_DEFAULT):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   888
        self.strings = []
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   889
        self.write = self.strings.append
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   890
        self.offset = 0
3808
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   891
        ### chapter 5
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   892
        # Following Ken Lunde's advice and the PDF spec, this includes
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   893
        # some high-order bytes.  I chose the characters for Tokyo
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   894
        # in Shift-JIS encoding, as these cannot be mistaken for
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   895
        # any other encoding, and we'll be able to tell if something
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   896
        # has run our PDF files through a dodgy Unicode conversion.
3843
ee97902ffa44 pdfdoc.py, pdfform.py & pdfutils.py: eliminate LINEEND
robin
parents: 3840
diff changeset
   897
        self.add((pdfdocEnc("%%PDF-%s.%s" % pdfVersion) +
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   898
            b'\n%\223\214\213\236 ReportLab Generated PDF document http://www.reportlab.com\n'
3843
ee97902ffa44 pdfdoc.py, pdfform.py & pdfutils.py: eliminate LINEEND
robin
parents: 3840
diff changeset
   899
            ))
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   900
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   901
    def closeOrReset(self):
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   902
        pass
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   903
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   904
    def add(self, s):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   905
        """should be constructed as late as possible, return position where placed"""
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   906
        s = pdfdocEnc(s)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   907
        result = self.offset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   908
        self.offset = result+len(s)
2477
704e05bad378 pdfdoc.py: minor changes to bring into line with TBlatter's early serializer version
rgbecker
parents: 2476
diff changeset
   909
        self.write(s)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   910
        return result
3808
11feb32dd300 pdfdoc.py: fix binary header stuff and signature
robin
parents: 3801
diff changeset
   911
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   912
    def format(self, document):
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   913
        return b''.join(self.strings)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   914
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   915
class PDFCrossReferenceSubsection(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   916
    def __init__(self, firstentrynumber, idsequence):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   917
        self.firstentrynumber = firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   918
        self.idsequence = idsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   919
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   920
        """id sequence should represent contiguous object nums else error. free numbers not supported (yet)"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   921
        firstentrynumber = self.firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   922
        idsequence = self.idsequence
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   923
        entries = list(idsequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   924
        nentries = len(idsequence)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   925
        # special case: object number 0 is always free
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   926
        taken = {}
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   927
        if firstentrynumber==0:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   928
            taken[0] = "standard free entry"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   929
            nentries = nentries+1
4299
a0e20256e0d7 fix xref table
robin
parents: 4297
diff changeset
   930
            entries.insert(0, "0000000000 65535 f ")
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   931
        idToNV = document.idToObjectNumberAndVersion
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   932
        idToOffset = document.idToOffset
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   933
        lastentrynumber = firstentrynumber+nentries-1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   934
        for id in idsequence:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   935
            (num, version) = idToNV[id]
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
   936
            if num in taken:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   937
                raise ValueError("object number collision %s %s %s" % (num, repr(id), repr(taken[id])))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   938
            if num>lastentrynumber or num<firstentrynumber:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   939
                raise ValueError("object number %s not in range %s..%s" % (num, firstentrynumber, lastentrynumber))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   940
            # compute position in list
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   941
            rnum = num-firstentrynumber
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   942
            taken[num] = id
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   943
            offset = idToOffset[id]
4299
a0e20256e0d7 fix xref table
robin
parents: 4297
diff changeset
   944
            entries[num] = '%0.10d %0.5d n ' % (offset, version)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   945
        # now add the initial line
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   946
        firstline = "%s %s" % (firstentrynumber, nentries)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   947
        entries.insert(0, firstline)
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   948
        # make sure it ends with \n
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   949
        entries.append("")
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   950
        return pdfdocEnc('\n'.join(entries))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   951
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   952
class PDFCrossReferenceTable(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   953
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   954
        self.sections = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   955
    def addsection(self, firstentry, ids):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   956
        section = PDFCrossReferenceSubsection(firstentry, ids)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   957
        self.sections.append(section)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   958
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   959
        sections = self.sections
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   960
        if not sections:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   961
            raise ValueError("no crossref sections")
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   962
        L = [b"xref\n"]
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   963
        for s in self.sections:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   964
            fs = format(s, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   965
            L.append(fs)
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   966
        return pdfdocEnc(b''.join(L))
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   967
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   968
class PDFTrailer(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   969
    def __init__(self, startxref, Size=None, Prev=None, Root=None, Info=None, ID=None, Encrypt=None):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   970
        self.startxref = startxref
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   971
        if Size is None or Root is None:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
   972
            raise ValueError("Size and Root keys required")
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   973
        dict = self.dict = PDFDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   974
        for (n,v) in [("Size", Size), ("Prev", Prev), ("Root", Root),
1062
c6dec1c5a962 extensions to support encryption plugins
aaron_watters
parents: 1014
diff changeset
   975
                      ("Info", Info), ("ID", ID), ("Encrypt", Encrypt)]:
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   976
            if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   977
                dict[n] = v
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   978
        dict.multiline='forced'
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   979
    def format(self, document):
4297
88d3b24abf35 minor changes to layout
robin
parents: 4296
diff changeset
   980
        fdict = self.dict.format(document,IND=b'\n')
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   981
        return b''.join([
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   982
                b'trailer\n',
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   983
                fdict,
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   984
                b'\nstartxref\n',
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   985
                pdfdocEnc(str(self.startxref)),
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
   986
                b'\n%%EOF\n',
3840
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   987
                ]
b10fadf9ccb5 pdfdoc.py: start move to using bytes
robin
parents: 3835
diff changeset
   988
                )
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   989
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   990
#### XXXX skipping incremental update,
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   991
#### encryption
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   992
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   993
#### chapter 6, doc structure
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   994
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
   995
class PDFCatalog(PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   996
    __Comment__ = "Document Root"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   997
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   998
    # to override, set as attributes
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
   999
    __Defaults__ = {"Type": PDFName("Catalog"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1000
                "PageMode": PDFName("UseNone"),
4184
4e8fc7fc7092 support top level Lang; version-->3.1.54
robin
parents: 4160
diff changeset
  1001
                "Lang": None,
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1002
                }
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1003
    __NoDefault__ = """
3629
f148d4473631 pdfdoc.py: fix openeactions-->openaction bug fix contributed by Andrew Cutler
rgbecker
parents: 3617
diff changeset
  1004
        Dests Outlines Pages Threads AcroForm Names OpenAction PageMode URI
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1005
        ViewerPreferences PageLabels PageLayout JavaScript StructTreeRoot SpiderInfo""".split()
4281
12c26acf8913 minor efficiency changes
robin
parents: 4252
diff changeset
  1006
    __Refs__ = __NoDefault__
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1007
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1008
    def format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1009
        self.check_format(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1010
        defaults = self.__Defaults__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1011
        Refs = self.__Refs__
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1012
        D = {}
4184
4e8fc7fc7092 support top level Lang; version-->3.1.54
robin
parents: 4160
diff changeset
  1013
        for k,v in defaults.items():
4e8fc7fc7092 support top level Lang; version-->3.1.54
robin
parents: 4160
diff changeset
  1014
            v = getattr(self,k,v)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1015
            if v is not None:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1016
                D[k] = v
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1017
        for k in self.__NoDefault__:
4184
4e8fc7fc7092 support top level Lang; version-->3.1.54
robin
parents: 4160
diff changeset
  1018
            v = getattr(self,k,None)
4e8fc7fc7092 support top level Lang; version-->3.1.54
robin
parents: 4160
diff changeset
  1019
            if v is not None:
4e8fc7fc7092 support top level Lang; version-->3.1.54
robin
parents: 4160
diff changeset
  1020
                D[k] = v
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1021
        # force objects to be references where required
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1022
        for k in Refs:
3326
ce725978d11c Initial Python3 compatibility fixes
damian
parents: 3318
diff changeset
  1023
            if k in D:
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1024
                #print"k is", k, "value", D[k]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1025
                D[k] = document.Reference(D[k])
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1026
        dict = PDFDictionary(D)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1027
        return format(dict, document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1028
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1029
    def showOutline(self):
2852
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1030
        self.setPageMode("UseOutlines")
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1031
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1032
    def showFullScreen(self):
2852
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1033
        self.setPageMode("FullScreen")
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1034
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1035
    def setPageLayout(self,layout):
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1036
        if layout:
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1037
            self.PageLayout = PDFName(layout)
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1038
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1039
    def setPageMode(self,mode):
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1040
        if mode:
397ae33d96fe pdfdoc.py: added setPageLayout and setPageMode
rgbecker
parents: 2838
diff changeset
  1041
            self.PageMode = PDFName(mode)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1042
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1043
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1044
        """for use in subclasses"""
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1045
        pass
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1046
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1047
class PDFPages(PDFCatalog):
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2200
diff changeset
  1048
    """PAGES TREE WITH ONE INTERNAL NODE, FOR "BALANCING" CHANGE IMPLEMENTATION"""
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1049
    __Comment__ = "page tree"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1050
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1051
    # note: could implement page attribute inheritance...
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1052
    __Defaults__ = {"Type": PDFName("Pages"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1053
                    }
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1054
    __NoDefault__ = "Kids Count Parent".split()
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1055
    __Refs__ = ["Parent"]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1056
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1057
        self.pages = []
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1058
    def __getitem__(self, item):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1059
        return self.pages[item]
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1060
    def addPage(self, page):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1061
        self.pages.append(page)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1062
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1063
        # convert all pages to page references
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1064
        pages = self.pages
2229
99cb312c5269 Fix transparency & jpeg behaviour
rgbecker
parents: 2200
diff changeset
  1065
        kids = PDFArray(pages)
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1066
        # make sure all pages are references
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1067
        kids.References(document)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1068
        self.Kids = kids
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1069
        self.Count = len(pages)
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
  1070
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1071
class PDFPage(PDFCatalog):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1072
    __Comment__ = "Page dictionary"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1073
    # all PDF attributes can be set explicitly
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1074
    # if this flag is set, the "usual" behavior will be suppressed
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1075
    Override_default_compilation = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1076
    __RefOnly__ = 1
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1077
    __Defaults__ = {"Type": PDFName("Page"),
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1078
                   # "Parent": PDFObjectReference(Pages),  # no! use document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1079
                    }
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1080
    __NoDefault__ = """Parent
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1081
        MediaBox Resources Contents CropBox Rotate Thumb Annots B Dur Hid Trans AA
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1082
        PieceInfo LastModified SeparationInfo ArtBox TrimBox BleedBox ID PZ
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1083
        Trans""".split()
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1084
    __Refs__ = """Contents Parent ID""".split()
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1085
    pagewidth = 595
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1086
    pageheight = 842
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1087
    stream = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1088
    hasImages = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1089
    compression = 0
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1090
    XObjects = None
3226
cd06c1172678 pdfgen,pdfbase: fix handling of CMYKColorSep spotNames with spaces etc
rgbecker
parents: 3198
diff changeset
  1091
    _colorsUsed = {}
3570
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
  1092
    _shadingsUsed = {}
500
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1093
    Trans = None
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1094
    # transitionstring?
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1095
    # xobjects?
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1096
    # annotations
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1097
    def __init__(self):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1098
        # set all nodefaults to None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1099
        for name in self.__NoDefault__:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1100
            setattr(self, name, None)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1101
    def setCompression(self, onoff):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1102
        self.compression = onoff
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1103
    def setStream(self, code):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1104
        if self.Override_default_compilation:
3721
0c93dd8ff567 initial changes from 2to3-3.3
rptlab
parents: 3687
diff changeset
  1105
            raise ValueError("overridden! must set stream explicitly")
3731
b233dd0577ff another round of changes mostly type related
rptlab
parents: 3723
diff changeset
  1106
        if isSeq(code):
4296
87b770e44067 don't use cr lf line endings any more
robin
parents: 4284
diff changeset
  1107
            code = '\n'.join(code)+'\n'
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1108
        self.stream = code
500
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1109
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1110
    def setPageTransition(self, tranDict):
58d712fef651 Fixed page transitions; extended Pythonpoint
andy_robinson
parents: 494
diff changeset
  1111
        self.Trans = PDFDictionary(tranDict)
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1112
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1113
    def check_format(self, document):
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1114
        # set up parameters unless usual behaviour is suppressed
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1115
        if self.Override_default_compilation:
1577
ad1dd1f8e061 Jerome Alet's auto transparency patch
rgbecker
parents: 1576
diff changeset
  1116
            return
2798
40f791c3cbf8 pdfdoc.py: allow for proper external forms info
rgbecker
parents: 2778
diff changeset
  1117
        self.MediaBox = self.MediaBox or PDFArray(self.Rotate in (90,270) and [0,0,self.pageheight,self.pagewidth] or [0, 0, self.pagewidth, self.pageheight])
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1118
        if not self.Annots:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1119
            self.Annots = None
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1120
        else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1121
            #print self.Annots
3723
99aa837b6703 second stage of port to Python 3.3; working hello world
rptlab
parents: 3721
diff changeset
  1122
            #raise ValueError("annotations not reimplemented yet")
4000
1718042b25c0 pdfdoc.py, pdfform.py & pdfencrypt.py: switch to using PDFObject inheritance(not __PDFObject__=True) to identify our instances
robin
parents: 3995
diff changeset
  1123
            if not isinstance(self.Annots,PDFObject):
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1124
                self.Annots = PDFArray(self.Annots)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1125
        if not self.Contents:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1126
            stream = self.stream
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1127
            if not stream:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1128
                self.Contents = teststream()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1129
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1130
                S = PDFStream()
484
cc20a344973c Aaron's latest update from the newslist
rgbecker
parents: 483
diff changeset
  1131
                if self.compression:
3359
171fa15695a8 reportlab: optional A85 patch contributed by Yoann Roman
rgbecker
parents: 3327
diff changeset
  1132
                    S.filters = rl_config.useA85 and [PDFBase85Encode, PDFZCompress] or [PDFZCompress]
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1133
                S.content = stream
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1134
                S.__Comment__ = "page stream"
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1135
                self.Contents = S
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1136
        if not self.Resources:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1137
            resources = PDFResourceDictionary()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1138
            # fonts!
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1139
            resources.basicFonts()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1140
            if self.hasImages:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1141
                resources.allProcs()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1142
            else:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1143
                resources.basicProcs()
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1144
            if self.XObjects:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1145
                #print "XObjects", self.XObjects.dict
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1146
                resources.XObject = self.XObjects
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
  1147
            if self.ExtGState:
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
  1148
                resources.ExtGState = self.ExtGState
3570
494980a689ff reportlab: add in shading technology contributed by Peter Johnson <johnson.peter@gmail.com>
rgbecker
parents: 3470
diff changeset
  1149
            resources.setShading(self._shadingUsed)
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
  1150
            resources.setColorSpace(self._colorsUsed)
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
  1151
481
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1152
            self.Resources = resources
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1153
        if not self.Parent:
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1154
            pages = document.Pages
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1155
            self.Parent = document.Reference(pages)
136669babedf complete revision of pdfdoc. Not finished (compression missing, testing needed)
aaron_watters
parents: 479
diff changeset
  1156
3198
683ca9eb6b18 reportlab: added in support for Overprint/Opacity & Separated colours (Opacity inspired by Simon King)
rgbecker
parents: 3172
diff changeset
  1157
#this code contributed by  Christian Jacobs <cljacobsen@gmail.com>
4195
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4188
diff changeset
  1158
class DuplicatePageLabelPage(Exception):
5abb3b7f93b1 added rl_settings.errorOnDuplicatePageLabelPage & support in PageLabel class for stable sorting
robin
parents: 4188
diff changeset
  1159
    pass
2869
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1160
class PDFPageLabels(PDFCatalog):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1161
    __comment__ = None
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1162
    __RefOnly__ = 0
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1163
    __Defaults__ = {}
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1164
    __NoDefault__ = ["Nums"]
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1165
    __Refs__ = []
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1166
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1167
    def __init__(self):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1168
        self.labels = []
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1169
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1170
    def addPageLabel(self, page, label):
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1171
        """ Adds a new PDFPageLabel to this catalog.
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1172
        The 'page' argument, an integer, is the page number in the PDF document
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1173
        with which the 'label' should be associated. Page numbering in the PDF
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1174
        starts at zero! Thus, to change the label on the first page, '0' should be
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1175
        provided as an argument, and to change the 6th page, '5' should be provided
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1176
        as the argument.
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1177
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1178
        The 'label' argument should be a PDFPageLabel instance, which describes the
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1179
        format of the labels starting on page 'page' in the PDF and continuing
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1180
        until the next encounter of a PDFPageLabel.
912456caa112 reportlab: add in PageLabel stuff from Christian jacobs and Date fix from Dinu Gherman
rgbecker
parents: 2852
diff changeset
  1181