Exemple #1
0
def register_font(font='Vera.ttf'):
    """Register fonts for report labs canvas."""
    directory = os.path.join(bundle_dir())
    ttfFile = resource_path(os.path.join(directory, font))
    if os.path.exists(ttfFile):
        pdfmetrics.registerFont(TTFont("Vera", ttfFile))
        return ttfFile
    else:
        print(ttfFile, 'can not be found')
Exemple #2
0
    def __init__(self,
                 document,
                 watermark,
                 underneath=False,
                 overwrite=False,
                 output=None,
                 suffix='watermarked',
                 decrypt=False,
                 tempdir=None,
                 method='pdfrw'):
        """
        Add a watermark to an existing PDF document

        1. Read PDF document and decrypt if encrypted
        2. Get input PDF document info
            2a. Retrieve dimensions
            2b. Determine orientation
            2c. Upscale to fit letter
        3. Get watermark info
            3a. Retrieve dimensions
            3b. Rotate if necessary
            3c. Upscale to fit document
        4. Set file names
            4a. Check if a upscaled doc was created, if not use original document
            4b. Check if watermark was rotated, if not use original watermark
        5. Add watermark to PDF document
            5a. Create watermarked document filename (ex: originalpdfname_watermarked.pdf)
            5b. Create PDF file reader and file writer objects
            5c. Add watermark to each page of original
            5d. Save watermarked document to file
        """
        self.rotate = 0
        self.scale = 0
        self.underneath = underneath
        self.tempdir = tempdir
        self.method = method

        self.document_reader = pypdf3_reader(document, decrypt)
        self.document = self._get_document_info(document)
        self.watermark_file = self._get_watermark_info(self.document, watermark)
        pdf_fname, wtrmrk_fname = self._set_filenames

        if overwrite:
            self.output_filename = document
        elif output:
            self.output_filename = output
        elif suffix:
            self.output_filename = add_suffix(document, suffix)
        else:
            tmpf = NamedTemporaryFile(suffix='.pdf', dir=self.tempdir, delete=False)
            self.output_filename = resource_path(tmpf.name)

        self.add(pdf_fname, wtrmrk_fname)
    def save(self, img=None, destination=None, file_name='pil', ext='.png'):
        img = self.img if not img else img
        if destination:
            output = os.path.join(destination, Path(file_name).stem + ext)
        elif self.tempdir:
            tmpimg = NamedTemporaryFile(suffix='.png',
                                        dir=self.tempdir.name,
                                        delete=False)
            output = resource_path(tmpimg.name)
            tmpimg.close()
        else:
            output = os.path.join(bundle_dir(), file_name + ext)

        # Save image file
        img.save(output)
        return output
Exemple #4
0
    def save(self, img=None, destination=None, file_name='pil'):
        img = self.img if not img else img
        fn = file_name.replace('.png',
                               '') if file_name.endswith('.png') else file_name
        if destination:
            output = os.path.join(destination, fn + '.png')
        elif self.tempdir:
            tmpimg = NamedTemporaryFile(suffix='.png',
                                        dir=self.tempdir.name,
                                        delete=False)
            output = resource_path(tmpimg.name)
            tmpimg.close()
        else:
            output = os.path.join(bundle_dir(), fn + '.png')

        # Save image file
        img.save(output)
        return output
Exemple #5
0
import os
from PyBundle import bundle_dir, resource_path


def _image_directory():
    directory = os.path.join(bundle_dir(), 'img')
    if os.path.exists(directory):
        return directory
    else:
        print(directory, 'can not be found')


IMAGE_DIRECTORY = _image_directory()


def available_images():
    imgs = [i for i in os.listdir(IMAGE_DIRECTORY) if not i.startswith('.')]
    if len(imgs) > 0:
        return sorted(imgs, reverse=True)
    else:
        return ['Add images...']


IMAGE_DEFAULT = resource_path('Wide.png')

__all__ = ['IMAGE_DEFAULT', 'IMAGE_DIRECTORY', 'available_images']
Exemple #6
0
 def dst(self):
     if not self._dst:
         with NamedTemporaryFile(suffix='.pdf', dir=self.dir, delete=False) as tmppdf:
             self._dst = resource_path(tmppdf.name)
     return self._dst