Exemplo n.º 1
0
    def save(self, file):
        #avoid crashes if they wrote nothing in the page
        if self.data is None:
            self.data = TestStream

        if self.compression == 1:
            comp = zlib.compress(self.data)  #this bit is very fast...
            base85 = pdfutils._AsciiBase85Encode(comp)  #...sadly this isn't
            data_to_write = pdfutils._wrap(base85)
        else:
            data_to_write = self.data
        # the PDF length key should contain the length including
        # any extra LF pairs added by Print on DOS.

        #lines = len(string.split(self.data,'\n'))
        #length = len(self.data) + lines   # one extra LF each
        length = len(data_to_write) + len(LINEEND)  #AR 19980202
        if self.fontType is None:
            fontStreamEntry = ""
        else:
            fontStreamEntry = "/Subtype %s" % (self.fontType)

        if self.compression:
            file.write(
                ('<<  %s /Length %d /Filter [/ASCII85Decode /FlateDecode] >>' %
                 (fontStreamEntry, length) + LINEEND).encode('utf-8'))
        else:
            file.write(('<< /Length %d %s >>' % (length, fontStreamEntry) +
                        LINEEND).encode('utf-8'))
        file.write(('stream' + LINEEND).encode('utf-8'))
        file.write(data_to_write + b'\r\n')
        file.write(('endstream' + LINEEND).encode('utf-8'))
Exemplo n.º 2
0
    def save(self, file):
        #avoid crashes if they wrote nothing in the page
        if self.data is None:
            self.data = TestStream

        if self.compression == 1:
            comp = zlib.compress(
                (self.data).encode('utf-8'))  #this bit is very fast...
            base85 = pdfutils._AsciiBase85Encode(comp)  #...sadly this isn't
            wrapped = pdfutils._wrap(base85)
            data_to_write = wrapped
        else:
            data_to_write = self.data
        # the PDF length key should contain the length including
        # any extra LF pairs added by Print on DOS.

        #lines = len(string.split(self.data,'\n'))
        #length = len(self.data) + lines   # one extra LF each
        length = len(data_to_write) + len(LINEEND)  #AR 19980202
        if self.compression:
            file.write(
                ('<< /Length %d /Filter [/ASCII85Decode /FlateDecode]>>' %
                 length + LINEEND).encode('utf-8'))
        else:
            file.write(('<< /Length %d >>' % length + LINEEND).encode('utf-8'))
        file.write(('stream' + LINEEND).encode('utf-8'))
        file.write((data_to_write + LINEEND).encode('latin-1'))  # Really?
        file.write(('endstream' + LINEEND).encode('utf-8'))
    def drawInlineImage(self, image, x, y, width=None, height=None):
        """Draw a PIL Image into the specified rectangle.  If width and
        height are omitted, they are calculated from the image size.
        Also allow file names as well as images.  This allows a
        caching mechanism"""
        # print "drawInlineImage: x=%s, y=%s, width = %s, height=%s " % (x,y, width, height)
        import zlib

        self._currentPageHasImages = 1

        if isinstance(image, str):
            if os.path.splitext(image)[1] in ['.jpg', '.JPG']:
                #directly process JPEG files
                #open file, needs some error handling!!
                imageFile = open(image, 'rb')
                info = self.readJPEGInfo(imageFile)
                imgwidth, imgheight = info[0], info[1]
                if info[2] == 1:
                    colorSpace = 'DeviceGray'
                elif info[2] == 3:
                    colorSpace = 'DeviceRGB'
                else:  #maybe should generate an error, is this right for CMYK?
                    colorSpace = 'DeviceCMYK'
                imageFile.seek(0)  # reset file pointer
                imagedata = []
                imagedata.append('BI')  # begin image
                # this describes what is in the image itself
                imagedata.append('/Width %0.4f /Height %0.4f' %
                                 (info[0], info[1]))
                imagedata.append('/BitsPerComponent 8')
                imagedata.append('/ColorSpace /%s' % colorSpace)
                imagedata.append('/Filter [ /ASCII85Decode /DCTDecode]')
                imagedata.append('ID')
                #write in blocks of (??) 60 characters per line to a list
                compressed = imageFile.read()
                encoded = pdfutils._AsciiBase85Encode(compressed)
                outstream = StringIO(encoded)
                dataline = outstream.read(60)
                while dataline != "":
                    imagedata.append(dataline)
                    dataline = outstream.read(60)
                imagedata.append('EI')
            else:
                if not pdfutils.cachedImageExists(image):
                    pdfutils.cacheImageFile(image)
                #now we have one cached, slurp it in
                cachedname = os.path.splitext(image)[0] + '.a85'
                imagedata = open(cachedname, 'rb').readlines()
                #trim off newlines...
                imagedata = list(map(str.strip, imagedata))

                #parse line two for width, height
                words = imagedata[1].split()
                imgwidth = int(words[1])
                imgheight = int(words[3])
        else:
            #PIL Image
            #work out all dimensions
            myimage = image.convert('RGB')
            imgwidth, imgheight = myimage.size
            imagedata = []
            imagedata.append('BI')  # begin image

            # this describes what is in the image itself
            imagedata.append(
                '/W %0.4f /H %0.4f /BPC 8 /CS /RGB /F [/A85 /Fl]' %
                (imgwidth, imgheight))
            imagedata.append('ID')

            #use a flate filter and Ascii Base 85 to compress
            raw = myimage.tostring()
            assert len(raw) == (imgwidth *
                                imgheight), "Wrong amount of data for image"
            compressed = zlib.compress(raw)  #this bit is very fast...
            encoded = pdfutils._AsciiBase85Encode(
                compressed)  #...sadly this isn't

            #write in blocks of (??) 60 characters per line to a list
            outstream = StringIO(encoded)
            dataline = outstream.read(60)
            while dataline != "":
                imagedata.append(dataline)
                dataline = outstream.read(60)
            imagedata.append('EI')

        #now build the PDF for the image.
        if not width:
            width = imgwidth
        if not height:
            height = imgheight

        # this says where and how big to draw it
        #self._code.append('ET')
        #self._code.append('q %0.4f 0 0 %0.4f %0.4f %0.4f cm' % (width, height, x, y+height))
        if self.bottomup:
            self._code.append('q %0.4f 0 0 %0.4f %0.4f %0.4f cm' %
                              (width, height, x, y))
        else:
            # multiply  height by (-1) to overcome flip in coordinate system -cwl
            self._code.append('q %0.4f 0 0 %0.4f %0.4f %0.4f cm' %
                              (width, -height, x, y + height))
        self._code.extend(imagedata)
        self._code.append('Q')